Java 带有HashMap的listview仅显示一个数据
我参考了androidhive的网站,用HashMap查看列表。我从数据库中获取数据没有问题,但是我的ListView只能显示表的第一条记录。我不知道问题是在数据库中,还是我没有在主活动中申请循环。如果是,我如何应用它。请帮帮我!谢谢 这是我用来获取数据的数据库Java 带有HashMap的listview仅显示一个数据,java,android,listview,android-listview,hashmap,Java,Android,Listview,Android Listview,Hashmap,我参考了androidhive的网站,用HashMap查看列表。我从数据库中获取数据没有问题,但是我的ListView只能显示表的第一条记录。我不知道问题是在数据库中,还是我没有在主活动中申请循环。如果是,我如何应用它。请帮帮我!谢谢 这是我用来获取数据的数据库 public ArrayList<Object> getFood(String cID) { ArrayList<Object> rowArray = new ArrayList<Object>
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 ArrayList getFood(字符串cID)
{
ArrayList rowArray=新的ArrayList();
光标;
尝试
{
String sql=“从食品表中选择食品名称、价格,其中categoryID=?”;
cursor=db.rawQuery(sql,新字符串[]{”“+cID});
cursor.moveToFirst();
如果(!cursor.isAfterLast())
{
做
{
添加(cursor.getString(0));
添加(cursor.getString(1));
}
while(cursor.moveToNext());
}
cursor.close();
}
捕获(SQLE异常)
{
Log.e(“列表错误”,例如toString());
e、 printStackTrace();
}
返回行数组;
}
这是我的适配器
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;
}
}
公共类List2Adapter扩展了BaseAdapter{
私人活动;
私有数组列表数据;
专用静态充气机=空;
公共列表2适配器(活动a、阵列列表d)
{
活动=a;
数据=d;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
@凌驾
public int getCount(){
返回data.size();
}
@凌驾
公共对象getItem(int位置){
返回位置;
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
视图vi=转换视图;
if(convertView==null)
{
vi=充气机充气(R.layout.row,空);
}
TextView foodname2=(TextView)vi.findViewById(R.id.foodname2);
TextView价格=(TextView)vi.findViewById(R.id.price);
HashMap food=新的HashMap();
食物=数据。获取(位置);
foodname2.setText(food.get(MainActivity.foodname2));
price.setText(food.get(MainActivity.price));
返回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);}
静态最终字符串FOODNAME2=“foodName”;
静态最终字符串PRICE=“PRICE”;
私有无效列表菜单(字符串CID)
{
LISTORDER=(ListView)findViewById(R.id.LISTORDER);
ArrayList数据=DB.getFood(CID);
ArrayList测试=新的ArrayList();
HashMap=newHashMap();
put(FOODNAME2,(String)data.get(0));
map.put(PRICE,(String)data.get(1));
测试。添加(map);
LISTMENU=(ListView)findViewById(R.id.LISTMENU);
List2Adapter=新的List2Adapter(MainActivity.this,test);
LISTMENU.setAdapter(适配器);}
像这样尝试查询
if (cursor.moveToFirst()) {
do {
rowArray.add(cursor.getString(0));
rowArray.add(cursor.getString(1));
} while (cursor.moveToNext());
}
这是因为您只添加了一项。更新您的
列表菜单
方法:
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);
}
private void列表菜单(字符串CID)
{
LISTORDER=(ListView)findViewById(R.id.LISTORDER);
ArrayList数据=DB.getFood(CID);
ArrayList测试=新的ArrayList();
对于(inti=0;iIt的工作,非常感谢。感谢您一次又一次地救了我=)