我有一个 ListView 。我想单击每个列表项,它将打开不同的 Activity 。事实上,我写了代码,但它不起作用。我的编码能力无法解决这个问题。如何使用字符串和类对创建 Hahmap,然后将其放入 ArrayAdapter 中。谁能给我看一下代码吗?
ListViewAcivity.java
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
//错误就在这里......//这个构造函数无法解决。//我不知道如何使用//String, Class 对与数组适配器。
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){
};
});}
}
main_screen_Activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
最佳答案
安ArrayAdapter期望显示对象的数组或列表。所以你需要提供一个数组/列表
是什么让您认为它可以处理 map ?列表/数组代表事物的序列;而 map 代表的是从某种事物到其他事物的映射。
换句话说:您无法传递 map 对象。
只需传递包含要显示的字符串的列表或数组(“医疗提醒”,...)。
但是拥有那张 map 是有值(value)的;你只需要稍微改变一下你的代码:
首先,您可以在创建 ArrayAdapter 时使用 map 的键作为输入,如下所示:
List<String> arrayItems = new ArrayList<>(hashMap.keySet());
然后,在 onClickItem()
中,您不需要需要检索索引 - 因为您已经有了可以告诉您哪个类属于哪个字符串的映射!含义:ListView 将所选项目作为字符串 提供,而hashMap
将所有可能的字符串映射到相应的 Activity 类!
因此,您可以扔掉整个“switch”代码,而是执行以下操作:
String itemValue = (String) listView.getItemAtPosition(position);
Class classForSelectedValue = hashMap.get(itemValue);
Intent activity = new Intent(ListViewActivity.this, classForSelectedValue);
关于java - 如何创建一个 ListView ,以便单击每个列表项将打开不同的 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39136569/