SibinF
SibinF

Reputation: 385

How to set OnitemClick in custom listview

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);

Upvotes: 0

Views: 1203

Answers (4)

Droidman
Droidman

Reputation: 11608

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");

Upvotes: 1

r0-
r0-

Reputation: 2498

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

Upvotes: 1

Android_coder
Android_coder

Reputation: 9993

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);
}
});

Upvotes: 1

Nermeen
Nermeen

Reputation: 15973

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);
   }
 });

Upvotes: 1

Related Questions