[英]Click the list item to show the contents in separate activity
While clicking the Map1
list item, I need to show the Category1.java
Class contents in separate activity. 单击
Map1
列表项时,我需要在单独的活动中显示Category1.java
类的内容。
MainActivity.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: 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>
Output: 输出:
Finally, I got an output like this. 最后,我得到了这样的输出。
Now my only problem is if I click the Map1
list item, I need to display the contents in separate Activity.So that I am creating the Category1
class for that. 现在我唯一的问题是,如果我单击
Map1
列表项,则需要在单独的Activity中显示内容,以便为此创建Category1
类。 Below I am posted the codes related to Category1
. 下面,我张贴了与
Category1
相关的代码。
Category1.java: 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: 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>
If you like to do that, you can use following code 如果您愿意,可以使用以下代码
@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);
}
Then in the Category1
Activity, retrieve those values: 然后在
Category1
活动中,检索这些值:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
MainActivity.java: 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: Category1.java:
Inside oncreate()
里面的
oncreate()
String data = getIntent().getExtras().getString("TagName");//If data is of string type
MainActivity.java: 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: 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"));
}
}
In your onItemClick call 在您的onItemClick通话中
StartActivity(new Intent(MainActivity.this,Category.class).putExtra("data",parentArray[position]));
then in OnCreate of your Category1 然后在您的Category1的OnCreate中
String data = getIntent().getExtras().getString("data");
Then Set the data on your Textview 然后在您的Textview上设置数据
TextView myText =(TextView)findviewbyid(R.id.textView1);
myText.setText(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.