为什么我的ListView正在添加xml中的所有项目?

时间:2012-05-27 03:41:26

标签: android android-listview

我找不到我的问题的答案,我有一个带有一些TextViews和一个ListView的主XML,问题是当我的List出现在我的Main xml上时,它添加了listview之外的Texviews,所以我得到了在每个项目中,一个TOTAL textview和列表都在所有XML布局中,而不是在底部,所以我不确定出错了什么

thx in advanced for your help

问候

这是我的文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
    android:id="@+id/TableLayout01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:stretchColumns="1" >
    <TableRow>
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Total Expenses $"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:textStyle="bold" />
        <TextView
            android:id="@+id/tvTotalGastos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:textStyle="bold" />
    </TableRow>
</TableLayout>
<TableLayout
    android:id="@+id/TableLayout02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/TableLayout01"
    android=""
    android:orientation="horizontal" >
    <TableRow>
        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_margin="4dip"
            android:layout_weight="1"
            android:padding="10dip"
            android:text="Date"
            android:textColor="@android:color/white" />
        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_margin="4dip"
            android:layout_weight="1"
            android:padding="10dip"
            android:text="Description"
            android:textColor="@android:color/white" />
        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_margin="4dip"
            android:layout_weight="1"
            android:padding="10dip"
            android:text="Amount"
            android:textColor="@android:color/white" />
    </TableRow>
</TableLayout>
<ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ListView>

My 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="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >


  <TextView
        android:id="@+id/tvDate"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_margin="4dip"
        android:layout_weight="1"
        android:padding="10dip"
        android:text="Date" />

    <TextView
        android:id="@+id/tvDescription"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_margin="4dip"
        android:layout_weight="1"
        android:padding="10dip"
        android:text="Description" />

    <TextView
        android:id="@+id/tvAmount"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_margin="4dip"
        android:layout_weight="1"
        android:padding="10dip"
        android:text="Amount" />


</LinearLayout>

和我的Java类

public class GastosListActivity extends ListActivity {

private DatabaseHandler dbh;
private List<Datos> datos;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.gastoslist);

    //ListView list2= (ListView) findViewById(R.id.listview);

    // new obj databasehandler
    dbh = new DatabaseHandler(this);
    datos = dbh.getAllData();

    ArrayList<Map<String, String>> list = buildData();
    String[] from = { "Date", "Description","Amount" };
    int[] to = { R.id.tvDate, R.id.tvDescription,R.id.tvAmount };

    SimpleAdapter adapter = new SimpleAdapter(this, list,
            R.layout.gastoslist, from, to);
    setListAdapter(adapter);

}
private ArrayList<Map<String, String>> buildData() {

    ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
    for (Datos dn : datos) {            
        list.add(putData(dn.get_fecha(), dn.get_concepto(),     dn.get_cantidad()));


    }
    return list;    

}

private HashMap<String, String> putData(String date, String description, String amount) {
    HashMap<String, String> item = new HashMap<String, String>();
    item.put("Date", date);
    item.put("Description", description);
    item.put("Amount", amount);
    return item;
}



}

1 个答案:

答案 0 :(得分:0)

问题解决,我使用Cursor而不是arraylist

更改代码
dbh = new DatabaseHandler(this);


        Cursor cursor = dbh.obtenerDatos();
        String[] from = new String[] { dbh.COLUMN_FECHA,
                dbh.COLUMN_CONCEPTO, dbh.COLUMN_CANTIDAD };
        int[] to = new int[] { R.id.tvDate, R.id.tvDescription,
                R.id.tvAmount };

        cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor,
                from, to);

        listContent.setAdapter(cursorAdapter);
相关问题