我有 2 列餐和服务,并想在列表视图中显示它。
这是我到目前为止所尝试的:
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();
}
编辑:
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);
}
}
编辑: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>
但他们只是显示在一排?帮助我弄清楚如何单独显示这些记录。谢谢。