我有一个列表视图。我想单击每个列表项目并打开不同的活动。其实,我写了代码,但它不工作。我的编码技能无法处理这个问题。如何用字符串和类对创建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对和Array Adapter。
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>
感谢您的澄清。现在,我明白了。我正在修复它。再次感谢。 – SOURAV
当然,如果它给你你正在寻找的东西,随时接受我的答案。 – GhostCat