1

I have a ListView with the following Adapter :

public class gestionAdapter extends ArrayAdapter<Sav> {

    public gestionAdapter(Context context, List<Sav> savs){
        super(context, 0, savs);
    }
     @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.adapter_gestion,parent, false);
        }
         Sav sav = getItem(position);
        ViewHolderGestion viewHolder = (ViewHolderGestion) convertView.getTag();
        if(viewHolder == null){
            viewHolder = new ViewHolderGestion();
            viewHolder.titreSav = (TextView) convertView.findViewById(R.id.TitreSav);
            viewHolder.intervenant = (TextView) convertView.findViewById(R.id.interv);
            viewHolder.client = (TextView) convertView.findViewById(R.id.cli);
            viewHolder.archive = (Button) convertView.findViewById(R.id.archive);
            viewHolder.actif = (Button) convertView.findViewById(R.id.actif);
            convertView.setTag(viewHolder);
            viewHolder.archive.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getContext(), "archive ", Toast.LENGTH_LONG).show();
                }

            });
            viewHolder.actif.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getContext(), "actif ", Toast.LENGTH_LONG).show();
                }
            });
        }
        viewHolder.titreSav.setText(sav.getTitre());
        viewHolder.intervenant.setText(sav.getIntervenant());
        viewHolder.client.setText(sav.getNomClient());
        return convertView;
    }
}

As you notice, there are two buttons for each row. At the moment, I've put the listeners in the Adapter But is it possible to put these listeners in the receiving view, so I can use these buttons to refresh my View for example?

At the moment I've tried to make some of the functions of my View public, but I'm not satisfied at all with that solution.

Amir
  • 16,067
  • 10
  • 80
  • 119
  • I dont understand you completely but You can put listener for your Root layout too. Also your ViewHolder pattern logic in your Adapter is not correct. – Amir May 18 '16 at 07:51
  • it seems I can't access these elements from the Activity that displays the listView – RiddlerNewComer May 18 '16 at 07:52
  • You can either put listner on root layout, put listner on listView someListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Log.d("############","Items " + MoreItems[arg2] ); } }); – Rafal May 18 '16 at 07:54
  • You should define listener in your adapter and notify that. – Amir May 18 '16 at 07:56
  • I understand the general concept of what you guys tell me, but do you know where I could find a commented sample of code? I'm not that good on that aspect of android developement, it would help me a lot – RiddlerNewComer May 18 '16 at 07:58

2 Answers2

0

First things to mention your viewHolder pattern is not correct take look at here.

Second thing is that if you want to access to your rootLayout inside activity easiest way is define Listener in your Adapter and then notify that in your Activity.

for example define following interface in your Adapter and create instance in your adapter :

private OnBackClickListener onBackClickListener;

 public interface OnBackClickListener {
        void onBackClick();
    }

Then when your rootLayout clicked in your adapter call following method :

rootView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onBackClickListener != null) {
                    onBackClickListener.onBackClick();
                }
            }
        });

In your activity pass this listener to your Adapter like this :

MyAdapter adapter = new MyAdapter(context,new OnBackClickListener(){//rest of things});

Now in your Activity when rootLayout clicked OnBackClickListener of your Adapter notify you.

In case of RecyclerView you can find complete implementation here.

Community
  • 1
  • 1
Amir
  • 16,067
  • 10
  • 80
  • 119
0

You can add the listeners to your activity but you wont be able to point out which view has been clicked.Instead you can do it like this

public static class ViewHolderTwo extends RecyclerView.ViewHolder implements View.OnClickListener {
    private TextView dictionaryWord;
    private TextView dictionaryShortcut;
    private CheckBox deleteWord;
    public interface Listener
    { public void viewOneclicked();
       public void viewTwoClicked();
    }
    private Listener mListener;


    public ViewHolderTwo(View v,Listener listener) {
        super(v);
        this.mListener = listener;
        dictionaryWord = (TextView) v.findViewById(R.id.dictionaryWord);
        dictionaryShortcut = (TextView) v.findViewById(R.id.dictionaryShortcut);
        deleteWord = (CheckBox) v.findViewById(R.id.deleteWord);
    }

    @Override
    public void onClick(View v) {
        if(v == dictionaryWord)
        {
        mListener.viewOneClicked();
        return;
       }
}

and make your activity implement listener and pass its reference in viewholders constructor

  • And this way i'll be able to use the methods in my activity in the listener? – RiddlerNewComer May 18 '16 at 08:08
  • Yes you can use activity methods with viewholder by casting it to activity and checking for null pointer.But make sure that you wont hold any references to listener if the activity is no longer in memory – user3300839 May 18 '16 at 08:20