简体   繁体   中英

Make onClickListener of Custom ListView

I Have Implemented a Custom Listview & i am facing problem on creating its onClickListener. In OnCreate()

lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new CustomAdapter(MainActivity.this,q));

& CustomAdapter class:

 class CustomAdapter extends ArrayAdapter<GS>
  {
       ArrayList<GS> list;
       LayoutInflater mInfalter;    
       public CustomAdapter(Context context, ArrayList<GS> list)
       {
           super(context,R.layout.customlayout,list);
          this.list= list;  
          mInfalter = LayoutInflater.from(context);
        for(int i=0;i<list.size();i++)
        {
            Log.i("................",""+list.get(i).getAS_name());
        }
       }
}
       @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
          Log.i("..........","Hello in getView");
          if(convertView==null)
          {
               convertView = mInfalter.inflate(R.layout.customlayout,parent,false);
               holder = new ViewHolder();
               holder.tv1 = (TextView)convertView.findViewById(R.id.textView1); 
               convertView.setTag(holder); 
          }else{
                holder = (ViewHolder)convertView.getTag();
          } 

                holder.tv1.setText(list.get(position).getAS_name());

          return convertView;
    }

  }

 static class ViewHolder
    {
        TextView tv1;
    }  

Does its OnClickListener has to be made in getView() method ? Or it can be made in the OnCreate() ? And How?

Thanks

Here's a standalone example I just put together and tested that I hope will help you:

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<MyCustomAdapterItem> items = new ArrayList<MyCustomAdapterItem>();
        items.add(new MyCustomAdapterItem("1", "one"));
        items.add(new MyCustomAdapterItem("2", "two"));

        ListView listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, items));
        listView.setOnItemClickListener(this);
    }

    private class MyAdapter extends ArrayAdapter<MyCustomAdapterItem> {
        public MyAdapter(Context context, int textViewResourceId, List<MyCustomAdapterItem> objects) {
            super(context, textViewResourceId, objects);
        }
    }

    private class MyCustomAdapterItem {
        private String id;
        private String text;

        public MyCustomAdapterItem(String id, String text) {
            this.id = id;
            this.text = text;
        }

        public String getText() {
            return text;
        }

        public String getId() {
            return id;
        }

        @Override
        public String toString() {
            return getText();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        MyCustomAdapterItem myCustomAdapterItem = (MyCustomAdapterItem) adapterView.getItemAtPosition(position);
        if ("1".equals(myCustomAdapterItem.getId())) {
            Toast.makeText(this, "I can't believe you just selected this item!!!", Toast.LENGTH_SHORT).show();
        } else if ("2".equals(myCustomAdapterItem.getId())) {
            Toast.makeText(this, "This item is better than the first", Toast.LENGTH_SHORT).show();
        }
    }
}

Notice in this example that the Activity implements the AdapterView OnItemClickListener interface. Whenever you click any item in the list, the onItemClick method will be called. Just make sure to also set the activity as the OnItemClickListener, or else onItemClick won't be called:

listView.setOnItemClickListener(this)

This onItemClick implementation demonstrates one way that you can conditionally perform different logic, depending on the item that was clicked. You could alternately just use the position argument, if you know the fixed position index ahead of time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM