您好我在列表视图中使用列表视图我有veg和nonveg.so当我点击veg它应该移动到下一个活动并显示项目,当我点击Non-veg它应该到下一个活动。我已经为两个列表项编写了setonclickitemlistener(),但是当我按veg或非veg时,它只会进入非veg活动。这是代码
public class MainActivity extends Activity {
String[] items={"veg","non-veg"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv= (ListView)findViewById(R.id.listView1);
ArrayAdapter<Object> adapter1 = new ArrayAdapter<Object (this,android.R.layout.simple_list_item_1,items);
lv.setAdapter(adapter1);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
{
Toast.makeText(getBaseContext(),"YOU HAVE SELECTED VEG", 4000).show();
Intent i=new Intent(MainActivity.this,veg.class);
startActivity(i);
}
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(),"YOU HAVE SELECTED NON-VEG", 4000).show();
Intent i=new Intent(MainActivity.this,Nonveg.class);
startActivity(i);
}
});
} }
请告诉我如何识别veg和non veg的id以及如何编写setonclicklistener()方法。
答案 0 :(得分:0)
你只能拥有一个OnItemClickListener ......那将需要检查视图以确定去哪里。
答案 1 :(得分:0)
此代码完全正常运行:
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv = (ListView)findViewById(R.id.listView1);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
final String[] values = new String[] { "Veg", "Non-veg" };
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (arg2 == 0) {
Intent intent = new Intent(MainActivity.this, veg.class);
startActivity(intent);
}
if (arg2 ==1) {
Intent intent = new Intent(MainActivity.this, Nonveg.class);
startActivity(intent);
}
}
});
}
arg2是列表中项目的位置
答案 2 :(得分:0)
点击listview项目获取listview中位置的项目..检查其veg或non veg
public class MainActivity extends Activity {
String[] items={"veg","non-veg"};
ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView)findViewById(R.id.listView1);
setContentView(lv);
ArrayAdapter<Object> adapter1 = new ArrayAdapter<Object> (this,android.R.layout.simple_list_item_1,items);
lv.setAdapter(adapter1);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
{
String itemselected =(String) (lv.getItemAtPosition(arg2));
if(itemselected.equals("veg")){
Toast.makeText(getBaseContext(),"YOU HAVE SELECTED VEG", 4000).show();
Intent i=new Intent(MainActivity.this,veg.class);
startActivity(i);
}
else if(itemselected .equals("non-veg"))
{
Toast.makeText(getBaseContext(),"YOU HAVE SELECTED NON VEG",4000).show();
Intent i=new Intent(MainActivity.this,nonveg.class);
startActivity(i);
}
}
}
});
}
});