如何从列表中获取值并在列表视图android中显示它

时间:2016-12-24 19:58:56

标签: java android sqlite listview

我是android新手。我有一个使用listview的应用程序,我想从数据库中获取数据并在listview中显示它。 我已获取值并将其放入列表中,但从列表中获取时会出错。

AllOrderDbManager.java

 public static List<DetailItem> getDetails(String ordId) {  //ordId is the row id
    List<DetailItem> data = new ArrayList<>();
    String query = AllOrderDbHelper._ID + "=?";
    Cursor cursor = database.query(AllOrderDbHelper.TABLE_NAME2, null, query, new String[]{ordId}, null, null, null);

    if (cursor != null)
        if (cursor.moveToFirst()) {
            do {
                String food = cursor.getString(cursor.getColumnIndex(FOOD_NAME));
                String quantity = cursor.getString(cursor.getColumnIndex(QUANTITY));

                DetailItem detailItem = new DetailItem();
                detailItem.setFood(food);
                detailItem.setQuantity(quantity);
                data.add(detailItem);
            } while (cursor.moveToNext());
        }
    cursor.close();
    return data;
}

这是我的DbManager。这里我按行id获取值并将其设置为列表

DetailItem.java

public class DetailItem {

String food = "";
String quantity = "";

public String getFood() {
    return food;
}

public void setFood(String food) {
    this.food = food;
}

public String getQuantity() {
    return quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}
@Override
public String toString() {
    return food;

 }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
private AllOrderDbManager allOrderDbManager;
private ListView listView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    allOrderDbManager = new AllOrderDbManager(this);
    allOrderDbManager.open();

    listView = (ListView) findViewById(R.id.lvDetails);



    Intent intent = getIntent(); 
    String ordId = intent.getStringExtra("ordId");

   List<DetailItem> values = AllOrderDbManager.getDetails(ordId);
    for (DetailItem detailItem: values) {
        String food_name = detailItem.getFood();
        String quantity = detailItem.getQuantity();


    }

     String[] from = new String[] {food_name, quantity};
        int[] to = new int[] {R.id.tvItemsName, R.id.tvQuantity};

        SimpleAdapter adapter = new SimpleAdapter(this,values,                                                        

        R.layout.all_order_details_list, from, to);

        listView.setAdapter(adapter);
 }
}

在这里,我无法从列表中获取值并将其设置为listview。

activity_main.xml中

   RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >

     <ListView
      android:id="@+id/lvDetails"
       android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true" >
     </ListView>

all_order_details_list

 <TextView
    android:id="@+id/tvItemsName"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:textStyle="bold"
    android:textColor="@android:color/black"
    android:textSize="15sp" />
<TextView
    android:id="@+id/tvQuantity"
    android:layout_width="0dip"
    android:textStyle="bold"
    android:gravity="center"
    android:textSize="15sp"
    android:textColor="@android:color/black"
    android:layout_height="match_parent"
    android:layout_weight="1"/>

请帮忙。

1 个答案:

答案 0 :(得分:0)

我已经使用HashMap解决了它。

public class MainActivity extends AppCompatActivity {
private AllOrderDbManager allOrderDbManager;
private ListView listView;
static final String FOODNAME = "foodName";
static final String QTY = "quantity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    allOrderDbManager = new AllOrderDbManager(this);
    allOrderDbManager.open();

    listView = (ListView) findViewById(R.id.lvDetails);

    Intent intent = getIntent(); 
    String ordId = intent.getStringExtra("ordId");

    String[] from = new String[] {"foodName","quantity"};
    int[] to = new int[] {R.id.tvItemsName, R.id.tvQuantity};

    List<DetailItem> values = AllOrderDbManager.getDetails(ordId);
    ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>();

    for (DetailItem detailItem: values) {
        HashMap<String, String> map = new HashMap<String, String>();
           map.put(FOODNAME,(String)detailItem.getFood());
           map.put(QTY,(String)detailItem.getQuantity());
            test.add(map);
    }

    SimpleAdapter adapter = new SimpleAdapter(this, test, R.layout.all_order_details_list, from, to);
    listView.setAdapter(adapter);

}

Thanku