将活动添加到ListView
问题描述:
我想创建一个ListView,其中的项目在第二个活动中打开不同的图片(当它们被选中时)。将活动添加到ListView
我已经做了一些的,但无法弄清楚如何编写代码显示在第二个活动不同的图片。
任何帮助表示赞赏。
以下是我迄今所做的:
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView listView;
String[] country = {
"India",
"France",
"Germany",
"Japan",
"The US",
"Spain",
"Brazil",
"Korea"
};
Integer[] countryFlag = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic7,
R.drawable.pic8,
};
@Override
protected void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
CustomListAdapter adapter = new CustomListAdapter(this, country, countryFlag);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, NextScreen.class);
intent.putExtra("position", position);
intent.putExtra("country", country);
intent.putExtra("countryFlag", countryFlag);
startActivity(intent);
}
});
}
}
CustomListAdapter.java
public class CustomListAdapter extends ArrayAdapter<String> {
Activity context;
String[] country;
Integer[] countryFlag;
static class ViewHolder{
public ImageView img;
public TextView txt;
}
public CustomListAdapter(Activity context, String[] country, Integer[] countryFlag) {
super(context, R.layout.second_layout, country);
this.context=context;
this.country=country;
this.countryFlag=countryFlag;
}
public View getView (int position, View view, ViewGroup parent) {
View row = view;
if (row == null) {
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.second_layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.img = (ImageView) row.findViewById(R.id.img);
viewHolder.txt = (TextView) row.findViewById(R.id.txt);
row.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) row.getTag();
holder.txt.setText("The country of " + country[position]);
holder.img.setImageResource(countryFlag [position]);
return row;
}
}
NextScreen.java
公共类NextScreen扩展活动{
String[] position;
String[] country;
int countryFlag;
@Override
protected void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
setContentView(R.layout.third_layout);
Intent intent = getIntent();
position = intent.getStringArrayExtra("position");
country = intent.getStringArrayExtra("country");
countryFlag = intent.getExtras().getInt("countryFlag");
}
}
答
你在意图传递和接收附加信息的方式是错误的。你正在传递某些东西并阅读其他内容。
String[] country;
int[] countryFlag;
int position;
@Override
protected void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
setContentView(R.layout.third_layout);
//assuming you have an image view in the layout with id flag
ImageView flagImage = (ImageView)findViewById(R.id.flag);
Intent intent = getIntent();
country = intent.getStringArrayExtra("country");
countryFlag = intent.getIntArrayExtra("countryFlag");
position = intent.getExtras().getInt("position");
int imageID = countryFlag[position];
//now you can use this imageId to set on a imageView
flagImage.setImageResource(imageID);
}
[离线]您可以简化显示图像。您可以创建一个简单的对话框,而不是创建新的活动,而是创建一个具有虚拟视图的'setContentView',并将资源添加到该视图并简单地显示它。 – PedroHawk