Reputation: 197
I want to create a ListView whose items, in a second activity, open different pictures (when they are selected).
I've done some of it but couldn't figure out how to write the codes for showing different pictures in the second activity.
Any help is appreciated.
Here is what I've done so far:
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
public class NextScreen extends Activity {
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");
}
}
Upvotes: 0
Views: 46
Reputation: 838
The way you are passing and receiving the extras in the intent is wrong. You are passing something and reading something else.
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);
}
Upvotes: 1