My ListView is still slow after using ViewHolder - android

I'm using listview with customized list item layout.Also i'm using ViewHolder on my list view but the performance of list view is still very slow.How to solve this?
Here is my Activity.java
public class FilemanagerActivity extends Activity {
private ListView list;
private TextView status;
private String[] list_items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filemanager);
//Initialization
list=(ListView)findViewById(R.id.list);
///Function to List a dirs from that dir
listFiles("/");
list.setItemsCanFocus(false);
CustomAdapter adapter=new CustomAdapter(this, R.layout.list_filemanager, list_items);
list.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.filemanager, menu);
return true;
}
// CustomAdapter
private class CustomAdapter extends ArrayAdapter<String> {
String[] values;
int layout,img_dir;
LayoutInflater lf;
public CustomAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
values = objects;
layout = resource;
lf= getLayoutInflater();
img_dir=R.drawable.device_mobile;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewholder;
if(convertView==null)
{
convertView = lf.inflate(layout, parent, false);
viewholder=new ViewHolder();
viewholder.checkbox=(CheckBox)convertView.findViewById(R.id.list_checkbox);
viewholder.txt = (TextView) convertView.findViewById(R.id.list_txt);
//viewholder.img = (ImageView) convertView.findViewById(R.id.list_img);
convertView.setTag(viewholder);
}
else
{
viewholder=(ViewHolder) convertView.getTag();
}
viewholder.txt.setText(values[position]);
//viewholder.img.setImageResource(img_dir);
viewholder.checkbox.setChecked(true);
return convertView;
}
}
//View Holder
public class ViewHolder
{
public TextView txt;
public ImageView img;
public CheckBox checkbox;
}
}
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FilemanagerActivity"
android:orientation="vertical">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list"
android:focusable="false"
></ListView>
</LinearLayout>
List item Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textAppearance="#android:style/TextAppearance.Large"
android:id="#+id/list_txt"
android:layout_weight="0.2"
/>
<CheckBox
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/list_checkbox"
android:layout_weight="0.8"
/>
</LinearLayout>
Is there any performance error in my code?

Related

Always getting a NullPointerException on adding a spinner to my fragment

I'm trying to create a contact list with a spinner where you can choose different roles for contacts.
I'm always getting a NullPointerException on adding the spinner to the layout of my contact list (list_item.xml) and I just can't find my mistake(s).
kontakte_fragment - XML File
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:background="#ffffff">
<ListView
android:layout_width="fill_parent"
android:layout_height="657dp"
android:id="#+id/listViewKontakte" />
</LinearLayout>
</ScrollView>
The Layout for my List (list_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff">
<com.github.siyamed.shapeimageview.CircularImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/screen1"
app:siBorderWidth="2dp"
app:siBorderColor="#color/colorPrimary"
android:layout_gravity="center"
android:id="#+id/kontaktListePic"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/titelKontaktListe"
android:id="#+id/textview1kontakteTitel"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="15dp"
android:layout_alignTop="#+id/kontaktListePic"
android:layout_toRightOf="#+id/kontaktListePic"
android:layout_toEndOf="#+id/kontaktListePic"
android:layout_marginBottom="0dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/unterTitelKontaktListe"
android:id="#+id/untertitelKontaktListe"
android:textSize="12sp"
android:layout_below="#+id/textview1kontakteTitel"
android:layout_alignLeft="#+id/textview1kontakteTitel"
android:layout_alignStart="#+id/textview1kontakteTitel" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnerKontakte"
android:layout_alignBottom="#+id/untertitelKontaktListe"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="250dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="-5dp" />
</RelativeLayout>
KontakteFragment - ActivityClass
public class KontakteFragment extends Fragment {
private ArrayList<String> data = new ArrayList<String>();
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
public static KontakteFragment newInstance() {
KontakteFragment fragment = new KontakteFragment();
return fragment;
}
public KontakteFragment(){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.kontakte_fragment, container, false);
ListView lv = (ListView) rootView.findViewById(R.id.listViewKontakte);
generateListContent();
lv.setAdapter(new MyListAdapter(getContext(), R.layout.list_item, data));
spinner = (Spinner) rootView.findViewById(R.id.spinnerKontakte);
adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.profile_kontakte, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
return rootView;
}
private void generateListContent() {
for(int i = 0; i < 55; i++){
data.add("This is row number" + i);
}
}
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.kontaktListePic);
viewHolder.titel = (TextView) convertView.findViewById(R.id.textview1kontakteTitel);
viewHolder.unterTitel = (TextView) convertView.findViewById(R.id.untertitelKontaktListe);
convertView.setTag(viewHolder);
}
else{
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.titel.setText(getItem(position));
mainViewHolder.unterTitel.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder {
ImageView thumbnail;
TextView titel;
TextView unterTitel;
}
} // This is the end of our KontakteFragment Class
You're looking for the Spinner in the Fragment's layout, but it's in the LitsView's elements layout.
You can get a proper reference to your Spinner inside your Adapter's getView method, like so :
Spinner yourSpinner = (Spinner) convertView.findViewById(R.id.spinnerKontakte);
Where did you added Spinner in fragment? you are doing this in wrong manner
public class KontakteFragment extends Fragment {
private ArrayList<String> data = new ArrayList<String>();
ArrayAdapter<CharSequence> adapter;
public static KontakteFragment newInstance() {
KontakteFragment fragment = new KontakteFragment();
return fragment;
}
public KontakteFragment(){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.kontakte_fragment, container, false);
ListView lv = (ListView) rootView.findViewById(R.id.listViewKontakte);
generateListContent();
lv.setAdapter(new MyListAdapter(getContext(), R.layout.list_item, data));
adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.profile_kontakte, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
return rootView;
}
private void generateListContent() {
for(int i = 0; i < 55; i++){
data.add("This is row number" + i);
}
}
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.kontaktListePic);
viewHolder.titel = (TextView) convertView.findViewById(R.id.textview1kontakteTitel);
viewHolder.unterTitel = (TextView) convertView.findViewById(R.id.untertitelKontaktListe);
convertView.setTag(viewHolder);
viewHolder.spinner = (Spinner)convertView.findViewById(R.id.spinnerKontakte);
}
else{
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.titel.setText(getItem(position));
mainViewHolder.unterTitel.setText(getItem(position));
// here you have to set data to spinner
}
return convertView;
}
}
public class ViewHolder {
ImageView thumbnail;
TextView titel;
TextView unterTitel;
Spinner spinner
}
}

List image is empty

i'm having some trouble with my code app: i'm trying to display in an activity a list of image taken from an array but when i try to execute the app i've an empty list without error in debug. Can anyone help me? Here is my activity:
String [] vett;
ListView list;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_techpuno);
list=(ListView)findViewById(android.R.id.list);
Intent intent = getIntent();
vett=intent.getExtras().getStringArray("nomeImmagine");
for(int i = 0;i<vett.length;i++){
Log.d("test", vett[i]);}
//list.setListAdapter(new AdapterTecniche(this, vett));
list.setAdapter(new AdapterTecniche(this, R.layout.activity_techp,vett));
}
And here it is the adapter:
public class AdapterTecniche extends ArrayAdapter<String> {
private final int resource;
private final String[] img;
public ImageLoader imageLoader;
LayoutInflater inflater;
public AdapterTecniche(Context context,int resource, String[]img) {
// your code
super(context, R.layout.activity_techpuno);
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.resource=resource;
this.img=img;
imageLoader=new ImageLoader(getContext().getApplicationContext());
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
View rowView = convertView;
if(rowView == null) {
Log.d("errore","Immagini non presenti");
rowView = inflater.inflate(R.layout.immagini, parent, false);
viewHolder=new ViewHolder();
viewHolder.imageView=(ImageView) rowView.findViewById(R.id.list_image);
rowView.setTag(viewHolder);
}
else
{
viewHolder=(ViewHolder)rowView.getTag();
}
if(img==null){
Log.d("errore","Immagini non presenti");
}else{
int resourceId = context.getResources().getIdentifier(img[position],"drawable", context.getPackageName());
for(int i=0;i<img.length;i++){
Log.d("err", img[i]);
}
imageLoader.DisplayImage(resourceId, viewHolder.imageView);
}
return rowView;
}
static class ViewHolder {
ImageView imageView;
}
}
Immagini.xml (each image layout)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/list_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_gravity="center_horizontal" />
</LinearLayout>
And Activity layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list" />
</LinearLayout>
setContentView(R.layout.activity_techpuno);
should be before
list=(ListView)findViewById(R.id.listView);
Because Activity firstly need to have binded/inflated layout, after that you can "find" your views from it.
Edit
Use ArrayAdapter(Context context, int resource, T[] objects) constructor to give objects to it.
Or override getCount() because your adapter have no idea how many items he got.

Can't find TextView in recursive custom Listview adapter

I created this class called GeoArea, which is suppose to store "Geographical Area" that have children Geographical Areas, this is fairly strait foward:
public class GeoArea {
public String id;
public String name;
public List<GeoArea> subGeoAreas;
public GeoArea parentGeoArea;
public GeoArea(String id) {
this.id = id;
name = id;
subGeoAreas = new LinkedList<GeoArea>();
}
#Override
public String toString() {
return name;
}
}
I have created the following Layout to render it on Android, the idea here is to for each GeoArea to recursively render it self and then it's children GeoArea in a listView:
//layout_geo_area.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtGeoAreaName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="Geo Area Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/listViewChildGeoAreas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtGeoAreaName"
android:gravity="left" >
</ListView>
</RelativeLayout>
This is my adapter I created for GeoArea to be displayed in a listView:
public class AdapterGeoArea extends ArrayAdapter<GeoArea>{
private ArrayList<GeoArea> _myGeoArea;
private Context _myContext;
LayoutInflater _inflater;
public AdapterGeoArea(Context context, ArrayList<GeoArea> myGeoArea) {
super(context, 0, myGeoArea);
_myGeoArea = myGeoArea;
_inflater = LayoutInflater.from(context);
_myContext = context;
}
public int getCount() {
return _myGeoArea.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
GeoAreaLayoutHolder holder;
if (convertView == null) {
convertView = _inflater.inflate(R.layout.layout_geo_area,parent,false);
holder = new GeoAreaLayoutHolder();
holder.txtGeoAreaName = (TextView)convertView.findViewById(R.id.txtGeoAreaName);
holder.txtGeoAreaName.setTag(convertView);
holder.listViewChildGeoAreas = (ListView)convertView.findViewById(R.id.listViewChildGeoAreas);
holder.listViewChildGeoAreas.setTag(convertView);
} else {
holder = (GeoAreaLayoutHolder) convertView.getTag();
}
GeoArea curGeoArea = _myGeoArea.get(position);
holder.txtGeoAreaName.setText(curGeoArea.name);
if(curGeoArea.subGeoAreas.size()>0){
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
AdapterGeoArea adapter = new AdapterGeoArea(_myContext, testList);
for(GeoArea childGeoArea:curGeoArea.subGeoAreas){
testList.add(childGeoArea);
}
holder.listViewChildGeoAreas.setAdapter(adapter);
}
return convertView;
}
static class GeoAreaLayoutHolder {
public TextView txtGeoAreaName;
public ListView listViewChildGeoAreas;
}
}
And here is my Activity that I am using to set it all up:
public class ActivityGeoAreas extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_geo_area);
GeoArea.searchTerm = "Bar & Grill";
GeoArea torontoArea = new GeoArea("cityOfToronto");
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
testList.add(torontoArea);
AdapterGeoArea adapter = new AdapterGeoArea(this, testList);
ListView lv = (ListView) findViewById(R.id.listViewChildGeoAreas);
lv.setAdapter(adapter);
}
}
When I try to run it, I get the error nullPointerException on the line:
holder.txtGeoAreaName.setText(curGeoArea.name);
What am I doing wrong?
You may want to check ExpandableListView may suit your needs better
http://developer.android.com/reference/android/widget/ExpandableListView.html
An example # http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
Continuing from my previous answer to your question ( i though that solved your problem)
To display just name in your listview
list_row.xml // this is the layout with textview to be inflated in getView.
Each row will have textview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textGeoArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Choose Area"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
layout_geo_area.xml // with only listview no textview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listViewChildGeoAreas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left" >
</ListView>
</RelativeLayout>
Now your adapter class
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row,parent,false);
// inflate list_row.xml with textview
holder = new ViewHolder();
holder.tv = (TextView)convertView.findViewById(R.id.textGeoArea);
holder.setTag(convertView);
}
else {
holder = (ViewHolder) convertView.getTag();
}
GeoArea curGeoArea = _myGeoArea.get(position);
holder.tv.setText(curGeoArea.name);
return convertView;
}
static class ViewHolder // use a view holder for smooth scrolling an performance
{
TextView tv;
}
You have a custom adapter.
public class AdapterGeoArea extends ArrayAdapter<GeoArea>{
Now you set the adapter to listview like below
AdapterGeoArea adapter = new AdapterGeoArea(this, testList);
ListView lv = (ListView) findViewById(R.id.listViewChildGeoAreas);
lv.setAdapter(adapter);
So why do you need the below. remove these
if(curGeoArea.subGeoAreas.size()>0){
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
AdapterGeoArea adapter = new AdapterGeoArea(_myContext, testList);
for(GeoArea childGeoArea:curGeoArea.subGeoAreas){
testList.add(childGeoArea);
}
holder.listViewChildGeoAreas.setAdapter(adapter);

android gridview slow scrolling

I have an android activity with a gridview, each cell contains a textview with a single character (so there are around 60-70 characters/cells on the screen at a time). The scrolling of the gridview is unacceptably slow and unsmooth. I tried replacing the gridview with a listview, and the scrolling of the listview is much faster. How can i speed this up?
The activity layout is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="56dp"
android:numColumns="auto_fit" >
</GridView>
</LinearLayout>
And inside each cell is this layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="40sp" />
</LinearLayout>
And the code for the activity is:
public class TestGridActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_activity);
GridView gridView = (GridView) findViewById(R.id.gridView1);
ArrayList<Map<String, String>> list = getData();
SimpleAdapter arrayAdapter = new SimpleAdapter(this, list,
R.layout.grid_layout, new String[] { "literal"},
new int[] { R.id.textView1});
gridView.setAdapter(arrayAdapter);
}
}
edit: pks asking to post adapter code, the above code I have used a generic simpleAdapter, but i have tried a custom view, which didn't help.
public class GridAdapter extends BaseAdapter {
private Context context;
private ArrayList<Map<String, String>> list;
private LayoutInflater inflater;
public static class ViewHolder {
TextView textView1;
int position;
}
public GridAdapter(Context c, ArrayList<Map<String, String>> l) {
context = c;
list = l;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return list.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = inflater.inflate(R.layout.grid_layout, null);
holder = new ViewHolder();
holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
holder.position = position;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView1.setText(list.get(position).get("character"));
return convertView;
}
}
I've also tried creating all the views in advance, and that didn't help scrolling speed.

Android List with Image - How to implement

I'm looking for a clear way to establish the XML resource for a list of strings with an image attached to it.
Should I use attributes? What is the best practice?
list_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/row_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView android:id="#+id/row_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
In onCreate()
setContentView(R.layout.list_layout);
ListView lv = (ListView) findViewById(R.id.list);
List<ListRow> list = new ArrayList<ListRow>();
for ( loop through your data ) {
list.add(new ListRow("text", R.drawable.image));
}
YourAdapter adapter = new YourAdapter(this, list);
lv.setAdapter(adapter);
The classes
class ListRow {
private String text;
private int resource;
public ListRow(String text, int resource) {
super();
this.text = text;
this.resource = resource;
}
public int getText() {
return text;
}
public int getResource() {
return resource;
}
}
class YourAdapter extends BaseAdapter implements OnClickListener {
private Context context;
private List<ListRow> theList;
public YourAdapter (Context context, List<ListRow> theList) {
this.context = context;
this.theList = theList;
}
public View getView(int position, View convertView, ViewGroup viewGroup) {
ListRow row = theList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.row_text);
tv.setText(row.getText());
ImageView iv = (ImageView) convertView.findViewById(R.id.row_image);
iv.setBackgroundResource(row.getResource());
return convertView;
}
#Override
public void onClick(DialogInterface dialog, int which) {
}
}

Categories

Resources