ListView中的重复数据?

时间:2015-01-05 07:34:48

标签: android sqlite listview adapter

我有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。

3 个答案:

答案 0 :(得分:1)

检索游标后清除MPSK ..

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

.....

答案 1 :(得分:1)

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

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

     } while (cursor_Sharee.moveToNext());

答案 2 :(得分:0)

更改此

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

                                } 
                            }