我搜索了stackoverflow但无法找到答案(确定有一个地方)但我正在尝试为每个listview输入添加一个活动。我可以用一个意图来管理它,但是我如何为每个listview输入分配一个单独的活动。所以要清楚,我想让每个项目都有一个血清活动。
目前使用此代码发起活动,但希望每个项目都有单独的活动。
public class ListviewActivityActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] adobe_products = getResources().getStringArray(R.array.list_products);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, adobe_products));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String product = ((TextView) view).getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), BMICalculatorActivity.class);
startActivity(i);
}
});
}
尝试添加此功能但无法正常工作
private static final int ACTIVITY_0 = 0;
private static final int ACTIVITY_1 = 1;
private static final int ACTIVITY_2 = 2;
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent();
// Set up different intents based on the item clicked:
switch (position)
{
case ACTIVITY_0:
intent.setClass(this, listview.activty.BMICalculatorActivity.class);
break;
case ACTIVITY_1:
intent.setClass(this, listview.activty.BodyLog.class);
break;
我知道它的排序很简单,所以任何帮助都会很棒。感谢
答案 0 :(得分:0)
尝试这样的事情:
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i;
// selected item
String product = (String) lv.getItem(position);
if(product.equals("photoshop"))
i = new Intent(ListviewActivity.this, Photoshop.class);
else if (product.equals("final cut pro"))
i = new Intent(ListviewActivity.this, FinalCutPro.class);
else
i = new Intent(ListviewActivity.this, Generic.class);
startActivity(i);
}
});
另一种选择是创建一个POJO,其中包含listview条目的名称和要调用的类。并添加一个数组而不是一个字符串数组。这将需要使用某种ArrayAdapter。然后,当你得到项目时,你只需致电startActivity(new Intent(listviewActivity.this, myPojo.classToCall);
你应该能够在某个地方找到一个很好的例子......
答案 1 :(得分:0)
我想你想要这样的东西:
Intent i = null;
switch (position)
{
case ACTIVITY_0:
i = new Intent(getApplicationContext(), BMICalculatorActivity.class);
break;
case ACTIVITY_1:
i = new Intent(getApplicationContext(), BodyLog.class);
break;
}
if(i != null)
{
startActivity(i);
}