0

我有一个列表视图,我希望每个按钮都打开不同的 Activity。

listview 有很多选项,每个选项都会导致另一个活动。

我不知道该怎么做。

谢谢。

爪哇

public class AndroidListViewActivity extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] adobe_products = getResources().getStringArray(
                R.array.adobe_products);

        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
                R.id.label, adobe_products));

        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                String product = ((TextView) view).getText().toString();

                Intent i = new Intent(getApplicationContext(), mavo.class);

                i.putExtra("product", product);
                startActivity(i);

            }  });}}

监听项 XML

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold" >

</TextView>
4

2 回答 2

1

我只是在猜测,因为你没有说你的代码出了什么问题。也许 ArrayAdapter 不仅仅使用 TextView 来布局每个列表项,因此您不能简单地将 TextView 拉出您正在做的事情。

试试这个来获取产品字符串: String product = adobe_products[position];

你将不得不做出String[] adobe_products最终决定。

编辑:根据我认为您要问的内容,试试这个:

lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Class<? extends Activity> activityToStart = null;

            switch (position){
            case 0:
                activityToStart = MyProduct0Activity.class;
                break;
            case 1:
                activityToStart = MyProduct1Activity.class;
                break;
            //etc.
            }

            Intent i = new Intent(getApplicationContext(), activityToStart);
            startActivity(i);

        }  });
于 2013-08-20T16:07:47.753 回答
0

Okie.. So here is what i understand from your comments. You have a listview and each item in the listview is a textview. And when you click on each item, you need to go to different screen / Activity.

If it is the case...

replace this code

 public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                String product = ((TextView) view).getText().toString();

                Intent i = new Intent(getApplicationContext(), mavo.class);

                i.putExtra("product", product);
                startActivity(i);

            }

with this..

     public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    Switch(position){
case 0:
String product = ((TextView) view).getText().toString();
Intent i = new Intent(AndroidListViewActivity.this, FIRST_SCREEN.class);
i.putExtra("product", product);
startActivity(i);
break;
case 1:
String product = ((TextView) view).getText().toString();
Intent i = new Intent(AndroidListViewActivity.this, SECOND_SCREEN.class);
i.putExtra("product", product);
startActivity(i);
break;

// Repeat the same for all screens
...
...
...

}


                }

Note ::: I have not tested this code. This is just to give you an idea on how to do it.

Hope this helps..

于 2013-09-04T10:10:49.360 回答