Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView中是否存在重复数据?_Android_Sqlite_Listview_Adapter - Fatal编程技术网

Android ListView中是否存在重复数据?

Android ListView中是否存在重复数据?,android,sqlite,listview,adapter,Android,Sqlite,Listview,Adapter,我有一个从Sqlite填充的ListView。但是当我填充列表视图时,我看到重复的数据 我的结构类: public class Struct{ public String IdS; public String NameS; public String Group; } ArrayAdapter adapter = new AdapterPSK(MPSK, activity, width, context); ListView lstPa = (ListV

我有一个从
Sqlite
填充的
ListView
。但是当我填充
列表视图时,我看到重复的数据

我的
结构类

public class Struct{
    public String IdS;
    public String NameS;
    public String Group;
}
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();
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;
    }
我的
活动.类

public class Struct{
    public String IdS;
    public String NameS;
    public String Group;
}
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();
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;
    }
MPSK
是:

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

注意:
ListView
显示sqlite的最后一行。但是当我得到
Log
时,我会看到所有的获取数据。我的
Query
是真的。我的错误来自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());

)()

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