44199

want ListView to open multiple different Images

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>

Recommend

  • onLeScan never gets called android
  • Problem in Creating a View Dynamically ( in Android ) [Error : couldn't save which view …]
  • RxJava,Retrofit Error :Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
  • onActivityResult() not called after startActivityForResult() with Intent.ACTION_GET_CONTENT
  • Store additional information during registration with Firebase in Android
  • Shuffling an Array in Android
  • How to add an item to a ListView with each Button click
  • Where are files stored on an Android emulator?
  • SQLiteOpenHelper: onCreate() method not called on physical device
  • When is locking on types a good idea?
  • android-How to enable cache or buffering when videoView have downloaded a video
  • Django Migrations fail during django initialization
  • OnItemSelectedListener for Spinner that was added programmatically does not trigger
  • Change border corlor of NSTableView
  • Animate a CAShapeLayer's subclass custom property based on the path property
  • Android: How to open the webview in a new screen
  • Listview - list item selection and initial setting (custom made items)
  • redirect user after update in class based view in django
  • Retrieving a double from a JTextArea while solving for X
  • I have two activity.I want to Send the data from second activity to previous activity.First activity
  • Unable to resolve Static method?
  • MRO with multiple inheritance in python
  • Replace Fragment with another on back button
  • Extending the Django 1.11 User Model
  • Activity overlaying. WindowManager.LayoutParams
  • Fully customized Python Help Usage
  • Swift Initialization Rule Confusion
  • How to upload specific List image using click on Upload button
  • Blackberry 6: how to detect a long click on track pad?
  • How to get listview position?
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • WPF version of .ScaleControl?
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • how to avoid repetitive constructor in children
  • Deleting and Updating values from a cusrsor adapter
  • Possible to stop flickering java tooltip in heavyweight mode?
  • align graphs with different xlab
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize