I have 2 columns meal and serving, and wanted to display it in listview.
Here’s what I’ve tried so far:
private ListView lv;
//private ArrayList<String> results = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.meal_result);
BreakFastLog info = new BreakFastLog(this);
info.open();
String data1 = info.getMealNameData();
String data2 = info.getServingData();
String[] inputData = data1.split( " " );
String[] inputData2 = data2.split( " " );
lv = (ListView) findViewById (R.id.lvBF);
ArrayList<HashMap<String, String[]>> mylistData =
new ArrayList<HashMap<String, String[]>>();
int[] columnIds = new int[] {
R.id.tvBreakfastMealContainer, R.id.tvBreakfastServingContainer};
HashMap<String, String[]> map = new HashMap<String, String[]>();
//initialize row data
map.put( "Meal", inputData );
map.put( "Serving", inputData2 );
mylistData.add( map );
//lv.setAdapter( new ArrayAdapter<String>( this,android.R.layout.simple_list_item_1, inputData ) );
SimpleAdapter arrayAdapter =
new SimpleAdapter(this, mylistData, R.layout.breakfast_listview_header,
new String[] {"Meal", "Serving"}, columnIds);
lv.setAdapter(arrayAdapter);
lv.setTextFilterEnabled( true );
info.close();
}
EDIT:
class MyCustomAdapter extends BaseAdapter
{
String[] data_meal;
String[] data_serving;
MyCustomAdapter()
{
data_meal = null;
data_serving = null;
}
MyCustomAdapter( String[] meal, String[] serving )
{
data_meal = meal;
data_serving = serving;
}
public int getCount()
{
return data_meal.length;
}
public String getItem( int position )
{
return null;
}
public long getItemId( int position )
{
return position;
}
public View getView( int position, View convertView, ViewGroup parent )
{
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate( R.layout.breakfast_listview_header, parent, false );
TextView txtMeal = ( TextView ) row.findViewById ( R.id.tvBreakfastMealContainer );
TextView txtServing = ( TextView ) row.findViewById ( R.id.tvBreakfastServingContainer );
txtMeal.setText( data_meal[ position ] );
txtServing.setText( data_serving[ position ] );
return (row);
}
}
EDIT: XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@drawable/ihealthfirst" >
<!--
<TextView
android:id="@+id/tvBreakfastDateContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_weight="1" /> -->
<TextView
android:id="@+id/tvBreakfastMealContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_weight="2" />
<TextView
android:id="@+id/tvBreakfastServingContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:gravity="center_horizontal"
android:layout_weight="1" />
</LinearLayout>
But they are just displaying only in one row? Help me figure out how to display those records individually. Thanks.
Yes, you are doing right but there is one more thing to do in your
getView()code. Make yourrowlayout into arowwith twocolumnsand fill both thecolumnssimultaneously in thegetView()function. That will make yourListViewa two column list.