How to set OnitemClick in custom listview

别说谁变了你拦得住时间么 提交于 2019-12-12 02:23:33

问题


I created a listview using listview adapter that extends base adapter.

It works fine. but I need to add onitemClick Listener to listview, when I click a list item it go to another activity ,along with the listitem.how can we do this ?

This is my code in the listview adapter

public class listviewAdapter extends BaseAdapter{
    String FIRST_COLUMN = Constant.FIRST_COLUMN;
    String SECOND_COLUMN = Constant.SECOND_COLUMN;
    String THIRD_COLUMN = Constant.THIRD_COLUMN;
    String FOURTH_COLUMN = Constant.FOURTH_COLUMN;
    public ArrayList<HashMap<String,String>> list;
    Activity activity;

    public listviewAdapter(Activity activity, ArrayList<HashMap<String,String>> list) {
    super();
    this.activity = activity;
    this.list = list;
    }

    @Override
    public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
    }

    @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
    }

    @Override
    public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
    }

    private class ViewHolder {
       TextView txtFirst;
       TextView txtSecond;
       TextView txtThird;
       TextView txtFourth;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi=convertView;
    // TODO Auto-generated method stub
            ViewHolder holder;
            LayoutInflater inflater =  activity.getLayoutInflater();

            if (convertView == null)
            {
                convertView = inflater.inflate(R.layout.requestcard_columnsdetails, null);
                holder = new ViewHolder();




                holder.txtFirst = (TextView) convertView.findViewById(R.id.date);
                holder.txtSecond = (TextView) convertView.findViewById(R.id.from);
                holder.txtThird = (TextView) convertView.findViewById(R.id.to);
                holder.txtFourth = (TextView) convertView.findViewById(R.id.time);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }

            HashMap<String, String> map = list.get(position);
            holder.txtFirst.setText(map.get(FIRST_COLUMN));
            holder.txtSecond.setText(map.get(SECOND_COLUMN));
            holder.txtThird.setText(map.get(THIRD_COLUMN));
            holder.txtFourth.setText(map.get(FOURTH_COLUMN));



        return convertView;
    }
}

And I populated list items in the main activity using

int a,b;
list = new ArrayList<HashMap<String,String>>();
for ( a=0; a<outputString.length;a++){
    HashMap<String,String> temp1 = new  HashMap<String,String>();
    temp1.put( FIRST_COLUMN,outputString[a][0]);
    temp1.put(SECOND_COLUMN, outputString[a][1]);
    temp1.put(THIRD_COLUMN, outputString[a][4]);
    temp1.put(FOURTH_COLUMN, outputString[a][5]);
    list.add(temp1);
}
listviewAdapter adapter = new listviewAdapter(this, list);
lview.setAdapter(adapter);

回答1:


along with the listitem

if you want to get some data from the ListItem, just use parent.getItemAtPosition(position) inside the listener which was already mentioned. This will return a corresponding Object that you can perform further actions with

   HashMap<String, String> map = (HashMap<String, String>) parent.getItemAtPosition(position);
  String one = map.get("key");
  String two = map.get("key2");

  Intent next = new Intent(FirstActivity.this, NextActivity.class);
  next.putExtra("One", one);
  next.putExtra("Two", two);
  startActivity(next);

Get it in your second Activity:

 Bundle extras = getIntent().getExtras();
 String one = extras.getStringExtra("One");
 String two = extras.getStringExtra("Two");



回答2:


Use setOnItemClickListener:

lview.setOnItemClickListener(new OnItemClickListener() {

   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

       Intent intent = new Intent(Activity.this, secondActivity.class);
    startActivity(intent);
   }
 });



回答3:


Try this:

list_view.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
        {

         Intent myIntent = new Intent(Current_Activity.this,Next_Activity.class);
         startActivity(myIntent);
}
});



回答4:


Create your onItemClickListener in your listview like this:

lview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView parent, View view,
                    int position, long id) {
     Intent intent = new intent(mainactivity.this, target.class);
     // maybe put some extra arguments to the intent 
     startActivity(intent);

            }
        });

To put your extra data to the next activity use your intent: Intent doc



来源:https://stackoverflow.com/questions/15337153/how-to-set-onitemclick-in-custom-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!