当我尝试更新listview的一行时,效果行总是最后一行

时间:2016-10-12 06:55:15

标签: android listview android-sqlite onclicklistener

美好的一天,
我的应用程序是一个简单的备忘录应用

这是我的 customAdapter

public class CustomAdapter extends BaseAdapter{   
private ArrayList<MemoDetails> mMemos = new ArrayList<MemoDetails>();
private Context mContext;
boolean isDeleteMode = false;

private LayoutInflater inflater=null;
public CustomAdapter(MemoListActivity mainActivity, ArrayList<MemoDetails> memos) {

    mMemos = memos;
    mContext = mainActivity;

    inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    return mMemos.size();
}

@Override
public Object getItem(int position) {
    return mMemos.get(position);
}

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

public class Holder
{
    TextView tViewMemoTitle;
    TextView tViewMemoCreatedDate;
    TextView tViewMemoAlarmedDate;
    CheckBox cb;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Holder holder=new Holder();
    View rowView;       
    rowView = inflater.inflate(R.layout.program_list, null);

    holder.tViewMemoTitle=(TextView) rowView.findViewById(R.id.memo_list_title);      
    holder.tViewMemoCreatedDate=(TextView) rowView.findViewById(R.id.memo_list_created_date);
    holder.tViewMemoAlarmedDate=(TextView) rowView.findViewById(R.id.memo_list_alarmed_date);
    holder.cb=(CheckBox) rowView.findViewById(R.id.cbBox);

    final MemoDetails memo = getDetails(position);

    holder.tViewMemoTitle.setText(memo.getmMemoTitle());     
    holder.tViewMemoCreatedDate.setText("Created Date : " + memo.getmCreatedDate());
    holder.tViewMemoAlarmedDate.setText("Alarm Date : " + memo.getmAlarmedDate() + " " + memo.getmAlarmed());

    if(isDeleteMode) {
        holder.cb.setVisibility(View.VISIBLE);
    } else {
        holder.cb.setVisibility(View.INVISIBLE);
    }

    rowView.setOnClickListener(new OnClickListener() {            
        @Override
        public void onClick(View v) {
            Cursor c=SplashActivity.db.rawQuery("SELECT * FROM memos WHERE id='"+memo.getmMemoId()+"'", null);
            if(c.moveToFirst()) {
                SplashActivity.db.execSQL("UPDATE memos SET "
                        + "alarmed='1' WHERE "
                        + "id = '" + memo.getmMemoId() + "'" );
                memo.setmAlarmed("1");
                MemoListActivity.mCustomAdapter.notifyDataSetChanged();
                notifyDataSetChanged();
            }
        }
    }); 


    holder.cb.setOnCheckedChangeListener(myCheckChangList);
    holder.cb.setTag(position);
    holder.cb.setChecked(memo.mCheckBox);

    return rowView;
}

private MemoDetails getDetails(int position) {
    return ((MemoDetails) getItem(position));
}

public ArrayList<MemoDetails> getBox() {
    ArrayList<MemoDetails> box = new ArrayList<MemoDetails>();
    for (MemoDetails memo : mMemos) {
        if (memo.mCheckBox)
            box.add(memo);
    }
    return box;
}

private OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        getDetails((Integer) buttonView.getTag()).mCheckBox = isChecked;
    }
};

}

我的每份备忘录都有

的详细信息
alarmed = '0' 

所以当我点击特定行时,它将转向

alarmed = '1'  

我的问题是,每当我点击列表视图中的任何一行时,
唯一受影响的行是最后一行。

这是我的 onclicklistener 部分

rowView.setOnClickListener(new OnClickListener() {            
        @Override
        public void onClick(View v) {
            Cursor c=SplashActivity.db.rawQuery("SELECT * FROM memos WHERE id='"+memo.getmMemoId()+"'", null);
            if(c.moveToFirst()) {
                SplashActivity.db.execSQL("UPDATE memos SET "
                        + "alarmed='1' WHERE "
                        + "id = '" + memo.getmMemoId() + "'" );
                memo.setmAlarmed("1");
                MemoListActivity.mCustomAdapter.notifyDataSetChanged();
                notifyDataSetChanged();
            }
        }
    }); 

代码已更新

我删除了

private MemoDetails memo;  

作为全局变量将其实例化为最终

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final MemoDetails memo = getDetails(position);

}  

它已修复:)

1 个答案:

答案 0 :(得分:0)

此代码错误

private static string CalculateUnitNumber(string sInput)
    {
        char[] array = sInput.ToCharArray();
        List<int> sequence = array.Select(c => Convert.ToInt32(c.ToString())).ToArray().ToList();
        string sSum = sequence.Sum() + "";
        while (sSum.Length != 1)
        {
            sSum = CalculateUnitNumber(sSum);
        }
        return sSum;
    }

当您单击该行时,您尝试从“memo.getmMemoId()”获取id,这是您的arrayList的最后一个对象。

你应该从行中获取id,然后才能正常工作。

以多种方式完成。如果你需要id,你只需在viewholder类中声明一个变量“id”

 rowView.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {
                Cursor c=SplashActivity.db.rawQuery("SELECT * FROM memos WHERE id='"+memo.getmMemoId()+"'", null);
                if(c.moveToFirst()) {
                    SplashActivity.db.execSQL("UPDATE memos SET "
                            + "alarmed='1' WHERE "
                            + "id = '" + memo.getmMemoId() + "'" );
                    memo.setmAlarmed("1");
                    MemoListActivity.mCustomAdapter.notifyDataSetChanged();
                    notifyDataSetChanged();
                }
            }
        }); 

然后为这样的每一行添加id

public class Holder
{
    TextView tViewMemoTitle;
    TextView tViewMemoCreatedDate;
    TextView tViewMemoAlarmedDate;
    CheckBox cb;
   String id;
}

然后每个行对象的id为object。

holder.tViewMemoTitle.setText(memo.getmMemoTitle());     
    holder.tViewMemoCreatedDate.setText("Created Date : " + memo.getmCreatedDate());
    holder.tViewMemoAlarmedDate.setText("Alarm Date : " + memo.getmAlarmedDate() + " " + memo.getmAlarmed());

**holder.id=memo.getmMemoId();**

这是我的代码,请检查一次。