
Question:
I'm trying to make an app that contains multiple listviews with onclicklisteners. So far, I have correctly coded one listview to open up an activity that displays an image. Because I have hundreds of images I want to display, I want to reduce the amount of activities that I will have to create in the manifest. I am curious as to what coding lines I can implement into my current code that will open an activity with an imageview and display "xyz.jpg", but display "second.jpg" if a different list option is clicked. My code is below:
public class obstetriclist extends AppCompatActivity {
ListView listview;
//String[] fullcardiaclist = new String[]{"APGAR Score", "Childbirth / Labor", "Childbirth Procedure", "Newly Born", "Obstetrical Emergency"};
//42 pictures
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_obstetriclist);
final Context context = getApplicationContext();
//list contains image name, image location
final List<ImageDisplay> myImageList = new ArrayList<ImageDisplay>();
myImageList.add(new ImageDisplay("Aspirin", R.drawable.aspirin));
myImageList.add(new ImageDisplay("Drug2", R.drawable.ekghomepic));
myImageList.add(new ImageDisplay("Drug3", R.drawable.aspirin));
myImageList.add(new ImageDisplay("Drug4", R.drawable.ekghomepic));
//define ListView and create onItemClick Listener
listview = (ListView) findViewById(R.id.obstetriclistview);
ArrayAdapter<ImageDisplay> arrayAdapters = new ArrayAdapter<ImageDisplay>(this, R.layout.support_simple_spinner_dropdown_item, myImageList);
listview.setAdapter(arrayAdapters);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(context, imagedisplay.class);
myIntent.putExtra("name", myImageList.get(position).getName());
myIntent.putExtra("imagePath", myImageList.get(position).getPath());
startActivityForResult(myIntent, 0);
}
} }
public class ImageDisplay {
private String Name;
private int Path; // use String if you use a path, in here i'm storing image in drawable
public ImageDisplay() {
}
public ImageDisplay(String name, int path) {
this.Name = name;
this.Path = path;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getPath() {
return Path;
}
public void setPath(int path) {
this.Path = path;
}
}
Answer1:Just create 1 activity (or fragment) that contain <ImageView/>
tag. Whenever open activity, you send the image src inside Intent (or in Bundle if using Fragment).
Then in this activity onCreate method, read the intent and put it in ImageView src
<strong>EDIT:</strong> Even though it's not the best approach to use ListView, hope this will give you some idea:
Here is my Main Activity, which creating the ListView. I also create a custom object class to handle both Image resource id and Image name:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
final Context context = getApplicationContext();
//list contains image name, image location
final List<ImageDisplay> myImageList = new ArrayList<ImageDisplay>();
myImageList.add(new ImageDisplay("Drug1", R.drawable.drug1));
myImageList.add(new ImageDisplay("Drug2", R.drawable.drug2));
myImageList.add(new ImageDisplay("Drug3", R.drawable.drug3));
myImageList.add(new ImageDisplay("Drug4", R.drawable.drug4));
//define ListView and create onItemClick Listener
listview = (ListView)findViewById(R.id.druglistview);
ArrayAdapter<ImageDisplay> arrayAdapters = new ArrayAdapter<ImageDisplay>(this, R.layout.support_simple_spinner_dropdown_item, myImageList);
listview.setAdapter(arrayAdapters);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(context, DisplayActivity.class);
myIntent.putExtra("name", myImageList.get(position).getName());
myIntent.putExtra("imagePath", myImageList.get(position).getPath());
startActivityForResult(myIntent, 0);
}
});
}
public class ImageDisplay {
private String Name;
private int Path; // use String if you use a path, in here i'm storing image in drawable
public ImageDisplay(){}
public ImageDisplay(String name, int path){
this.Name = name;
this.Path = path;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getPath() {
return Path;
}
public void setPath(int path) {
this.Path = path;
}
}
Finally, here is your Display Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Intent myItent = getIntent();
String Name = myItent.getStringExtra("name"); //Do whatever you need with image title
int Path = myItent.getIntExtra("imagePath", 0); //pass this to ImageView
imageView.setImageResource(Path);
}
Answer2:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent in = new Intent(view.getContext(), aspirin.class);
in.putExtra("name", (String)adapterView.getItemAtPosition(position));
startActivityForResult(in, 0);
}
});
hope it helps. The trick is to package the info along with the intent object and process it on the target activity. Retrieve the string and show the image. <a href="https://developer.android.com/reference/android/app/Activity.html#StartingActivities" rel="nofollow">Starting Activities</a>