ListView 中的重复数据?

Duplicate data in ListView?

我有一个 ListViewSqlite 填充。但是当我填写 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 是 true 。我的错误来自 MPSK。

改变这个

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);

                                } 
                            }

检索光标后清除 MPSK..如

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

.....

您的 Struct stp = new Struct(); 应该在 :

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

     } while (cursor_Sharee.moveToNext());

;)