0

我正在做一个当前需要列表的项目,当单击列表中的特定项目时,会出现另一个列表活动。有没有关于如何从一个列表视图活动移动到另一个的解决方案。我目前正在使用http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/作为我的 ListView 的参考。

4

2 回答 2

0

api 提供ExpandableListView。你的要求看起来很接近它

于 2012-09-23T11:01:42.710 回答
0

一种解决方案是:

创建第二个 ListView 活动并使用 OnItemClickListener 实现第一个 ListActivity,该 OnItemClickListener 使用常规 Intent 打开第二个 ListViewActivity。

    listView = (ListView) findViewById(R.id.mylistview);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position,
                    long arg3) {

                Intent intent = new Intent(FirstListActivity.this, SecondListActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("pos", position);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

更新:

我写了一个简单的示例列表应用程序。你可以用它来检查。可以打开第二个列表活动的代码已包含在内,但已被注释掉。如果你让这个例子运行起来,你离得越来越近了。然后你可以尝试注释掉 Intent 代码。

package com.adpog.listviewexample;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Find the ListView resource.   
        ListView mainListView = (ListView) findViewById( R.id.my_list );  

        // Set the Adapter as the ListView's adapter.  
        mainListView.setAdapter( new BaseAdapter(){

            // Create and populate a List of planet names.  
            String[] planets = new String[] {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune" };    

            @Override
            public int getCount() {
                return planets.length;
            }

            @Override
            public Object getItem(int pos) {
                return planets[pos-1];
            }

            @Override
            public long getItemId(int pos) {
                return 0;
            }

            @Override
            public View getView(int pos, View view, ViewGroup viewgroup) {
                if(view == null){
                    /**
                     <?xml version="1.0" encoding="utf-8"?>
                        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                            android:orientation="horizontal" 
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent">
                            <ImageView 
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" 
                                android:src="@drawable/ic_launcher"
                             />
                            <TextView
                                android:id="@+id/item_text" 
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" 
                                android:text="Text not set" />
                        </LinearLayout>
                    */
                    view = View.inflate(getApplicationContext(), R.layout.row, null);
                }
                return view;
            }
        });        

        mainListView.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView< ? > arg0, View arg1, int arg2, long arg3) {
                Log.d("ListView", "Pos: " + arg2 + ", long : "+arg3);
                Toast.makeText(getApplicationContext(), "Test " + arg2, Toast.LENGTH_SHORT).show();
                /* Alternative way; opens a new Activity
                Intent intent = new Intent(this, SecondListViewActivity.class);
                intent.putExtra("position", pos);
                startActivity(intent);
                */
            }

            @Override
            public void onNothingSelected(AdapterView< ? > arg0) {
            }
        });

        /**
         * Implement an action for each item click.
         */
        mainListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView< ? > arg0, View arg1, int arg2, long arg3) {
                Log.d("ListView", "OnClickPos: " + arg2 + ", long : "+arg3);
                Toast.makeText(getApplicationContext(), "Test " + arg2, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
于 2012-09-23T11:12:35.877 回答