Reputation: 1269
I get rows from a db as an ArrayList. I want to use data that is in two of the columns to populate a list view.
What I have done so far only shows the ?row reference? (com.app.Poster@2349049)
ListView listView = (ListView) findViewById(R.id.poster_list_view);
MySQLiteHelper db = new MySQLiteHelper(this);
List<Poster> posters = db.getAllPosters();
ArrayAdapter<Poster> listAdapter = new ArrayAdapter<Poster>(this, android.R.layout.simple_list_item_1, posters);
listView.setAdapter(listAdapter);
I thought maybe I am suppose to loop the ArrayList and add each row to the adapter
for (Poster p : posters){
listAdapter.add(p.getPosterTitle());
}
Apparently the adapter only wants an object I think. Should I be customizing an adapter for this? I thought it would be a lot easier.
This is my Poster Class
public class Poster {
private int posterId;
private int categoryId;
private int eventId;
private String presenterFname;
private String presenterLname;
private String posterTitle;
private String posterSynopsis;
private String posterFilename;
private String posterRemoteLocation;
public Poster(int pid, int cid, int eid, String prf, String prl, String pt, String ps, String pfn, String fl){
posterId = pid;
categoryId = cid;
eventId = eid;
presenterFname = prf;
presenterLname = prl;
posterTitle = pt;
posterSynopsis = ps;
posterFilename = pfn;
posterRemoteLocation = fl;
}
public int getPosterID(){
return this.posterId;
}
public int getCatID(){
return this.categoryId;
}
public int getEventId(){
return this.eventId;
}
public String getPresenterFname(){
return this.presenterFname;
}
public String getPresenterLname(){
return this.presenterLname;
}
public String getPosterTitle(){
return this.posterTitle;
}
public String getPosterSynopsis(){
return this.posterSynopsis;
}
public String getPosterFilename(){
return this.posterFilename;
}
public String getPosterRemote(){
return this.posterRemoteLocation;
}
}
Upvotes: 0
Views: 502
Reputation: 1009
You can find the class for a custom adapter to vaguely fit your needs below. This will work in pretty much the same way that you are trying to use the general one in your code sample.
This method will give you more flexibility in the way your information is displayed.
You can set the custom layout in the getView method. As well as set the values in the view from the item number passed in.
public class PosterListAdapter extends BaseAdapter {
private final ArrayList<Poster> listItems;
private final LayoutInflater inflater;
public PosterListAdapter(ArrayList<Poster> listItems, LayoutInflater inflater) {
this.listItems = listItems;
this.inflater = inflater;
}
@Override
public int getCount() {
return this.listItems.size();
}
@Override
public Film getItem(int i) {
return this.listItems.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.poster_layout, viewGroup, false);
}
Poster item = this.listItems.get(i);
TextView posterTitle = ((TextView) view.findViewById(R.id.poster_layout_title));
posterTitle.setText(item.getTitle());
ImageView posterImage = ((ImageView) view.findViewById(R.id.poster_layout_image));
posterImage.setImageResource(R.drawable.apple);
return view;
}
}
Below you will find a simple layout that can be used with the above array adapter for a list view item.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<ImageView
android:id="@+id/poster_layout_image"
android:layout_width="50dp"
android:layout_height="75dp"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/poster_layout_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/poster_layout_image"
android:textColor="@color/text"
android:textSize="20sp" />
</RelativeLayout>
Upvotes: 1