How to Pass Activity Object into Onbindviewholder Method for Using That One in Getscaledbitmap Method

How to pass Activity object into onBindViewHolder method for using that one in getScaledBitmap method

Try to use this {(AppCompatActivity)context} instead of context

How to start activity from onClick on RecyclerView in actvity

Try this:

in your onResume(): you are already attaching setOnItemClickListener into the PhotoRecyclerViewAdapter

@Override
protected void onResume() {
super.onResume();
((PhotoRecyclerViewAdapter) mAdapter).setOnItemClickListener(new PhotoRecyclerViewAdapter
.MyClickListener() {
@Override
public void onItemClick(int position, View v) {
Log.i(LOG_TAG, " Clicked on Item " + position);
Intent intent = new Intent(FrameListActivity.this , FinalActivity.class);
intent.putExtra("resultpos", "" + position);
startActivity(intent);
}
});
}

How to get reference of Activity in the Adapter class

What I was required to do was typecast the context passed to Activity and then move the code where I bind to OnPostExecute like so -

    @Override
protected void onPostExecute(String s) {
super.onPostExecute(s);

((Activity)mContext).runOnUiThread(new Runnable() { // here is the change
@Override
public void run() {

_vwHolder.ProductImageUrl.setImageBitmap(_btmp);
}
});
}

Even though Sunil kind of said the same thing in the end, but he understood the problem wrong. I got this solution from another post, so posting exactly what I coded now in my code.

My app works properly now. Thanks.

How to set LiveData from Adapter?

You shouldn't do it that way, here is a course about RecyclerView that goes from displaying to handle clickListener.

Here is how they define it:

While the ViewHolder is a great place to listen for clicks, it's not usually the right place to handle them. You should usually handle clicks in the ViewModel, because the ViewModel has access to the data and logic for determining what needs to happen in response to the click.

So you should be doing this:

Adapter:

class ThumbnailAdapter(model: SharedViewModel, val clickListener: ThumbnailListener) :
RecyclerView.Adapter<ThumbnailAdapter.CustomViewHolder>() {

private var thumbnailList = listOf<Pair<String, File>>()
private val myModel = model

override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val item = getItem(position)
holder.itemView.setOnClickListener {
listener.onClick(item)
}
holder.bind(marsProperty)
}

inner class CustomViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private val thumbnail = view.thumbnail
private val title = view.title

fun bind(item: Pair<String, File>) {
val titleToSet = item.first
val bitmapToSet = Util.uriToBitmap(item.second)
val resizedBitmapToSet = Bitmap.createScaledBitmap(bitmapToSet, 150, 150, false)
thumbnail.setImageBitmap(resizedBitmapToSet)
title.text = titleToSet
}
}

class ThumbnailListener(val clickListener: (libraryId: Long) -> Unit) {
fun onClick(val library: Library) = clickListener(library.id)
}

[...]
}

ViewModel:

private val _navigateToBook = MutableLiveData<Long>()
val navigateToBook: LiveData<Long>()
get() = _navigateToBook

fun onLibraryClicked(libraryId: Long) {
// your stuff
_navigateToBook.value = libraryId
}

Fragment:

thumbnailAdapter = ThumbnailAdapter(model, ThumbnailListener { libraryId ->
viewModel.onLibraryClicked(libraryId)
})

viewModel.navigateToBook.observe(viewLifecycleOwner, Observer { book ->
book?.let {
this.findNavController().navigate(YOUR_DESTINATION)
}
})


Second possiblity to set listener with databinding this time:

xml item file:

<data>

<variable
name="listener"
type="Your.Package.To.ThumbnailListener" />


</data>

<FrameLayout
android:layout_width="match_parent"
android:layout_height="170dp"
android:onClick="@{() -> listener.onClick(property)}">

...

</FrameLayout>

Adapter:

fun bind(item: Pair<String, File>, listener: ThumbnailListener) {
val titleToSet = item.first
val bitmapToSet = Util.uriToBitmap(item.second)
val resizedBitmapToSet = Bitmap.createScaledBitmap(bitmapToSet, 150, 150, false)
thumbnail.setImageBitmap(resizedBitmapToSet)
title.text = titleToSet
itemView.clickListener = listener
}

How to Save the Values of recycler view in Sqlite Database

So you are already passing the arraylist (teaItemList) in your constructor of your adapter. Since the reference is same you can use the same array list for saving or sending it to the database.

Since name and price of items are same, You need to get the value only like for if user clicks + = String.valueOf(count+ 1) and for (-) = String.valueOf(count - 1)

So add one more field like (int count) in teaPOJO and
whenever user clicks + :
Do this

holder.ivPlus.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
int count=0;
TeaPojo teaPojo = teaItemList.get(getAdapterPosition);
try{
count = Integer.parseInt(holder.tvcount.getText().toString());
}
catch(Exception e) {
count = 0;
}
//count++;
count = Integer.parseInt(holder.tvcount.getText().toString());
holder.tvcount.setText(String.valueOf(count+ 1));
teaPojo.setCount(count);
Log.e ("count", String.valueOf (count));
}

});

User clicks - :

holder.ivMinus.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View view) {
int count=0;
TeaPojo teaPojo = teaItemList.get(getAdapterPosition);
try{

count = Integer.parseInt(holder.tvcount.getText().toString());
}
catch(Exception e) {
count = 0;
}
if(count>0) {
//count--;
count = Integer.parseInt(holder.tvcount.getText().toString ());
holder.tvcount.setText (String.valueOf (count - 1));
teaPojo.setCount(count);
tealist.setCount (count);
}enter code here
}

});
// if you want to save in db from activity itself only
saveToDb(teaItemList);
// if you want to send to server from activity itself only
handleSendToServer(teaItemList);

Loading a Bitmap thumbnail into a RecyclerView with AsyncTask bug

Figured it out!
I added code to change the photo to the placeholder image after every bind. This is what I changed in my adapter.

 @Override
public void onBindViewHolder(FileHolder holder, int position) {
FolderFile file = mFiles.get(position);
holder.bindFile(file);

if (file.isPhoto()) {
Drawable placeholder = getResources().getDrawable(R.mipmap.picture_blu);
holder.mImagePreview.setBackground(placeholder);
holder.mImagePreview.setImageBitmap(null);


createThumbnail = new CreateThumbnail(holder, position, file.getId());
createThumbnail.execute();
}
}


Related Topics



Leave a reply



Submit