Android HashMap只显示最后一项
所以我从数据库中获取数据并将其放入HashMap中,然后尝试在listview上显示它。但问题是,它只显示最后一项作为所有文本视图的文本,我真的找不到哪里是我的错误。所以如果有人能帮助我,我会非常高兴。以下是我正在使用的代码:Android HashMap只显示最后一项,android,listview,hashmap,Android,Listview,Hashmap,所以我从数据库中获取数据并将其放入HashMap中,然后尝试在listview上显示它。但问题是,它只显示最后一项作为所有文本视图的文本,我真的找不到哪里是我的错误。所以如果有人能帮助我,我会非常高兴。以下是我正在使用的代码: public class Recommended extends TabGroupActivity { private final String IMAGE = ""; private final String TITLE = ""; privat
public class Recommended extends TabGroupActivity {
private final String IMAGE = "";
private final String TITLE = "";
private final String CARDS_COUNT = "";
private ArrayList <HashMap<String, Object>> items;
Bitmap b;
String cardsCount;
String text;
int collId;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.recommended_layout);
ListView lv1 = (ListView)findViewById(R.id.listViewRecommended);
UserDatabaseHelper userDbHelper = new UserDatabaseHelper(this, null, 1);
userDbHelper.initialize(this);
items = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> hm;
String sql = "SELECT objectId, title, cardsCount FROM collections WHERE isRecommended = 1";
Cursor cursor = userDbHelper.executeSQLQuery(sql);
if(cursor.getCount()==0){
Log.i("Cursor Null","CURSOR IS NULL");
} else if(cursor.getCount()>0){
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
text = cursor.getString(cursor.getColumnIndex("title"));
Log.i("Show Title","Show Title : "+text);
cardsCount = cursor.getString(cursor.getColumnIndex("cardsCount"));
collId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("objectId")));
Log.i("CollId","Collection ID : "+collId);
b = BitmapFactory.decodeFile("/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg", null);
hm = new HashMap<String, Object>();
hm.put(IMAGE, b);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
}
Log.i("items", "items: " + items);
final SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.main_listview,
new String[]{TITLE, CARDS_COUNT, IMAGE}, new int[]{ R.id.main_name, R.id.main_info, R.id.main_img});
lv1.setAdapter(adapter);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent previewMessage = new Intent(Recommended.this, MyCollectionId.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
previewMessage.putExtra("collection_id", collId);
parentActivity.startChildActivity("MyCollectionId", previewMessage);
}
});
}
public类建议扩展TabGroupActivity{
私有最终字符串图像=”;
私有最终字符串标题=”;
私人最终字符串卡_COUNT=“”;
私有ArrayList项;
位图b;
线卡;
字符串文本;
int-collId;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.推荐的_布局);
ListView lv1=(ListView)findViewById(建议使用R.id.ListViews);
UserDatabaseHelper userDbHelper=newuserdatabasehelper(this,null,1);
初始化(这个);
items=newarraylist();
HashMap-hm;
String sql=“从isRecommended=1的集合中选择objectId、title、cardsCount”;
Cursor Cursor=userDbHelper.executeSQLQuery(sql);
if(cursor.getCount()==0){
Log.i(“游标为空”、“游标为空”);
}else if(cursor.getCount()>0){
for(cursor.move(0);cursor.moveToNext();cursor.isAfterLast()){
text=cursor.getString(cursor.getColumnIndex(“标题”);
Log.i(“显示标题”,“显示标题:+文本”);
cardsCount=cursor.getString(cursor.getColumnIndex(“cardsCount”);
collId=Integer.parseInt(cursor.getString(cursor.getColumnIndex(“objectId”));
Log.i(“CollId”,“集合ID:+CollId”);
b=位图工厂.decodeFile(“/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg”,空);
hm=新的HashMap();
hm.put(图像,b);
hm.put(标题、文本);
hm.put(卡数,卡数+Stampii);
添加项目(hm);
}
日志i(“项目”,“项目:+项目”);
最终SimpleAdapter适配器=新SimpleAdapter(此,项,R.layout.main\u列表视图,
新字符串[]{TITLE,CARDS_COUNT,IMAGE},新int[]{R.id.main_name,R.id.main_info,R.id.main_img});
lv1.设置适配器(适配器);
lv1.setOnItemClickListener(新的OnItemClickListener(){
公共视图单击(适配器视图a、视图v、内部位置、长id)
{
Intent previewMessage=新的Intent(建议使用.this,MyCollectionId.class);
TabGroupActivity parentActivity=(TabGroupActivity)getParent();
previewMessage.putExtra(“集合id”,collId);
parentActivity.startChildActivity(“MyCollectionId”,previewMessage);
}
});
}
问题在于您在for循环内部初始化了哈希映射,所以每次循环迭代时它都会被重新初始化
所以把hm=newhashmap();
在循环之外,也可以尝试使用这种方法在使用游标时从DB获取值
请尝试以下代码将值从DB添加到哈希映射:
hm = new HashMap<String, Object>();
if (cursor.moveToFirst()) {
do {
text = cursor.getString(cursor.getColumnIndex("title"));
Log.i("Show Title","Show Title : "+text);
cardsCount = cursor.getString(cursor.getColumnIndex("cardsCount"));
collId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("objectId")));
Log.i("CollId","Collection ID : "+collId);
b = BitmapFactory.decodeFile("/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg", null);
hm.put(IMAGE, b);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
} while (cursor.moveToNext());
}
hm=newhashmap();
if(cursor.moveToFirst()){
做{
text=cursor.getString(cursor.getColumnIndex(“标题”);
Log.i(“显示标题”,“显示标题:+文本”);
cardsCount=cursor.getString(cursor.getColumnIndex(“cardsCount”);
collId=Integer.parseInt(cursor.getString(cursor.getColumnIndex(“objectId”));
Log.i(“CollId”,“集合ID:+CollId”);
b=位图工厂.decodeFile(“/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg”,空);
hm.put(图像,b);
hm.put(标题、文本);
hm.put(卡数,卡数+Stampii);
添加项目(hm);
}while(cursor.moveToNext());
}
问题在于您在for循环内部初始化了哈希映射,所以每次循环迭代时它都会被重新初始化
所以把hm=newhashmap();
在循环之外,也可以尝试使用这种方法在使用游标时从DB获取值
请尝试以下代码将值从DB添加到哈希映射:
hm = new HashMap<String, Object>();
if (cursor.moveToFirst()) {
do {
text = cursor.getString(cursor.getColumnIndex("title"));
Log.i("Show Title","Show Title : "+text);
cardsCount = cursor.getString(cursor.getColumnIndex("cardsCount"));
collId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("objectId")));
Log.i("CollId","Collection ID : "+collId);
b = BitmapFactory.decodeFile("/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg", null);
hm.put(IMAGE, b);
hm.put(TITLE, text);
hm.put(CARDS_COUNT, cardsCount +" Stampii");
items.add(hm);
} while (cursor.moveToNext());
}
hm=newhashmap();
if(cursor.moveToFirst()){
做{
text=cursor.getString(cursor.getColumnIndex(“标题”);
Log.i(“显示标题”,“显示标题:+文本”);
cardsCount=cursor.getString(cursor.getColumnIndex(“cardsCount”);
collId=Integer.parseInt(cursor.getString(cursor.getColumnIndex(“objectId”));
Log.i(“CollId”,“集合ID:+CollId”);
b=位图工厂.decodeFile(“/sdcard/7073d92dce10884554d7e047f1c51cb6.jpg”,空);
hm.put(图像,b);
hm.put(标题、文本);
hm.put(卡数,卡数+Stampii);
添加项目(hm);
}while(cursor.moveToNext());
}
您应该在for循环中声明hm。当您这样做时:
@Override
public void onCreate(Bundle savedInstanceState){
....
HashMap<String, Object> hm;
....
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
//do something with hm
items.add(hm);
}
...
@Override
public void onCreate(Bundle savedInstanceState){
....
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
HashMap<String, Object> hm;
//do something with hm
items.add(hm);
}
...
@覆盖
创建时的公共void(Bundle savedInstanceState){
....
HashMap-hm;
....
for(cursor.move(0);cursor.moveToNext();cursor.isAfterLast()){
//和hm做点什么
添加项目(hm);
}
...
它会将hashmap的引用添加到项目中,但每次迭代都会更改该引用。因此,本质上,您是在一次又一次地添加相同的引用。您应该这样做:
@Override
public void onCreate(Bundle savedInstanceState){
....
HashMap<String, Object> hm;
....
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
//do something with hm
items.add(hm);
}
...
@Override
public void onCreate(Bundle savedInstanceState){
....
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
HashMap<String, Object> hm;
//do something with hm
items.add(hm);
}
...
@覆盖
创建时的公共void(Bundle savedInstanceState){
....
for(cursor.move(0);cursor.moveToNext();cursor.isAfterLast()){
HashMap-hm;
//和hm做点什么
添加项目(hm);
}
...
您应该在for循环中声明hm。当您这样做时:
@Override
public void onCreate(Bundle savedInstanceState){
....
HashMap<String, Object> hm;
....
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
//do something with hm
items.add(hm);
}
...
@Override
public void onCreate(Bundle savedInstanceState){
....
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()) {
HashMap<String, Object> hm;
//do something with hm
items.add(hm);
}
...
@覆盖
创建时的公共void(Bundle savedInstanceState){
....
HashMap-hm;
....
对于(游标)。移动(0