How to retrieve rowId of a particular listitem of filtered listview in listActivity displaying SQlite data?
Hello Everyone I am new being in ANDROID so I am getting one problem during developing an app using SQLite database. I retrieved some data columns from SQLite database table, stored them in an ArrayList and displayed them using ListActivity. Then I made a nice looking listview filter also. Now after filtering I called onListItemClick method and after clicking any position, I am getting stuck in retrieving rowId of a particular listitem of filtered listview in listActivity and to store it in an integer variable.
Can anyone HELP me to know how it is possible.
Any Ideas? I need example code snippets...!!
My Code:
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 = ........./开发者_JAVA技巧/ 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);
}
}
}
Thanks in Advance.
You should use SimpleCursorAdapter in order to populate list with data from DB. In that case "id" parameter of onListItemClick(l, v, position, id ) will hold id of record.
精彩评论