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