我使用listview适配器创建了一个listview,扩展了基本适配器。
工作正常。但我需要将onitemClick Listener添加到listview,当我点击列表项时,它会转到另一个活动,以及listitem。我们可以这样做吗?
这是我在listview适配器中的代码
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;
}
}
我使用
填充主要活动中的列表项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);
答案 0 :(得分:1)
在列表视图中创建onItemClickListener,如下所示:
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);
}
});
要将您的额外数据用于下一个活动,请使用您的意图: Intent doc
答案 1 :(得分:1)
使用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);
}
});
答案 2 :(得分:1)
试试这个:
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);
}
});
答案 3 :(得分:1)
以及listitem
如果你想从ListItem获取一些数据,只需在已经提到的监听器中使用parent.getItemAtPosition(position)
。这将返回相应的Object
,您可以使用
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);
在第二个活动中获取它:
Bundle extras = getIntent().getExtras();
String one = extras.getStringExtra("One");
String two = extras.getStringExtra("Two");