我已经从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);}
答案 0 :(得分:2)
这是因为您只添加了一个项目。更新您的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);
}
答案 1 :(得分:0)
使用您的查询
尝试这样做if (cursor.moveToFirst()) {
do {
rowArray.add(cursor.getString(0));
rowArray.add(cursor.getString(1));
} while (cursor.moveToNext());
}