2

大家好,我是 ANDROID 的新手,所以在使用 SQLite 数据库开发应用程序时遇到了一个问题。我从SQLite 数据库表中检索了一些数据列,将它们存储在ArrayList中并使用 ListActivity 显示它们。然后我也做了一个漂亮的列表视图过滤器。现在过滤后我调用了 onListItemClick方法并单击任何位置后,我陷入了在 listActivity 中检索已过滤列表视图的特定列表项的 rowId并将其存储在整数变量中。

任何人都可以帮助我知道这怎么可能。

有任何想法吗?我需要示例代码片段...!!

我的代码:

package com.aman.samples.dbdemo;

import java.util.ArrayList;
import com.aman.samples.dbdemo.helpers.DatabaseHelper;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Filterable;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Searching extends ListActivity {

    public static final String KEY_ROWID = "RowId";
    private final String DB_NAME="Database.sqlite"; // data base name 
    private final String TABLE_NAME="Physics";   // table name  
    private SQLiteDatabase myDataBase;
     ArrayAdapter<String> adapter;
    EditText filterText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.searching);

        final ArrayList<String> list=new ArrayList<String>();


        myDataBase=this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
        DatabaseHelper dbHelper = new DatabaseHelper(getApplicationContext());
        dbHelper.openDataBase();

        Cursor c=myDataBase.rawQuery(" select * from "+TABLE_NAME,null);
        if(c!=null)  // if c value is not null 
        {
            if(c.moveToFirst())  // movies first column 
            {
                do
                {
                    String nam=c.getString(c.getColumnIndex("Law")); // getting specific values regarding column index 
                //  int age=c.getInt(c.getColumnIndex("age"));
                    list.add(" "+nam);

                }while(c.moveToNext()); // move to next row

            }
        }


         filterText = (EditText) findViewById(R.id.search_box);
        filterText.addTextChangedListener(filterTextWatcher);


        adapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, 
                list);
        setListAdapter(adapter);


    }

    private TextWatcher filterTextWatcher = new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub

        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            adapter.getFilter().filter(s);
        }



    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        filterText.removeTextChangedListener(filterTextWatcher);
    }

    protected void onListItemClick(ListView l,View v,int position,long id){
        super.onListItemClick(l, v, position, id );





        if(position==0)
        {

                   Integer rowId = .........// I WANT A SOLUTION OR METHOD TO RETRIEVE ROW ID OF PARTICULAR LISTITEM IN LISTACTIVITY DISPLAYING DATAS FROM SQLITE DATABASE TABLE


            String s=Integer.toString(rowId);

            Intent i=new Intent(this,FinalView.class);
            i.putExtra("key1", s);
            startActivity(i);
        }
        if(position==1)
        {

         Integer rowId = .........// I WANT A SOLUTION OR METHOD TO RETRIEVE ROW ID OF PARTICULAR LISTITEM IN LISTACTIVITY DISPLAYING DATAS FROM SQLITE DATABASE TABLE

            String s=Integer.toString(rowId);

            Intent i=new Intent(this,FinalView.class);
            i.putExtra("key1", s);
            startActivity(i);
        }
        if(position==2)
        {

               Integer rowId = .........// I WANT A SOLUTION OR METHOD TO RETRIEVE ROW ID OF PARTICULAR LISTITEM IN LISTACTIVITY DISPLAYING DATAS FROM SQLITE DATABASE TABLE
            String s=Integer.toString(rowId);
            Intent i=new Intent(this,FinalView.class);
            i.putExtra("key1", s);
            startActivity(i);
        }
    }
}

提前致谢。

4

1 回答 1

2

您应该使用SimpleCursorAdapter来使用数据库中的数据填充列表。在这种情况下,onListItemClick(l, v, position, id) 的“id”参数将保存记录的 id。

于 2011-06-11T08:38:33.927 回答