我已经使用 baseadapter 创建了列表视图。当点击不同的项目但无法获得项目价值时,我想去不同的活动。所以任何人都可以帮助我如何做到这一点。例如点击项目 xi 想要活动 1 点击项目说 z,我想去 avctivity2 谢谢。
这是代码:
public class homeScreen extends Activity{
ListView list1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.homescreen);
list1=(ListView)findViewById(R.id.spacelist);
list1.setAdapter(new MySimpleAdapter(this));
list1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {
// TODO Auto-generated method stub
Intent i=new Intent(homeScreen.this, privateSpaceList.class);
startActivity(i);
}
});
}
}
class SingleRow{
String title;
int image;
public SingleRow(String title,int image) {
// TODO Auto-generated constructor stub
this.title=title;
this.image=image;
}
}
class MySimpleAdapter extends BaseAdapter{
ArrayList<SingleRow> list;
Context context;
public MySimpleAdapter(Context c) {
// TODO Auto-generated constructor stub
context=c;
list=new ArrayList<SingleRow>();
//putting actual values in array
Resources res=c.getResources();
String[] titles=res.getStringArray(R.array.titles);
int[] images={R.drawable.error,R.drawable.ic_launcher,R.drawable.ic_launcher};
//putting single row in arraylist
for(int i=0;i<3;i++)
{
list.add(new SingleRow(titles[i], images[i]));
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int i) {
// TODO Auto-generated method stub
return list.get(i);
}
@Override
public long getItemId(int i) {
// TODO Auto-generated method stub
return i;
}
//called when want to display row
@Override
public View getView(int i, View view, ViewGroup viewgroup) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row,viewgroup,false);
TextView title=(TextView)row.findViewById(R.id.label);
ImageView image=(ImageView)row.findViewById(R.id.imageView);
SingleRow temp=list.get(i);
title.setText(temp.title);
image.setImageResource(temp.image);
return row;
}
}