listview_array[]
包含Test,Category1
和Category2。ListActivity.java:
package com.steph.listview;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListActivity extends Activity {
private ListView lv;
private String listview_array[] = { "Test", "Category1", "Category2", "Category3"};
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.list1);
lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listview_array));
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}
list1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
输出:
我的问题是如果我单击ListView中的Test
。以下名称必须显示在新的ListView中:Test1,Test2和Test3。
然后如果我单击ListView中的Category1
。以下名称必须显示在新的ListView中:Video1,Video2和Video3。
然后如果我单击ListView中的Category2
。以下名称必须
显示在新的ListView中:Audio1,Audio2和Audio3。
我不知道该怎么做。我可以创建另外三个单独的 Test,Category1和Category2的类。然后我需要使用 意图。
但我的问题是如何才能在单课中完成。如果您有任何建议,请帮助我。谢谢。
答案 0 :(得分:4)
虽然我不知道您为什么要在重新使用时加载此活动的新实例,但这里有一个工作示例你想要什么:
package com.steph.listview;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* Created by Simon on 14.7.7.
*/
public class ListActivity extends Activity {
private ListView lv;
private String[] groupArray = {"Category1", "Category2", "Category3"};
private String[][] childArray = {{"Test1", "Test2", "Test3"},
{"Video1", "Video2", "Video3"}, {"Audio1", "Audio2", "Audio3"}};
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.list1);
lv = (ListView) findViewById(R.id.listview);
String[] data = getIntent().getStringArrayExtra("strArray");
AdapterView.OnItemClickListener clickListener = null;
// If no data received means this is the first activity
if (data == null) {
data = groupArray;
clickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(ListActivity.this, ListActivity.class);
intent.putExtra("strArray", childArray[position]);
startActivity(intent);
}
};
}
ArrayAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(clickListener);
}
}
答案 1 :(得分:1)
如果你需要根据你的位置加载项目,你不需要创建不同的类。单个类就足够了
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int position,
long arg3)
{
String list_item=(String) parent.getItemAtPosition(position);
Intent intent=new Intent(this,IntentListActivity.class);
intent.putExtra("List_item",list_item);
startActivity(intent);
}
});
IntentListactivity.java
package com.steph.listview;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class IntentListactivity extends Activity {
private ListView lv;
String[] test_array={"test1","test2","test3"};
String[] video_array={"Video1","video2","video3"};
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.list1);
lv = (ListView) findViewById(R.id.listview);
Intent intent = getIntent();
String list_value = (String) intent.getSerializableExtra("List_item");
if("Test".equals("list_value"))
{
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, test_array));
}
else if("Category1".equals("list_value")
{
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, video_array));
}
lv.setTextFilterEnabled(true);
}
}