如何更新单击的行?

时间:2014-04-30 18:45:35

标签: android sql listview sql-update

经过很长一段时间后,我发现将参数从0更改为不同的数字允许我将具有相应ID的行更新为此数字:

private void saveItToDB() {
    dba.open();
    dba.updateDiaryEntry(((TextView) editText).getText().toString(), 2);
    dba.close();
    ((TextView) editText).setText("");
}

在上面的代码中,我将0改为2,我能够更新第2行的内容。这是完整的类代码:

class EditListItemDialog extends Dialog implements View.OnClickListener {
MyDB dba;
private View editText;
private DiaryAdapter adapter;
private SQLiteDatabase db;

public EditListItemDialog(Context context) {           
    super(context);
    dba = new MyDB(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
View btnOk = findViewById(R.id.button_ok);
editText = findViewById(R.id.edit_text);
btnOk.setOnClickListener(this);

dba.open();
}

private List<String> fragment_monday;

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Position is the number of the item clicked
//You can use your adapter to modify the item
adapter.getItem(position); //Will return the clicked item
}

public EditListItemDialog(Context context, DiaryAdapter adapter, int position) {
super(context);
this.fragment_monday = new ArrayList<String>();
this.adapter = adapter;
dba = new MyDB(context);
}

@Override
public void onClick(View v) {

fragment_monday.add(((TextView) v).getText().toString());//here is your updated(or not updated) text
dismiss();
try {
    saveItToDB();
} catch (Exception e) {
    e.printStackTrace();
}
}

private void saveItToDB() {
    dba.open();
    dba.updateDiaryEntry(((TextView) editText).getText().toString(), 2);
    dba.close();
    ((TextView) editText).setText("");
}



}

现在,我需要做些什么来告诉它我要更新被点击的行,而不是第2行?任何帮助将不胜感激。

这是MyDB代码:

public class MyDB {
private static final String TABLE_NAME = null;
private static final String KEY_ID = null;
private SQLiteDatabase db;
private final Context context;
private final MyDBhelper dbhelper;



// Initializes MyDBHelper instance
public MyDB(Context c){

    context = c;
    dbhelper = new MyDBhelper(context, Constants.DATABASE_NAME, null,
                                        Constants.DATABASE_VERSION);

}


// Closes the database connection
public void close()
{
    db.close();

}

// Initializes a SQLiteDatabase instance using MyDBhelper
public void open() throws SQLiteException
{

    try {
        db = dbhelper.getWritableDatabase();
    } catch(SQLiteException ex) {
        Log.v("Open database exception caught", ex.getMessage());
        db = dbhelper.getReadableDatabase();
    }

}

// updates a diary entry (existing row)
    public boolean updateDiaryEntry(String title, long rowId)
    {

        ContentValues newValue = new ContentValues();
        newValue.put(Constants.TITLE_NAME, title);

        return db.update(Constants.TABLE_NAME, newValue, Constants.KEY_ID + "=" + rowId, null)>0;

    }

// Reads the diary entries from database, saves them in a Cursor class and returns it from the method
public Cursor getdiaries()
{
    Cursor c = db.query(Constants.TABLE_NAME, null, null,
                        null, null, null, null);
    return c;
}


}

这是适配器:

public class Monday extends ListActivity {


private SQLiteDatabase db;
private static final int MyMenu = 0;
MyDB dba;
DiaryAdapter myAdapter;

private class MyDiary{
    public MyDiary(String t, String c){
        title=t;
        content=c;



}

    public String title;
    public String content;

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    dba = new MyDB(this);
    dba.open();
    setContentView(R.layout.fragment_monday);





    super.onCreate(savedInstanceState);
    myAdapter = new DiaryAdapter(this);
    this.setListAdapter(myAdapter);
}



public class DiaryAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private ArrayList<MyDiary> fragment_monday;
    public DiaryAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        fragment_monday = new ArrayList<MyDiary>();
        getdata();

        ListView list = getListView();
        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    new EditListItemDialog(Monday.this, null, position).show();

                return true;       
            }
        });

    }

    public void getdata(){
        Cursor c = dba.getdiaries();
        startManagingCursor(c);
        if(c.moveToFirst()){
            do{
                String title =
                        c.getString(c.getColumnIndex(Constants.TITLE_NAME));
                String content =
                        c.getString(c.getColumnIndex(Constants.CONTENT_NAME));

                MyDiary temp = new MyDiary(title,content);
                fragment_monday.add(temp);
            } while(c.moveToNext());
        }

    }



    @Override
    public int getCount() {return fragment_monday.size();}
    public MyDiary getItem(int i) {return fragment_monday.get(i);}
    public long getItemId(int i) {return i;}
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        final ViewHolder holder;

        View v = arg1;
        if ((v == null) || (v.getTag() == null)) {
            v = mInflater.inflate(R.layout.diaryrow,  null);
            holder = new ViewHolder();
            holder.mTitle = (TextView)v.findViewById(R.id.name);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        holder.mdiary = getItem(arg0);
        holder.mTitle.setText(holder.mdiary.title);


        v.setTag(holder);

        return v;


    }

    public class ViewHolder {
        MyDiary mdiary;
        TextView mTitle;


    }

}




/** Called when the user clicks the Edit button */
public void visitDiary(View view) {
    Intent intent = new Intent(this, Diary.class);
    startActivity(intent);
}
/** Called when the user clicks the back button */
public void visitSchedule(View view) {
    Intent intent = new Intent(this, DisplayScheduleScreen.class);
    startActivity(intent);
}


}

1 个答案:

答案 0 :(得分:0)

在适配器中实施getItemId(int position)。在onListItemClick方法中,使用给定的位置调用此方法。

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    long itemId = adapter.getItemId(position);
    saveItToDB(itemId);
}

private void saveItToDB(long itemId) {
    String text = editText.getText().toString();
    editText.setText("");

    dba.open();
    dba.updateDiaryEntry(text, itemId);
    dba.close();
}