如何创建Listview以便单击每个列表项将打开不同的活动?

时间:2016-08-25 04:09:03

标签: java android listview

我有一个列表视图。我想点击每个列表项,它会打开不同的活动。实际上,我编写了代码,但它不起作用。我的编码技能无法解决问题。如何使用字符串和类对创建Hahmap,然后将其放入ArrayAdapter。任何人都可以给我看一下代码吗?

ListViewAcivity.java

content = (snapshot.value as? NSDictionary)?["content"] as? String ?? ""

//错误在这里...... //这个构造函数无法解决。 //我不知道如何使用// String,Class pair with Array Adapter。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class ListViewActivity extends Activity {
    ListView listView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_screen_activity);

        // Get ListView object from xml
        listView = (ListView) findViewById(R.id.list);

        // Defined Array values to show in ListView
        HashMap<String, Class> hashMap=new HashMap<String, Class>();


         hashMap.put("A Function", MActivity.class);
         hashMap.put("B Function",AActivity.class);
         hashMap.put("c Function",XActivity.class);
         hashMap.put("D Function",ZActivity.class);
         hashMap.put("E Function", PActivity.class);
         hashMap.put("F Function", QActivity.class);


        // 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

main_screen_Activity.xml

  ArrayAdapter<HashMap<String,Class>> adapter = new ArrayAdapter<HashMap<String,Class>>(this, android.R.layout.simple_list_item_1, android.R.id.text1, hashMap);


        // Assign adapter to ListView
        listView.setAdapter(adapter);

        // ListView Item Click Listener
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                // ListView Clicked item index
                int itemPosition     = position;

                // ListView Clicked item value
                String  itemValue    = (String) listView.getItemAtPosition(position);
                switch(itemPosition){
                    case 0:  Intent newActivity = new Intent(ListViewActivity.this, MActivity.class);
                        startActivity(newActivity);
                        break;
                    case 1:  Intent newActivity1 = new Intent(ListViewActivity.this, AActivity.class);
                        startActivity(newActivity1);
                        break;
                    case 2:  Intent newActivity2 = new Intent(ListViewActivity.this, XActivity.class);
                        startActivity(newActivity2);
                        break;
                    case 3:  Intent newActivity3 = new Intent(ListViewActivity.this, ZActivity.class);
                        startActivity(newActivity3);
                        break;
                    case 4:  Intent newActivity4 = new Intent(ListViewActivity.this, PActivity.class);
                        startActivity(newActivity4);
                        break;
                    case 5:  Intent newActivity5 = new Intent(ListViewActivity.this, QActivity.class);
                        startActivity(newActivity5);
                        break;


                }

            }
            @SuppressWarnings("unused")
            public void onClick(View v){
            };
        });}
}

2 个答案:

答案 0 :(得分:2)

ArrayAdapter要求显示数组列表对象。所以你需要提供一个数组/列表

是什么让你认为它可以处理地图?列表/数组代表事物的序列;而地图代表了从某种东西到其他东西的映射。

换句话说:你不能传递地图对象。

只需传递包含您要显示的字符串的列表或数组(&#34;医疗提醒&#34;,...)。

但是拥有那张地图是有价值的;你只需要改变你的代码:

首先,您可以在创建ArrayAdapter时使用地图的作为输入,如下所示:

List<String> arrayItems = new ArrayList<>(hashMap.keySet());

然后,在onClickItem()中,需要检索索引 - 因为您已经拥有该地图,告诉您哪个类属于哪个字符串!含义:ListView为您选择的项目为 string hashMap将所有可能的字符串映射到相应的活动类!

所以,你可以扔掉你的整个&#34;开关&#34;代码,而是做类似的事情:

String itemValue = (String) listView.getItemAtPosition(position);
Class classForSelectedValue = hashMap.get(itemValue);
Intent activity = new Intent(ListViewActivity.this, classForSelectedValue);

答案 1 :(得分:1)

您要求的功能的简单静态示例。

public class MainActivity extends AppCompatActivity {

    ListView lv1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Initialize the list view
        lv1 = (ListView) findViewById(R.id.mainlist);

        //Add the List data
        //as the array is stored starting with 0, the Layouts will be having 0th position, Intents being 1 and so on..
        String[] sessiontuts = new String[]{"Activity 1", "Activity2"};

        //use the Simple array adapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,sessiontuts);

        //now to bind the data to list, just set the adapter we just created to the listview,
        lv1.setAdapter(adapter);

        //we need to have click listner on the particular item,
        //all the items in list will have a position starting from 0 to n,
        //so, write the intent code to launch particular activity depending on list item position
        lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                //using switch case, to check the condition.
                switch (pos){
                    case 0:
                        startActivity(new Intent(getApplicationContext(), Act1.class));
                        break;
                    case 1:
                        startActivity(new Intent(getApplicationContext(), Act2.class));
                        break;
                }
            }
        });
    }
}