Java 初学者Android:ListView影响下一节课
我是一个初学者程序员,所以请容忍我。我正在尝试创建一个应用程序,其中列表视图中的项目会影响下一个活动中显示的内容。到目前为止,我已经列出了以下活动:Java 初学者Android:ListView影响下一节课,java,android,eclipse,android-layout,android-intent,Java,Android,Eclipse,Android Layout,Android Intent,我是一个初学者程序员,所以请容忍我。我正在尝试创建一个应用程序,其中列表视图中的项目会影响下一个活动中显示的内容。到目前为止,我已经列出了以下活动: public class Primary extends ListActivity{ private static final String[] items = {"Item1", "Item2", "Item3", "item4", "Item5"}; @Override protected void onCreate(Bundle saved
public class Primary extends ListActivity{
private static final String[] items = {"Item1", "Item2", "Item3", "item4", "Item5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
TextView heading =(TextView)findViewById(R.id.listViewHeading);
heading.setText("Primary");
}
public void onListItemClick(ListView parent, View v, int position, long id){
}
如何使标题、图像和文本根据在列表视图中选择的项目进行更改?在listview活动中
Intent i = new Intent(this, ImageActivity.class);
i.putExtra("data", data);
startActivity(i);
下一个acitivity onCreate()方法
使用作为意图一部分的“附加”功能
从主服务器调用start ImageActivity时,可以使用“extras”在两者之间传递信息
有关详细信息,请参阅链接
我在这里给你举一个基本的例子。单击列表项时,使用“putExtra”将希望ImageActivity拥有的数据放入意图中
然后,在ImageActivity onCreate中,按如下方式检索数据:
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String data= extras.getString("DATA"); // matches the tag used in putExtra
}
检索完数据后,设置必要的视图。我想您需要在第二个活动中设置标题、图像和文本,与列表中第一个活动的选定索引相关 只需做一件事,在第一个活动中输入以下代码
public void onListItemClick(ListView parent, View v, int position, long id)
{
Intent intent = new Intent(this.getApplicationContext(), ImageActivity.class);
intent.putExtra("pos", position);
startActivity(intent);
}
private void addInfo()
{
Bundle ext = getIntent().getExtras();
if(ext != null)
{
int pos= ext.getInteger("pos");
// ext.getInt("pos");
heading.setText(headings[pos]);
// hey, frend, you don't have any array for selecting image-name and text
// image.setImageResource(images[x]);
// text.setText(text[x]);
}
}
所以,现在传递列表中所选项目的位置
现在,在下一个活动中输入以下代码
public void onListItemClick(ListView parent, View v, int position, long id)
{
Intent intent = new Intent(this.getApplicationContext(), ImageActivity.class);
intent.putExtra("pos", position);
startActivity(intent);
}
private void addInfo()
{
Bundle ext = getIntent().getExtras();
if(ext != null)
{
int pos= ext.getInteger("pos");
// ext.getInt("pos");
heading.setText(headings[pos]);
// hey, frend, you don't have any array for selecting image-name and text
// image.setImageResource(images[x]);
// text.setText(text[x]);
}
}
使用下面的代码
public void onListItemClick(ListView parent, View v, int position, long id)
{
Intent intent = new Intent(Primary.this, ImageActivity.class);
intent.putExtra("selected value", item[position]);
startActivity(intent);
}
在ImageActivity类中:在oncreate中(或者您可以将项变量设置为全局)
Intent Intent=新的Intent(getApplicationContext(),ImageActivity.class);Intent Intent=new Intent(this.getApplicationContext(),ImageActivity.class);
public void onListItemClick(ListView parent, View v, int position, long id)
{
Intent intent = new Intent(Primary.this, ImageActivity.class);
intent.putExtra("selected value", item[position]);
startActivity(intent);
}
String item = getIntent().getStringExtra("Selected value");