当我在列表视图中滚动时,图像会被洗牌或更改

时间:2014-08-21 21:46:27

标签: android

当我在列表视图中滚动时,我的图像被洗牌或更改.......当我向下滚动并继续改变自己时,图像得到洗牌我有一个自定义列表视图适配器,当我向下/向上滚动时列表,图像被洗牌,我不知道该怎么做。

public class MySimpleArrayAdapter extends BaseAdapter {
     private static ArrayList<String> searchArrayList;
     SQLiteDatabase database;


     private LayoutInflater mInflater;
     ArrayList<String> list = new ArrayList<String>();
     ArrayList<String> listid = new ArrayList<String>();

     public MySimpleArrayAdapter(Context context, ArrayList<String> results) {
      searchArrayList = results;
      mInflater = LayoutInflater.from(context);
      database = context.openOrCreateDatabase("ORCL", context.MODE_PRIVATE, null);
     }

     public int getCount() {
      return searchArrayList.size();
     }

     public Object getItem(int position) {
      return searchArrayList.get(position);
     }

     public long getItemId(int position) {
      return position;
     }

     public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder;
      if (convertView == null) {
       convertView = mInflater.inflate(R.layout.homelistvwlyout, null);
       holder = new ViewHolder();
       holder.txtName = (TextView) convertView.findViewById(R.id.textView1);
       holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
       holder.txtvwid = (TextView) convertView.findViewById(R.id.textView2);
       convertView.setTag(holder);
      } else {
       holder = (ViewHolder) convertView.getTag();
      }

        holder.txtName.setText(searchArrayList.get(position));
//      Cursor cursorQuote = database.rawQuery(
//              "SELECT * FROM  Qotestable where Notification_Status =1",
//              null);
        Cursor cursorQuote = database.query("Qotestable", null,
                "Notification_Status" + "=1", null, null, null, "Quote_ID"
                        + "DESC");



        if (cursorQuote.moveToFirst()) {
            do {

                list.add(cursorQuote.getString(3));
                listid.add(cursorQuote.getString(0));
            } while (cursorQuote.moveToNext());
        }
        //Collections.sort(list, Collections.reverseOrder()); 
        String s = list.get(position);
        if (s.contentEquals("1")) {
            holder.iv.setImageResource(R.drawable.onestaron);

        }else  if (s.contentEquals("2")) {
            holder.iv.setImageResource(R.drawable.twostaron);
        }else  if (s.contentEquals("3")) {
            holder.iv.setImageResource(R.drawable.threestaron);
        }
        holder.txtvwid.setText(listid.get(position));

      return convertView;
     }

     static class ViewHolder {
      TextView txtName,txtvwid;
      TextView txtCityState;
      ImageView iv;
      TextView txtPhone;
     }
    }

1 个答案:

答案 0 :(得分:0)

好的,没有承诺,因为我没有完整的源代码,但这些是我对你的适配器类进行的修改:

public class MySimpleArrayAdapter extends BaseAdapter {

    // Rename these to match your database column names
    private static final int SOME_DATABASE_COLUMN_NAME_ZERO = 0;
    private static final int SOME_DATABASE_COLUMN_NAME_THREE = 3;

    private ArrayList<String> mSearchArrayList;
    private Cursor mCursorQuote;

    public MySimpleArrayAdapter(Context context, ArrayList<String> results) {
        mSearchArrayList = results;
        SQLiteDatabase mDatabase = context.openOrCreateDatabase("ORCL", context.MODE_PRIVATE, null);
        mCursorQuote = mDatabase.query("Qotestable", null,
                "Notification_Status" + "=1", null, null, null, "Quote_ID"
                        + "DESC");
    }

    public int getCount() {
        return mSearchArrayList.size();
    }

    public Object getItem(int position) {
        return mSearchArrayList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<String> listid = new ArrayList<String>();
        ViewHolder holder = null;

        if (convertView == null) {
            LayoutInflater li = LayoutInflater.from(parent.getContext());
            convertView = li.inflate(R.layout.homelistvwlyout, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtName.setText(mSearchArrayList.get(position));

        if (mCursorQuote.moveToFirst()) {
            do {
                list.add(mCursorQuote.getString(SOME_DATABASE_COLUMN_NAME_THREE)); // no magic values
                listid.add(mCursorQuote.getString(SOME_DATABASE_COLUMN_NAME_ZERO)); // no magic values
            } while (mCursorQuote.moveToNext());
        }
        String s = list.get(position);
        if (s.contentEquals("1")) {
            holder.iv.setImageResource(R.drawable.onestaron);
        } else if (s.contentEquals("2")) {
            holder.iv.setImageResource(R.drawable.twostaron);
        } else if (s.contentEquals("3")) {
            holder.iv.setImageResource(R.drawable.threestaron);
        } else {
            // You need the else-case to set your ImageView --> this is probably your problem
            holder.iv.setImageResource(R.drawable.YOUNEEDSOMETHINGHERE);
        }
        holder.txtvwid.setText(listid.get(position));

        return convertView;
    }

    static class ViewHolder {
        TextView txtName;
        TextView txtvwid;
        ImageView iv;

        /**
         * A constructor that takes a View will encourage the next person to do the right thing with the costly call to
         * findViewById()
         *
         * @param v
         */
        ViewHolder(View v) {
            this.txtName = (TextView) v.findViewById(R.id.textView1);
            this.iv = (ImageView) v.findViewById(R.id.imageView1);
            this.txtvwid = (TextView) v.findViewById(R.id.textView2);
        }
    }
}