Reputation: 107
So I have a fragment view that looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivityFragment"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
//Various elements
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/notes_list_view"
android:drawSelectorOnTop="false">
</ListView>
</LinearLayout>
So the listview is at the bottom of a set of elements. I have been trying to figure out how to fill items in this listview, but no matter what I try I can't get anything to appear in the array, it's empty every time. Ideally I'd like to fill a item view that looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal">
<FrameLayout
android:layout_width="60dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/list_item_icon"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="7"
android:orientation="vertical">
<TextView
android:id="@+id/list_item_1_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:textSize="20sp"/>
<TextView
android:id="@+id/list_item_2_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:textAppearanceSmall"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/list_item_3_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:textAppearanceLarge"/>
<TextView
android:id="@+id/list_item_4_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif-condensed"
android:textAppearance="?android:textAppearanceSmall"/>
</LinearLayout>
</LinearLayout>
Can someone help me with writing an ArrayAdapter or CursorAdapter to just make something appear, I can tweak it later, which I plan on doing anyway since the data will ultimately come from a database.
Alternatively, can someone find what is causing nothing to display, I am not experienced in UI design, and I am worried I simply have the space for the listview obstructed.
For reference, I am using a min SDK of 11.
Upvotes: 0
Views: 1156
Reputation: 12953
Declare a Data transfer Class to store the Values Required to populate
public class Item
{
public string text1,text2,text3,text4;
public byte[] image;
}
Set values from Db into ArrayList
of Item
,then in Adpater Using DataDto
fill the View as in
public class ListAdapter extends ArrayAdapter<Item> {
ArrayList<Item> p;
public ListAdapter(Context ArrayList<Item> items) {
super(context, R.layout.list_item, items);
p=items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_item, null);
}
if (p != null) {
TextView tt1 = (TextView) v.findViewById(R.id.list_item_1_textview);
TextView tt2 = (TextView) v.findViewById(R.id.list_item_2_textview);
TextView tt3 = (TextView) v.findViewById(R.id.list_item_3_textview);
TextView tt4 = (TextView) v.findViewById(R.id.list_item_4_textview);
ImageView img=(ImageView)
v.findViewById(R.id.list_item_icon);
if (tt1 != null) {
tt1.setText(p.get(position).text1);
}
if (tt2 != null) {
tt2.setText(p.get(position).text4);
}
if (tt3 != null) {
tt3.setText(p.get(position).text3);
}
if (tt4 != null) {
tt1.setText(p.get(position).text4);
}
if(img!=null)
{
byte[] bitmapdata =p.get(position).image;
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata , 0, bitmapdata .length);
img.setimageBitmap(bitmap);
}
}
return v;
}
}
in your Activity ,fill ArrayList and pass it to Adapter as in
ListView lv=(ListView)findViewById(R.id.notes_list_view);
ArrayList<Item> items; populate this list from DB
ListAdpater data=new ListAdapter(this,items);
lv.setAdapter(data);
Upvotes: 1