我已經使用baseadapter創建了listview。 我想在不同的項目上點擊不同的項目,但無法獲得項目價值。所以任何人都可以幫助我如何去做。 例如點擊項目x我想活動1 點擊項目說z,我想去avctivity2 謝謝。Listview和baseadapter
這裏是代碼:
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;
}
}
設置行佈局可點擊 –
您可以在onItemClickListener中開啓位置值。 – Jumpa