ListView onItemClick更新:只能更新第一个项目

时间:2015-01-11 08:15:57

标签: android listview android-listview

我有一个listView,它由我的数据库中的数据填充。如果用户点击列表视图中的项目,他可以更新该项目的数据。现在的问题是,只能更新列表视图中的第一项。这是我的代码:

 private ListView.OnItemClickListener listContentOnItemClickListener
            = new ListView.OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {
                    // TODO Auto-generated method stub

                    Cursor cursorclick = (Cursor) parent.getItemAtPosition(position);
                    final String item_id = cursorclick.getString(cursorclick.getColumnIndex(ScoresDbAdapter.KEY_ID));
                    String item_score  = cursorclick.getString(cursorclick.getColumnIndex(ScoresDbAdapter.KEY_Score));
                    final String stud_id  = cursorclick.getString(cursorclick.getColumnIndex(ScoresDbAdapter.KEY_StudentID));


                    AlertDialog.Builder myDialog 
                    = new AlertDialog.Builder(Scores_Student_Profile.this);

                    myDialog.setTitle("Update Score");

                    TextView dialogTxt_id = new TextView(Scores_Student_Profile.this);
                    LayoutParams dialogTxt_idLayoutParams 
                     = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    dialogTxt_id.setLayoutParams(dialogTxt_idLayoutParams);
                    dialogTxt_id.setText("#" + String.valueOf(item_id));

                    final EditText dialogC1_id = new EditText(Scores_Student_Profile.this);
                    LayoutParams dialogC1_idLayoutParams 
                     = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                    dialogC1_id.setLayoutParams(dialogC1_idLayoutParams);
                    dialogC1_id.setText(item_score);

                    LinearLayout layout = new LinearLayout(Scores_Student_Profile.this);
                    layout.setOrientation(LinearLayout.VERTICAL);
                    layout.addView(dialogTxt_id);
                    layout.addView(dialogC1_id);

                    myDialog.setView(layout);



                    myDialog.setPositiveButton("Update", new DialogInterface.OnClickListener() {

                        // do something when the button is clicked
                        public void onClick(DialogInterface arg0, int arg1) {
                            String value1 = dialogC1_id.getText().toString();


 //I tried to make a toast here to see if the data is correct. The variables shows the intended values
Toast.makeText(getApplicationContext(),  " "+mRowId+" "+critspnrSelected+" "+item_id+" "+value1+" "+stud_id, Toast.LENGTH_LONG).show();

                            scoresDataBaseAdapter.update_byID(mRowId,critspnrSelected,item_id,value1,stud_id);
                            updateList();

                         }
                        });

                    myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        // do something when the button is clicked
                        public void onClick(DialogInterface arg0, int arg1) {

                         }
                        });

                    myDialog.show();


                }};

                private void updateList(){
                    cursorShowScore.requery();
                }  

        }

下面的代码是我如何显示ListView:

if (mRowId != null) {

                cursorShowScore = scoresDataBaseAdapter.queueSpnrScore(mRowId, critspnrSelected,studID);





            String[] from = new String[]{ScoresDbAdapter.KEY_ID,ScoresDbAdapter.KEY_Score};
            int[] to = new int[]{R.id.critnumberof,R.id.studscoreon};


                cursorAdapter = new SimpleCursorAdapter(this, R.layout.score_students_profile_criteria_row, cursorShowScore, from, to);
                listContent.setAdapter(cursorAdapter);
                listContent.setOnItemClickListener(listContentOnItemClickListener);

        } 

         }

这就是我更新的方式:

public void update_byID(String table,String crit,String id, String score,String studid){
                String tab = table+"_"+crit+"_scores";

                ContentValues values = new ContentValues();

                values.put(KEY_Score, score);

                mDb.update(tab, values,
                        KEY_ScoreID + " = ? and "+ KEY_StudentID + " = ?",
                        new String[] { id,studid });

            }

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

原来我在update_byID中传递了一个错误的变量。我通过声明解决了这个问题

final String score_id = cursorclick.getString(cursorclick.getColumnIndex(ScoresDbAdapter.KEY_ScoreID));

我将更新调用更改为:

scoresDataBaseAdapter.update_byID(mRowId,critspnrSelected,score_id,value1,stud_id);