0

我有以下两个文件:

podatkovna_baza.java

package com.example.ultimate.basketball.stats;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class podatkovna_baza extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "baza5";

    public podatkovna_baza(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }


    @Override
    public void onCreate(SQLiteDatabase baza5) {
        /*
         * Create the employee table and populate it with sample data.
         * In step 6, we will move these hardcoded statements to an XML document.
         */
        String sql = "CREATE TABLE IF NOT EXISTS ekipe4 (" +
                                        "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                                        "ime_ekipe TEXT, " +
                                        "kratko_ime TEXT, " +
                                        "kraj TEXT, " +
                                        "slika TEXT )";
        baza5.execSQL(sql);

        ContentValues values = new ContentValues();

        values.put("ime_ekipe", "Drustvo partizan");
        values.put("kratko_ime", "DRP");
        values.put("kraj", "Mirna");
        values.put("slika", "jajaja");
        baza5.insert("ekipe4", null, values);
    }


    @Override
    public void onUpgrade(SQLiteDatabase baza5, int oldVersion, int newVersion) {
        baza5.execSQL("DROP TABLE IF EXISTS ekipe4");
        onCreate(baza5);
    }       
}

osnovni_meni.java

package com.example.ultimate.basketball.stats;


import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;



public class osnovni_meni extends Activity {
    protected SQLiteDatabase baza5;
    protected Cursor cursor;
    protected ListAdapter adapter;
    protected ListView ekipe_list;
    protected EditText searchtext;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_osnovni_meni);
        baza5 = (new podatkovna_baza(this)).getWritableDatabase();

        //searchtext = (EditText) findViewById (R.id.searchText);
        //searchText = (EditText) findViewById (R.id.searchText);
        //employeeList = (ListView) findViewById (R.id.list);
    }


    public void hello(View view) {
        //cursor = baza2.rawQuery("SELECT ime_ekipe, kratko_ime, kraj FROM ekipe2" , null);

        ekipe_list = (ListView) findViewById (R.id.lista);

        cursor = baza5.query("ekipe4", null, null, null, null, null,
                        "_id" + " ASC");

        adapter = new SimpleCursorAdapter(
                        this, 
                        R.layout.ekipe_layout, 
                        cursor, 
                        new String[] {"_id","kratko_ime","kraj"}, 
                        new int[] {R.id.ime_ekipe,R.id.kratko_ime,R.id.kraj});
        ekipe_list.setAdapter(adapter);
        String sadas=adapter.toStr
    }


    public void delete_byID(int id) {
        baza5.delete("ekipe4", "_id"+"="+id, null);
    }


    public void delete_by_ime_ekipe(String ime) {
        baza5.delete("ekipe4", "kraj"+"="+ime, null);
    }


    public int deleteAll() {
        return baza5.delete("ekipe4", null, null);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_osnovni_meni, menu);
        return true;
    }


    public void onBackPressed() {
        // TODO Auto-generated method stub
        //super.onBackPressed();
        finish();
    }


    public void vibriraj()
    {
        Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);               
        v.vibrate(30);      
    }


    public void vpisi(View view)
    {   
        ContentValues values1 = new ContentValues();
        //values1.put("_id", "1");
        values1.put("ime_ekipe", "Chicago Bulls");
        values1.put("kratko_ime", "CHI");
        values1.put("kraj", "Chicago");
        baza5.insert("ekipe4", "kratko_ime", values1);
        vibriraj();
    }


    public void vpisi_ekipo(View view)
    {    
        vibriraj();

        EditText novo_ime_ekipe = (EditText) findViewById (R.id.novo_ime_ekipe);
        EditText novo_kratko_ime_ekipe = (EditText) findViewById (R.id.novo_kratko_ime_ekipe);
        EditText novo_kraj_ekipe = (EditText) findViewById (R.id.novo_kraj_ekipe);
        EditText novo_slika_ekipe = (EditText) findViewById (R.id.novo_slika_ekipe);

        ContentValues values1 = new ContentValues();

        values1.put("ime_ekipe", (novo_ime_ekipe.getText()).toString());
        values1.put("kratko_ime", (novo_kratko_ime_ekipe.getText()).toString());
        values1.put("kraj", (novo_kraj_ekipe.getText()).toString());
        values1.put("slika", (novo_slika_ekipe.getText()).toString());
        baza5.insert("ekipe4", "kratko_ime", values1);
        vibriraj();
        setContentView(R.layout.edit_teams);                        
    }
}

现在我想删除我选择的一行(从列表中获取 ID 并从数据库中获取相应的 ID)或编辑一行。我该怎么做呢?如果您知道数据库中的 id,那么删除或编辑 SQLite 条目非常容易,但我不知道。

对不起我的英语,如你所见,它不是我的母语。

4

2 回答 2

0
ekipe_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            // your code here   
            }
        });

在“位置”中,您将获得您单击的项目的 ID。所以你只需要将 ListView 中的 id 转换为数据库中的 id 即可。

否则,您将需要创建自定义适配器而不是SimpleCursorAdapter,并覆盖 getView() 方法。

于 2012-08-04T22:57:17.010 回答
0

在您提到的选择查询中“SELECT * FROM ANSWER WHERE ID="

但是你必须提到“”SELECT * FROM answer WHERE ID”

因为表名是答案而不是答案

于 2013-06-28T13:49:43.817 回答