0

这个问题的标题相同,但技术问题不同。

您好我正在尝试从 SQLite 获取数据,但我只能显示列表视图中的最后一项。我尝试了不同的解决方案,但没有成功。问题不是从 SQLite 获取项目(我能够获取所有项目),而是在列表视图中使用适配器显示项目。

这是我的代码。
ListActivity.java

 db=new DBHelper(getBaseContext());
 db.getWritableDatabase();
try {
final DBHelper m = new DBHelper(getBaseContext());
final List<GetSet> NotesWiseProfile = m.getBabyDetails();
for (final GetSet cn : NotesWiseProfile) {
    counter++;              
    String babyName = cn.getBabyName();
    String babyImage = cn.getBabyImage();               
    int babyId = cn.getBabyId();
    BabyData baby_data[]  = new BabyData[]
    {                       
         new BabyData(R.drawable.ic_launcher, babyName,babyId),
            };
        adapter = new MobileArrayAdapter(this, 
                R.layout.list_row, baby_data);
        listView1.invalidateViews();
        listView1.setAdapter(adapter);
  }
}
catch (Exception e) {
}  

BabyData.java

public class BabyData {
public int icon;
public String title;
public int babyid;
public BabyData(){
    super();
}    
public BabyData(int icon, String title,int babyId) {
    super();
    this.icon = icon;
    this.title = title;
    babyid = babyId;
}

}

MobileArrayAdapter.java

public class MobileArrayAdapter extends ArrayAdapter<BabyData>{

Context context; 
int layoutResourceId;    
BabyData data[] = null;

public MobileArrayAdapter(Context context, int layoutResourceId, BabyData[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    DataHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new DataHolder ();
        holder.imgIcon = (ImageView)row.findViewById(R.id.imvBabyFace);
        holder.txtTitle = (TextView)row.findViewById(R.id.tvbabyNameList);
        holder.txtBabyId = (TextView)row.findViewById(R.id.tvBabyId);

        row.setTag(holder);
    }
    else
    {
        holder = (DataHolder )row.getTag();
    }

    BabyData weather = data[position];
    holder.txtTitle.setText(weather.title);
    holder.txtBabyId.setText(String.valueOf(weather.babyid));
    holder.imgIcon.setImageResource(weather.icon);

    return row;
}

static class DataHolder 
{
    ImageView imgIcon;
    TextView txtTitle;
    TextView txtBabyId;
}
}   

我不明白我的代码有什么问题。请给我任何提示或参考。

提前致谢。

4

2 回答 2

3

将 listview 声明置于 for 循环之外,例如:

BabyData baby_data[]  = new BabyData[NotesWiseProfile.size()];

for (final GetSet cn : NotesWiseProfile) {

  String babyName = cn.getBabyName();
  String babyImage = cn.getBabyImage();               
  int babyId = cn.getBabyId();
  baby_data[counter] = new BabyData(R.drawable.ic_launcher, babyName,babyId);
  counter++;    
}
adapter = new MobileArrayAdapter(this, 
            R.layout.list_row, baby_data);
    listView1.invalidateViews();
    listView1.setAdapter(adapter);
于 2013-02-25T07:11:32.487 回答
0

我认为您应该使用一个字段来存放婴儿。目前,您正在为此使用本地 Baby 数组。据我所知,ListView 总是从您传递给它的数组中获取其数据(使其无效会导致 ListView 再次查找该数据。

回顾一下:将数组存储为字段 - 如果数据发生更改,请更新数组并在适配器上调用 notifyDatasetChanged(),这将导致 ListView 重新加载数据。

于 2013-02-25T07:16:43.090 回答