基于存储在SQLite中的主键编辑ListView

时间:2014-11-05 23:19:50

标签: android sqlite listview android-cursoradapter

我有一个listview,它有多个项目,所有数据当前都存储在数据库中。我希望能够通过列表视图

编辑和删除所选项目
 String CREATE_DRINK_TABLE = "CREATE TABLE drinks (" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "type TEXT, " + "volume DOUBLE, " + "time TEXT, " +
            "abv DOUBLE, " +
            "image BLOB )";

    //create drinks table
    database.execSQL(CREATE_DRINK_TABLE);
}

之后我有一个光标适配器

public List<NewDrink> getAllNewDrinks(){
        List<NewDrink> newDrinks = new LinkedList<NewDrink>();

        //1.build the query
        String query = "SELECT * FROM " + TABLE_DRINKS;

        //2. get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query,null);

        //3.go over each row,build book and add it to list
        NewDrink newdrink = null;
        if(cursor.moveToFirst()){
            do{
                newdrink = new NewDrink();
                newdrink.setId(Integer.parseInt(cursor.getString(0)));
                newdrink.setType(cursor.getString(1));
                newdrink.setVolume(cursor.getDouble(2));
                newdrink.setTime(cursor.getString(3));
                newdrink.setAbv(cursor.getDouble(4));
                newdrink.setImage(cursor.getBlob(5));


                newDrinks.add(newdrink);
            }while(cursor.moveToNext());
        }
        db.close();
        return newDrinks;
    }

一旦它在这里,我将所有饮料填入列表视图中。但是在列表视图中我希望能够删除和编辑饮料输入。

公共类DrinkLog_Screen扩展了Activity {

AlcoholDBHelper db = new AlcoholDBHelper(this);
ArrayList<NewDrink> imageArray = new ArrayList<NewDrink>();
ContactImageAdapter adapter;
ListView dataList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drink_log__screen);
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

    List<NewDrink> newdrinks = new ArrayList<NewDrink>();
    newdrinks = db.getAllNewDrinks();
    for(NewDrink nd : newdrinks){
        imageArray.add(nd);
    }
    adapter = new ContactImageAdapter(this,R.layout.image_screen_list,imageArray);
    ListView dataList = (ListView)findViewById(R.id.drink_list);
    dataList.setAdapter(adapter);
    registerForContextMenu(dataList);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.drink_log__screen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    return super.onOptionsItemSelected(item);
}
public void goToMainScreen (View v) {

    Intent intent = new Intent(this,Main_Screen.class);
    startActivity(intent);
}

}

这是我的列表视图的xml。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#ff1a1a1a">


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="470dp"
        android:id="@+id/drink_list"
        android:cacheColorHint="#0000"
        android:textColor="#ffffffff"
        android:layout_below="@+id/btnReturnMS"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnReturnMS"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="20sp"
        android:textColor="#ffffffff"
        android:text="@string/drink_log" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:text="@string/return_to_main_screen"
        android:id="@+id/btnLogin"
        android:layout_alignParentBottom="true"
        android:textColor="#ffffffff"
        android:textSize="15sp"
        android:onClick="goToMainScreen"
        android:background="#ff302f2e"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

在列表视图中,您可以像这样添加一个setOnItemClickListener

listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
            YOurClass item = (YourClass) listview.getItemAtPosition(position);
            /* get method from obtain your id ->getIdYourClass(); from each item from your listview */
            int id= item.getIdYourClass();
            /*
             pass data to custom dialog or another activity and them edit a row
             example-..
            */
            dialogEdit(id);
        } 
    });

我希望有帮助,对不起我的乐队英语