listview with arraylist,simple adapter in android

I try to show something into listview using arraylist and simple adapter. I tried something like below but in my result shows the last names of the arraylist. What is my wrong i cant understand.

final ListView listView = (ListView) findViewById(R.id.mylist); ArrayList> list_of_bookmarks = new ArrayList>(); HashMap b = new HashMap(); String[] from = < "php_key","c_key","android_key","hacking_key" >; String[] name_of_bookmarks = < "php","c","android","hacking" >; for(int i=0;i <4;i++) < b.put(from[i],name_of_bookmarks[i]); list_of_bookmarks.add(b); >>; int[] to = < R.id.txt1,R.id.txt1,R.id.txt1,R.id.txt1>; SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), list_of_bookmarks, R.layout.list_layout, from, to); listView.setAdapter(adapter); 

I just want to show "php","c","android","hacking" in a listview. And what should be more efficient way to do that.I am a beginner so you may suggest a better way which should i follow

asked Sep 5, 2013 at 6:02 user1960072 user1960072 Its display only last value ? Commented Sep 5, 2013 at 6:08 4 times the last value like "hacking,hacking,hacking,hacking" – user1960072 Commented Sep 5, 2013 at 6:10

5 Answers 5

My advice to you would be to create a separate class that extends the Adapter(or some subclass of it)

Here is a simple example of a String array adapter.

package ro.gebs.captoom.adapters; import android.content.Context; import android.view.View; import android.view.ViewGroup; import antistatic.spinnerwheel.adapters.AbstractWheelTextAdapter; import com.example.captoom.R; public class LanguagesAdapter extends AbstractWheelTextAdapter < // Countries names private String languages[]; public LanguagesAdapter(Context context) < super(context, R.layout.lang_item, NO_RESOURCE); languages = context.getResources().getStringArray(R.array.lang_array); setItemTextResource(R.id.language_txt); >@Override public View getItem(int index, View cachedView, ViewGroup parent) < View view = super.getItem(index, cachedView, parent); return view; >@Override public int getItemsCount() < return languages.length; >@Override protected CharSequence getItemText(int index) < return languages[index]; >> 

and the usage is simple just use the method .setAdapter();

Or another example which uses an arrayAdapter:

package apc.example; import java.util.ArrayList; import utils.BitmapManager; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; public class PersonAdapter extends ArrayAdapter  < Context context; int layoutResourceId; ArrayListdata = null; public PersonAdapter(Context context, int layoutResourceId, ArrayList data) < super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; >@Override public View getView(int position, View convertView, ViewGroup parent) < View row = convertView; ItemHolder holder = null; if (row == null) < LayoutInflater inflater = ((Activity) context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new ItemHolder(); holder.imgIcon = (ImageView) row.findViewById(R.id.icon); holder.txtName = (TextView) row.findViewById(R.id.title); holder.txtDescription = (TextView) row.findViewById(R.id.desc); row.setTag(holder); >else < holder = (ItemHolder) row.getTag(); >Person bean = data.get(position); holder.txtName.setText(bean.getName()); holder.txtDescription.setText(bean.getDescription()); Bitmap b = BitmapFactory.decodeResource(context.getResources(), R.drawable.user); BitmapManager.INSTANCE.setPlaceholder(b); BitmapManager.INSTANCE.loadBitmap(bean.getUrl(), holder.imgIcon, 80, 80); return row; > public static class ItemHolder < public ImageView imgIcon; TextView txtName; TextView txtDescription; >public void updateAdapter(ArrayList pers) < this.data = pers; >> 

This is an example of an adapter for a more complex class that has more fields rather than a simple string. But that can easily be modified to ArrayAdapter and then go from there.

Anyways i think it's always a best practice to write your custom adapters for listviews.