在Android应用程序中ListView中的ListView

时间:2012-09-23 10:54:59

标签: android android-layout listview

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

2 个答案:

答案 0 :(得分:0)

api提供ExpandableListView。你的要求看起来非常接近

答案 1 :(得分:0)

一种解决方案是:

创建第二个ListView活动,并使用OnItemClickListener实现第一个ListActivity,它使用常规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);
            }
        });

<强>更新

我写了一个简单的示例列表应用程序。您可以将它用于检查。包含可以打开第二个列表活动的代码,但已注释掉。如果你运行这个例子,你就越来越近了。然后你可以尝试注释掉意图代码。

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