【问题标题】:listview with HashMap show only one data带有 HashMap 的列表视图只显示一个数据
【发布时间】:2013-12-23 11:07:20
【问题描述】:

我曾参考 androidhive 的 this 网站来使用 HashMap 进行我的 ListView。我从数据库中获取数据没有问题,但我的 ListView 只能显示表的第一条记录。我不知道问题是出在数据库中还是我没有在主要活动中申请循环。如果是,我该如何应用它。请帮我!谢谢!

这是我获取数据的数据库

public ArrayList<Object> getFood(String cID)
{
    ArrayList<Object> rowArray = new ArrayList<Object>();
    Cursor cursor;

    try
    {
        String sql = "SELECT foodName, price FROM food_table WHERE categoryID = ?";
        cursor = db.rawQuery (sql, new String[]{""+cID});

        cursor.moveToFirst();

        if (!cursor.isAfterLast())
        {
            do
            {
                rowArray.add(cursor.getString(0));
                rowArray.add(cursor.getString(1));
            }
            while (cursor.moveToNext());
        }
        cursor.close();
    }

    catch (SQLException e)
    {
        Log.e("LIST ERROR", e.toString());
        e.printStackTrace();
    }
    return rowArray;
}

这是我的适配器

public class List2Adapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;

public List2Adapter(Activity a, ArrayList<HashMap<String, String>> d)
{
    activity = a;
    data = d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View vi = convertView;
    if (convertView == null)
    {
        vi = inflater.inflate(R.layout.row, null);
    }

    TextView foodname2 = (TextView)vi.findViewById(R.id.foodName2);
    TextView price = (TextView)vi.findViewById(R.id.price);

    HashMap<String,String> food = new HashMap<String, String>();
    food = data.get(position);

    foodname2.setText(food.get(MainActivity.FOODNAME2));
    price.setText(food.get(MainActivity.PRICE));

    return vi;
}
}

这是我的主要活动

static final String FOODNAME2 = "foodName";
static final String PRICE = "price";
private void listMenu(String CID)
{
    LISTORDER = (ListView) findViewById(R.id.listOrder);

    ArrayList<Object> data = DB.getFood(CID);

    ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>();

    HashMap<String, String> map = new HashMap<String, String>();

    map.put(FOODNAME2, (String) data.get(0));
    map.put(PRICE, (String) data.get(1));

    test.add(map);

    LISTMENU = (ListView) findViewById(R.id.listMenu);

    List2Adapter adapter = new List2Adapter(MainActivity.this, test);
    LISTMENU.setAdapter(adapter);}

【问题讨论】:

    标签: java android listview android-listview hashmap


    【解决方案1】:

    这是因为您只添加了一项。更新您的 listMenu 方法:

    private void listMenu(String CID)
    {
        LISTORDER = (ListView) findViewById(R.id.listOrder);
    
        ArrayList<Object> data = DB.getFood(CID);
        ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>();
    
        for (int i=0;i<data.size();i=i+2) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(FOODNAME2, (String) data.get(i));
            map.put(PRICE, (String) data.get(i+1));
            test.add(map);
        }
    
        LISTMENU = (ListView) findViewById(R.id.listMenu);
    
        List2Adapter adapter = new List2Adapter(MainActivity.this, test);
        LISTMENU.setAdapter(adapter);
    }
    

    【讨论】:

    • 成功了,非常感谢。谢谢你一次又一次地救了我=)
    【解决方案2】:

    用你的查询试试这个

    if (cursor.moveToFirst()) {
        do {
            rowArray.add(cursor.getString(0));
            rowArray.add(cursor.getString(1));
    
           } while (cursor.moveToNext());
     }
    

    【讨论】:

      猜你喜欢
      • 2015-02-22
      • 2012-08-19
      • 2019-07-10
      • 2018-12-07
      • 2014-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多