在BaseAdapter Android中使用Viewholder时加载数据错误

时间:2014-04-19 11:22:50

标签: android android-listview baseadapter

我正在使用BaseAdapter在gridview中加载数据。当数据加载到gridview,并且用户尝试滚动时,我曾经得到不正确的数据。为了解决这个问题,我使用了ViewHolder。这实际上解决了问题,但是,当用户单击gridview中的任何数据时,而不是从项目中获取正确的ID,所有项目都返回类似的ID号。

public class ProductAdapter extends BaseAdapter {
// Context context;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
ViewHolder holder;
int layoutResourceId;

Event event;
EventDBAdapter eventDb;

int id;
PersianCalendar sc;
String Today;
SharedPreferences Prefrences_test;
String date, userid;
String newText = "";
String test;
int status;

MoodsDBAdapter moodsDBAdapter;
Mood mood;
ArrayList<MoodsNote> arrayMoodsNote;
MoodsNoteDBAdapter moodsNoteDBAdapter;
MoodsNote moodsNote;


public ProductAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    // this.context = context;
    eventDb = new EventDBAdapter(activity);
    event = new Event();
    moodsDBAdapter = new MoodsDBAdapter(activity);
    mood = new Mood();
    moodsNoteDBAdapter = new MoodsNoteDBAdapter(activity);
    data = d;
    // this.layoutResourceId = layoutResourceId;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    Prefrences_test = activity.getSharedPreferences("temp", 0);
    date = Prefrences_test.getString("date", "");
    userid = Prefrences_test.getString("userid", "");
    sc = new PersianCalendar();
    Today = date;
    eventDb = new EventDBAdapter(activity.getApplicationContext());

    event = eventDb.getName(Today);

    try {
        arrayMoodsNote = moodsNoteDBAdapter.getItems(event.getID());
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

}

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

public Object getItem(int position) {
    return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    holder = null;



    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    if (vi == null) {
        vi = inflater.inflate(R.layout.note_mood_list_item, null);
        holder = new ViewHolder();
        holder.title = (TextView) vi.findViewById(R.id.mood_text);
        holder.txt_id = (TextView) vi.findViewById(R.id.txt_id_mood);
        holder.thumb_image = (ImageView) vi.findViewById(R.id.mood_img);
        holder.linear = (LinearLayout) vi.findViewById(R.id.mood_layout);
        vi.setTag(holder);
    } else {

        holder = (ViewHolder) vi.getTag();

    }
    holder.linear.setBackgroundResource(R.drawable.bg_mood);
    //holder.txt_id.setText("");


    String titles = song.get("name");
    holder.title.setText(titles);

    Resources res = activity.getResources();
    int resourceId = res.getIdentifier(song.get("icon"), "drawable",
            activity.getPackageName());
    holder.thumb_image.setBackgroundResource(resourceId);

    holder.txt_id.setText(song.get("id"));

    try {
        for (MoodsNote m : arrayMoodsNote) {
            int moodd = m.getMOOD();
            if (Integer.parseInt(holder.txt_id.getText().toString()) == moodd)

            {

                holder.linear.setBackgroundResource(R.drawable.bg_mood_on);

            }

        }
    } catch (Exception e) {
        // TODO: handle exception
    }

    holder.thumb_image.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Toast.makeText(activity, holder.txt_id.getText().toString(), 0).show();
            status = 1;
            event = new Event();
            int tedad = eventDb.getDate(date);

            if (tedad <= 0) {

                event = new Event(userid, date);
                eventDb.insert(event);
            }

            event = eventDb.getName(date);
            moodsNote = new MoodsNote();
            id = 0;
            id = Integer.parseInt(holder.txt_id.getText().toString());
            // id = rowItem.getID();
            int tedadd = moodsNoteDBAdapter.getLastItemID(id);

            if (tedadd <= 0)

            {
                moodsNote.setMOOD(id);
                moodsNote.setEVENT(event.getID());
                moodsNoteDBAdapter.insert(moodsNote);

                holder.linear.setBackgroundResource(R.drawable.bg_mood_on);
            }

            else {

                moodsNoteDBAdapter.deleteId(id);
                holder.linear.setBackgroundResource(R.drawable.bg_mood);

            }

        }
    });

    return vi;
}

static class ViewHolder {
    ImageView thumb_image;
    TextView txt_id, title;
    LinearLayout linear;

}

0 个答案:

没有答案