通过更改列表项微调器值来更新数据库

时间:2014-05-13 10:10:03

标签: android

您好我是android的新手,我遇到了问题,我有一个带有项目微调器的列表视图, 在更改listview的每一行中的所有微调器之后,它应该在提交按钮单击时将值更新到数据库,但它仅更改最后一个值。

提前感谢。

    slot1_spinn.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub

            value_spinn1 = slot1_spinn.getSelectedItem().toString();
            System.out.println("spinner on click value  "
                    + value_spinn1);

            int id = arg2;
            m_id = picture.member_id;

            System.out.println("slot1 spinner onclick get member id "
                    + m_id);


            System.out.println("spinner id "
                    + id);
            int spinnerValue = slot1_spinn.getSelectedItemPosition();
            System.out.println(slot1_spinn.getSelectedItemPosition());

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

这是列表项微调器点击监听器

下面的

是更新值的代码

private void edit_attendance() {
        // TODO Auto-generated method stub

        String date_update = datePicker.getText().toString();

        baseHandler = new DataBaseHandler(EditAttendance.this);
        SQLiteDatabase db = baseHandler.getWritableDatabase();
        ContentValues args = new ContentValues();


        args.put("status", value_spinn1);
        args.put("slot1", value_spinn1);
        args.put("slot2", value_spinn1);

        String[] args11 = new String[] { date_update, m_id };
        db.update("attendance", args, "att_date=? AND member_id=?",
                args11);


        db.close();
        baseHandler.close();



        System.out.println("edit_attendance value_spinn1"
                + value_spinn1);

        System.out.println("edit_attendance date_update" + date_update);
        System.out.println("edit_attendance member id" + m_id);

        Toast.makeText(getApplicationContext(), "Attendance has been changed successfully", 5000).show();

    }
});

1 个答案:

答案 0 :(得分:0)

我怀疑它是因为你保存了微调器选择的值,如下所示:

        value_spinn1 = slot1_spinn.getSelectedItem().toString();

然而,在更新方法中,您保存m_id而不是value_spinn1的值,m_id似乎始终具有从图片对象中获取的值,如在所选项目中指定的那样监听器:

        m_id = picture.member_id;

假设绑定到微调器的对象列表与picture是同一个类,则可以通过将微调器绑定到覆盖getItemId()的自定义适配器来获取成员标识。

public class PictureAdapter extends BaseAdapter {

Context context;
List<Picture> pictures;

public PictureAdapter(Context context, List<picture> pictures) {
    this.context = context;
    this.pictures = pictures;
}

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

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

public long getItemId(int position)
{
     return pictures.get(position).member_id;
}

public View getView(int position, View convertView, ViewGroup parent)
{
    if(convertView == null)
    {
        LayoutInflater inflater = LayoutInflater.from(context); 
        convertView = inflater.inflate(R.layout.my_picture_layout); 
    }

    //Or however you store your images        ((ImageView)convertView.findViewById(R.id.member_image)).setImageResource(getItem(position).imageResource));

return view;
}

}

然后在Item Selected Listener中,最后一个参数将是成员Id:

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int postion, long id)  
{
    m_id = id;
}