单击列表项以在单独的活动中显示内容

时间:2014-10-14 06:46:22

标签: android listview android-listview

点击Map1列表项时,我需要在单独的活动中显示Category1.java班级内容。

MainActivity.java:

package com.steph.listview;

import android.app.ListActivity;
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 MainActivity extends ListActivity {

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

 String[] parentArray = {"Map1","Map2","Map3","Map4","Map5"}; 

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
 android.R.layout.simple_list_item_1,parentArray);

 setListAdapter(adapter);

 ListView lv = getListView();

 lv.setOnItemClickListener(new OnItemClickListener() {

 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position,
 long id) {


 }
 });
 }
}

activity_main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
   </LinearLayout>

输出:

最后,我得到了这样的输出。

enter image description here

现在我唯一的问题是,如果我单击Map1列表项,我需要在单独的Activity.So中显示内容,我正在为此创建Category1类。下面我发布了与Category1相关的代码。

Category1.java:

package com.steph.listview;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Category1 extends Activity {

    @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.category1);
}

}

category1.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Map contenets have to display in this textView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click" />

</LinearLayout>

4 个答案:

答案 0 :(得分:2)

如果您愿意,可以使用以下代码

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
    Intent i = new Intent(MainActivity.this, Category1.class);
    i.putExtra("new_variable_name","value");
    startActivity(i);
}

然后在Category1活动中,检索这些值:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("new_variable_name");
}

答案 1 :(得分:1)

MainActivity.java:

lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                if(position==0){
                    Intent in=new Intent(MainActivity.this,Category1.class);
                    intent.putExtra("TagName", data);
                    startActivity(in);
                }
            }
        });

Category1.java:

内部oncreate()

String data = getIntent().getExtras().getString("TagName");//If data is of string type

答案 2 :(得分:1)

MainActivity.java:

package com.steph.listview;

import android.app.ListActivity;
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 MainActivity extends ListActivity 
 {

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

    final String[] parentArray = {"Map1","Map2","Map3","Map4","Map5"}; 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,parentArray);

    setListAdapter(adapter);

    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) 
        {
            Intent intent = new Intent(MainActivity.this,Map1.class);
            intent.putExtra("id", parentArray[position]);
            startActivity(intent);

        }
    });
}
}

<强> Map1.java:

   package com.steph.listview;

     import android.app.Activity;
     import android.os.Bundle;
     import android.widget.TextView;

     public class Map1 extends Activity 
       {
         TextView textView;
         @Override
         protected void onCreate(Bundle savedInstanceState) 
          {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.category1);
           textView= (TextView) findViewById(R.id.textView1);
           textView.setText(getIntent().getExtras().getString("id"));
          }
        }

答案 3 :(得分:0)

在你的onItemClick电话

StartActivity(new Intent(MainActivity.this,Category.class).putExtra("data",parentArray[position]));

然后在你的Category1的OnCreate

String data = getIntent().getExtras().getString("data");

然后在Textview上设置数据

TextView myText =(TextView)findviewbyid(R.id.textView1);
myText.setText(data);