【问题标题】:Duplicate data in ListView?ListView 中的重复数据?
【发布时间】:2015-01-05 07:34:48
【问题描述】:

我有一个 ListView 填充来自 Sqlite 。但是当我填写ListView 时,我看到了重复数据。

我的Struct.class

public class Struct{
    public String IdS;
    public String NameS;
    public String Group;
}

我的Activity.class

ArrayAdapter adapter = new AdapterPSK(MPSK, activity, width, context);
        ListView lstPa = (ListView) findViewById(R.id.lstPasokhgoo);
        lstPa.setAdapter(adapter);

                Struct stp = new Struct();
                cursor_Sharee = sql.rawQuery("SELECT ID_P,Name_P,Group_P FROM ChatPasokhgo_tbl where Group_P = '" + "LOL" + "'", null);
                try {
                    if (cursor_Sharee != null) {
                        if (cursor_Sharee.moveToFirst()) {
                            do {
                                stp.IdS = cursor_Sharee.getString(cursor_Sharee.getColumnIndex("ID_P"));
                                stp.NameS = cursor_Sharee.getString(cursor_Sharee.getColumnIndex("Name_P"));
                                stp.Group = cursor_Sharee.getString(cursor_Sharee.getColumnIndex("Group_P"));
                                MPSK.add(stp);
                            } while (cursor_Sharee.moveToNext());
                        }
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }finally{
                    cursor_Sharee.close();
                }
                adapter.notifyDataSetChanged();

MPSK 是:

public ArrayList<Struct> MPSK = new ArrayList<Struct>();

我的AdapterPSK.class

public class AdapterPSK extends ArrayAdapter<Struct> {
    static boolean enable = true;
    static Activity activity;
    static int widths;
    public AdapterPSK(ArrayList<Struct> array,Activity act,int WIDTH,Context context) {
        super(context, R.layout.chat_page, array);
        activity = act;
        widths = WIDTH;
    }

        public ViewHolder(View view)
        {
            txtName = (TextView) view.findViewById(R.id.txtname);
            layoutRoot = (ViewGroup) view.findViewById(R.id.layoutRoot);
            txtname = (TextView) view.findViewById(R.id.txtname);
        }
        public void fill(ArrayAdapter<Struct> arrayAdapter,final Struct item , int position)
        {
            MarginLayoutParams txtN = (MarginLayoutParams) txtname.getLayoutParams();
            txtN.rightMargin = MarginRight;
            txtname.setTextSize(TextSize);

            if (item.NameS != null) {
                txtName.setText(item.NameS);
            }else if(item.NameE != null){
                txtName.setText(item.NameE);
            }

        }
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        Struct item = getItem(position);
        if(convertView == null)
        {
            convertView = G.inflater.inflate(R.layout.adapter_pasokhgoo,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.fill(this, item, position);
        return convertView;
    }

注意:ListView 显示 sqlite 的最后一行。但是当我得到Log 时,请查看所有获取数据。我的Query 是真的。我的错误来自 MPSK。

【问题讨论】:

  • 循环内最后一行的 MPSK 是什么?
  • MPSK 是:public ArrayList MPSK = new ArrayList();
  • 您的列表视图是否只显示您的 sqlite 数据的最后一行?
  • 如果我的 sqlite 在 listView 中有 2 行,则显示 2 行,但数据是最后一行的值。

标签: android sqlite listview adapter


【解决方案1】:

您的Struct stp = new Struct(); 应位于:

  do {
        Struct stp = new Struct();
          .....
           ......

     } while (cursor_Sharee.moveToNext());

;)

【讨论】:

    【解决方案2】:

    检索光标后清除 MPSK.. as

         cursor_Sharee = sql.rawQuery("SELECT ID_P,Name_P,Group_P FROM ChatPasokhgo_tbl where Group_P = '" + "LOL" + "'", null);
        MPSK.clear();
    try
    {
    
    .....
    

    【讨论】:

      【解决方案3】:

      改变这个

      if (cursor_Sharee != null) {
                              if (cursor_Sharee.moveToFirst()) {
                                  do {
                                      stp.IdS = cursor_Sharee.getString(cursor_Sharee.getColumnIndex("ID_P"));
                                      stp.NameS = cursor_Sharee.getString(cursor_Sharee.getColumnIndex("Name_P"));
                                      stp.Group = cursor_Sharee.getString(cursor_Sharee.getColumnIndex("Group_P"));
                                      MPSK.add(stp);
                                  } while (cursor_Sharee.moveToNext());
                              }
      

      收件人

       if (cursor_Sharee != null) 
      {                       
            while(cursor_Sharee.moveToNext()) 
                   {
      
       stp.IdS = cursor_Sharee.getString(cursor_Sharee.getColumnIndex("ID_P"));
                                          stp.NameS = cursor_Sharee.getString(cursor_Sharee.getColumnIndex("Name_P"));
                                          stp.Group = cursor_Sharee.getString(cursor_Sharee.getColumnIndex("Group_P"));
                                          MPSK.add(stp);
      
                                      } 
                                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-02
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-04
        相关资源
        最近更新 更多