Last active
March 7, 2021 14:22
-
-
Save BetelGeuseee/d26d2f7478de2295fd448c0b51f5a071 to your computer and use it in GitHub Desktop.
ListView images on android using custom ArrayAdapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.listviewimage; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.content.Context; | |
import android.content.res.Resources; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.ImageView; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
String[] picTitles; | |
String[] picDescription; | |
ListView listView; | |
int[] images={R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//getting references of the resources in the application.(Example=strings are in values folder | |
//which is inside the res folder in app..so we need reference to the resource | |
Resources res = getResources(); | |
//now getting the array from the value folder which is inside resources | |
//and putting the String object pictites and picdescription | |
picTitles=res.getStringArray(R.array.titles);//returns the items in the array name=titles | |
picDescription=res.getStringArray(R.array.description);//returns the items in the array name=description | |
listView=(ListView)findViewById(R.id.listview); | |
ShirshakAdapter adapter = new ShirshakAdapter(this,picTitles,images,picDescription); | |
listView.setAdapter(adapter); | |
} | |
} | |
class ShirshakAdapter extends ArrayAdapter<String>{ | |
Context context; | |
int[] images; | |
String[] titleArray; | |
String[] desp; | |
ShirshakAdapter(Context c, String[] title,int[] imgs,String[] descriptions){ | |
super(c,R.layout.singlerow,R.id.textView2,title); | |
this.context=c; | |
this.images=imgs; | |
this.titleArray=title; | |
this.desp=descriptions; | |
} | |
//for every view this getView method is called... | |
public View getView(int position, View convertView, ViewGroup parent){ | |
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
View row=layoutInflater.inflate(R.layout.singlerow,parent,false); | |
ImageView imageview=(ImageView)row.findViewById(R.id.imageView); | |
TextView pictitles =(TextView) row.findViewById(R.id.textView2); | |
TextView picDescription=(TextView) row.findViewById(R.id.textView3); | |
//row contents the reference of the relative layout | |
imageview.setImageResource(images[position]); | |
pictitles.setText(titleArray[position]); | |
picDescription.setText(desp[position]); | |
return row; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment