In a separate adapter class, an ImageButton is defined. I want to access it from MainActivity in order to implement adding items from the Listview to my favorites. How to contact me?

Here is the adapter class

public class MyCursorAdapter extends CursorAdapter { private int layout; //Π½ΡƒΠΆΠ΅Π½ для создания ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ΠΎΠ² класса View public MyCursorAdapter(Context context, int layout, Cursor c, int flags) { super(context, c, flags); this.layout = layout; } public static class ViewHolder { public TextView txtBukva; public TextView txtSlovo; public ImageButton btnIzbrannoe; public void setBtnIzbrannoe(ImageButton btnIzbrannoe) { this.btnIzbrannoe = btnIzbrannoe; } } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(layout, parent, false); return view; } @Override public void bindView(View view, Context context, final Cursor cursor) { ViewHolder holder = new ViewHolder(); String bukva = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_SLOVO)).substring(0, 1).toUpperCase(); String slovo = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_SLOVO)); String izbrannoe = cursor.getString(cursor.getColumnIndex(Contract.Entry.COLUMN_IZBRANNOE)); holder.txtBukva = (TextView) view.findViewById(R.id.txtBukva); holder.txtSlovo = (TextView) view.findViewById(R.id.txtSlovo); holder.btnIzbrannoe = (ImageButton) view.findViewById(R.id.btnIzbrannoe); holder.txtBukva.setText(bukva); holder.txtSlovo.setText(slovo); holder.btnIzbrannoe.setFocusable(false); if (izbrannoe.equals("1")) { holder.btnIzbrannoe.setImageResource(R.drawable.icon_star_yellow); } else if (izbrannoe.equals("0")) { holder.btnIzbrannoe.setImageResource(R.drawable.icon_star_outline_black); } } } 
  • I recently answered questions here on the basis of very similar code. Do you study somewhere now? Where? - tse

2 answers 2

If the list is in activation (and not a fragment), then it is very easy to process a click on one of the View items of the list. In the xml markup of the item for the desired button, specify the onClick attribute and specify the name of the method for processing the click (here onFavoriteClick ):

 <Button android:id="@+id/btnIzbrannoe" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onFavoriteClick" android:text="Favorite" /> 

In the activit that displays the list, create a method with the same name and the following signature:

 public void onFavoriteClick(View view) { // ДСйствия ΠΏΡ€ΠΈ ΠΊΠ»ΠΈΠΊΠ΅ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ btnIzbrannoe Π² Π°ΠΉΡ‚Π΅ΠΌΠ΅ } 

The required additional data can be transferred via setTag() for this button in the adapter and obtained in the activation from View , which is returned by the callback. For example, pass the cursor position in the adapter:

Adapter:

 @Override public void bindView(View view, Context context, final Cursor cursor) { ViewHolder holder = new ViewHolder(); holder.btnIzbrannoe = (ImageButton) view.findViewById(R.id.btnIzbrannoe); holder.btnIzbrannoe.setTag(cursor.getPosition()); } 

Activate:

 public void onFavoriteClick(View view) { Integer position = (Integer)view.getTag(); } 

So you can transfer any object, if you need several data, you can use collections or a composite object-model.

Unfortunately, this simple method does not work in fragments and you need to organize your own callback interface (callback)

  • I have activations. No fragments - Veronica
  • created a new question - Veronica

First, you need to define a callback interface, through which the adapter returns to the MainActivity information about clicking on the "favorites" button in a particular item. Something like this:

 public interface OnFavoriteListener { void onFavoriteClicked(Integer id); } 

The second is to add a method to the adapter to which the lisner object will be transmitted. Or pass it through the constructor.

 public class MyCursorAdapter extends CursorAdapter { OnFavoriteListener callback; .... public void setOnFavoriteListener(OnFavoriteListener callback) { this.callback = callback; } } 

Third. It is necessary to bind its unique id to each displayed element. What is id for you, I do not understand: there is a column with id in the database, or it will be a line from bukva or slovo. Choose yourself. I would tie it to the root view element. Accordingly, in bindView, you need to do this:

 public void bindView(View view, Context context, final Cursor cursor { .... Integer id = .....; view.setTag(id); } 

Fourth. You need a button click handler that will restore the id and pass it to the MainActivity using a liner:

 public View newView(Context context, Cursor cursor, ViewGroup parent) { .... view.findViewById(R.id.btnIzbrannoe).setOnClickListener(new OnClickListener() { void onClick(View v) { if (callback != null) { Integer id = (Integer)v.getTag(); callback.onFavoriteClicked(id); } }); return view; } 

Fifth, in the MainActivity create a lisner of clicks on your button, in it write the change to the database and reload the adapter.

I am writing "out of my head", without checking, there may be typos in the code, delve into the essence.

  • The second also did not understand how to do it. I finished a long time ago, but we didn’t even come close to java and, moreover, Android programming. Therefore, it is now hard. What is the callback variable where to declare it? (( - Veronica
  • Added callback. - tse
  • And from the MainActivity, access the button using the onFavoriteClicked () method ?? onFavoriteClicked (Integer id); - here id is view id? type R.id.btnIzbrannoe? - Veronica
  • On the contrary, this is the button to access the MainActivity when it is clicked. The mechanism is essentially no different from the usual click listener. - tse
  • Id - this is the record by which you can change the flag "favorites" in the database. To understand which element klienuli. - tse