listview 1用于打开listview 2活动的活动

时间:2013-03-14 16:42:42

标签: java android android-intent android-widget android-listview

我希望listview1打开我的包中定义的一些活动,在这些活动中,我重新定义了一个新的listview2活动。

我试图编码相同。 请查看代码以了解问题。

public class ABC extends ListActivity{

String classNames[] = {"A","B","C"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,classNames));
}

protected void onListItemClick(ListView lv, View v, int position, long id){
    super.onListItemClick(lv, v, position, id);
    String openClass = classNames[position];
    try{
        Class selected = Class.forName("com.lab.example."+openClass);
        Intent selectedIntent = new Intent(this, selected);
        startActivity(selectedIntent);

    }
    catch(ClassNotFoundException e){
        e.printStackTrace();
    }

}

上面的源代码a,b,c类可以在listview中看到。但是当我点击A时,没有任何反应。

我想知道      @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setListAdapter(new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,classNames)); }

我如何在

上面添加自定义布局?

而为   A类: -

public class A extends ListActivity{

String classNames[] = {"x1","x2","x3"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, classNames));
}

protected void onListItemClick(ListView lv, View v, int position, long id){
    super.onListItemClick(lv, v, position, id);
    String openClass = classNames[position];
    try{
        Class selected = Class.forName("com.lab.example."+openClass);
        Intent selectedIntent = new Intent(this, selected);
        startActivity(selectedIntent);

    }
    catch(ClassNotFoundException e){
        e.printStackTrace();
    }

}

on the above source code x1,x2,x3 class activites are working for A only

而X1: -

public class X1 extends Activity {
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Listview Data
    String products[] = getResources().getStringArray(R.array.X1);
    lv = (ListView) findViewById(R.id.list_view11);
    inputSearch = (EditText) findViewById(R.id.inputSearch11);
    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list, R.id.route, products);
    lv.setAdapter(adapter);      
    inputSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            X1.this.adapter.getFilter().filter(cs);
        }
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }
        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });

}

}

清单: -

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lab.example"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.lab.example.MainActivitypage"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Menu"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.lab.example.Menu" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>



   <activity android:name="com.lab.example.ABC" />
   <activity android:name="com.lab.example.A" />
   <activity android:name="com.lab.example.B" />
   <activity android:name="com.lab.example.C" />
   <activity android:name="com.lab.example.X1" />
   <activity android:name="com.lab.example.X2" />
   <activity android:name="com.lab.example.X3" />

log cat中显示的错误: -

日志充满了这个错误没有别的,因为我是新手android不能理解这一点: -

03-14 23:16:55.970: E/PGA(3459): PgaSocketWriteAllHdipc: hd_ipc_send() failed
03-14 22:32:23.810: E/InputDispatcher(1304): channel 'b466cb20      com.lab.example/com.lab.example.ABC (server)' ~ Channel is unrecoverably broken and will be disposed! 
03-14 22:34:45.480: E/ALSALib(1293): external/alsa-lib/src/confmisc.c:136:    (snd_config_get_bool) Invalid type for nonblock

我想点击A-&gt; X1开始活动。 如果我不清楚,请在下面的评论中提及。 任何形式的建议都表示赞赏。

2 个答案:

答案 0 :(得分:1)

classNames A数组更改为ListActivity

String classNames[] = {"X1","X2","X3"};

因为您使用X1,X2和X3类名称在AndroidManifest.xml中声明活动

答案 1 :(得分:0)

试试这个:

Intent intent = new Intent();
intent.setClassName("com.lab.example", "com.lab.example." + openClass);
startActivity(intent);