0

My app needs to have multiple ListViews that when an option is clicked, will open a second activity that displays a zoomable picture. Everything in my code compiles, opens correct picture and zooms accordingly. However, the "names" on my list do not appear correctly and all say the same thing: "com.example.poweruser.fcemsandroid.obstetric". Anyone have a solution to correcting this? My code is below:

    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;
}

}

The second activity code is below:

public class imagedisplay extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_imagedisplay);

    ImageView imageView = (ImageView) findViewById(R.id.imageView3);
    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);

    TouchImageView img = new TouchImageView(this);
    img.setImageResource(Path);
    img.setMaxZoom(4f);
    setContentView(img);

}

}

beans217
  • 139
  • 2
  • 11
  • 1
    Please post your `ImageDisplay` class. – Mike M. Jan 21 '18 at 01:21
  • Its the second block of code – beans217 Jan 21 '18 at 01:22
  • 1
    `ArrayAdapter` <-- That one. – Mike M. Jan 21 '18 at 01:23
  • 1
    I found it in [your previous question](https://stackoverflow.com/q/48292223/2850651). You need to override the `toString()` method in `ImageDisplay` to return the text you want shown in the `ListView` items; looks like `Name`, in this case. `ArrayAdapter` will only display whatever `toString()` returns. – Mike M. Jan 21 '18 at 01:36
  • Oh, okay. So I need to create an "ImageDisplay" class that contains the lists? Will I need to create multiple classes for each different list? or Just one class with multiple different lists in it? – beans217 Jan 21 '18 at 01:45
  • 1
    You already have the `ImageDisplay` class. It's what you're adding instances of to `myImageList` in the code you posted above; e.g., `myImageList.add(new ImageDisplay(...));`. All you need to do is add a `toString()` override to that class - `@Override public String toString() { return Name; }`. – Mike M. Jan 21 '18 at 01:51
  • Sorry Mike, I updated my code with the public class thats contained in the 'obstetrics activity'. – beans217 Jan 21 '18 at 01:52
  • 1
    Right. There ya go. Just add that `toString()` method to `ImageDisplay`. – Mike M. Jan 21 '18 at 01:53
  • Sorry for the confusion, but thank you for your help! Problem Solved! – beans217 Jan 21 '18 at 02:10
  • 1
    No problem. Glad you got it working. Btw, you might consider renaming that `Activity` to prevent some of this confusion, especially if you're gonna have more questions about this particular project. Maybe change it to `ImageDisplayActivity`, which would be in keeping with Java naming conventions, and is clearly distinguishable from the `ImageDisplay` class we've been discussing. Just a suggestion. Cheers! – Mike M. Jan 21 '18 at 02:14

0 Answers0