java - listview 1 Activity 用于打开 listview 2 Activity

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

我希望 listview1 打开我的包中定义的一些 Activity ,并且在这些 Activity 上我重新定义了一个新的 listview2 Activity 。

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

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
        }
    });

}

}

list :-

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

日志 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->X1 时开始 Activity 。 如果我不清楚,请在下面的评论中提及。 任何形式的建议都值得赞赏。

最佳答案

classNames 数组 A ListActivity 更改为:

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

因为您在 AndroidManifest.xml 中使用 X1、X2 和 X3 类名声明 Activity

关于java - listview 1 Activity 用于打开 listview 2 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15415202/

相关文章:

android - Xamarin Studio 无法识别 Android SDK 路径

android - 从自定义 ListView 中删除项目

Android 在 & 符号后修剪其余的深层链接 URI

android - 在同一 Activity 中在 android 中播放 youtube 视频有哪些替代方案?

android - 如何将其他 Intent 值传递给 ACTION_OPEN_DOCUMENT_TREE Intent

java - 如何直接从ftl模板调用FreeMarker指令?

java - 用户 session 困惑::Spring Security

java - Spring MVC 异常 - 调用请求方法导致异常 : public static native long java. lang.System.currentTimeMillis()

java - Spring 事务 - 需要新的 - 父级回滚

java - 扩展输入的 EditText 宽度