public class Moshimoshi extends Activity {
/** Called when the activity is first created. */
private ListView lv;
private EditText ed;
ArrayAdapter<String> adapterForListview;
ContentResolver cr;
private long[] item_id;
private String lv_arr[];
int textlength = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cr = getContentResolver();
lv = (ListView) findViewById(R.id.listView1);
ed = (EditText) findViewById(R.id.editText1);
adapterForListview = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1);
viewData();
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, lv_arr));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
Log.i(String.valueOf(id), String.valueOf(item_id[(int) id]));
Intent info = new Intent(getApplicationContext(), Info.class);
int rowid = (int) item_id[(int) id];
info.putExtra("key", rowid);
// update.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
//
startActivity(info);
}
});
ed.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength = ed.getText().length();
adapterForListview.clear();
for (int i = 0; i < lv_arr.length; i++) {
if (textlength <= lv_arr[i].length()) {
if (ed.getText()
.toString()
.equalsIgnoreCase(
(String) lv_arr[i].subSequence(0,
textlength))) {
adapterForListview.add(lv_arr[i]);
}
}
}
lv.setAdapter(adapterForListview);
}
});
}
public void viewData() {
// Cursor cursor = conRes.query(CONTENT_URI_Applicant, new String[] {
// _ID,
// FName, LName }, null, null, FName);
Cursor cursor = cr.query(CONTENT_URI_rawquery, null,
"Select _id,FName,LName FROM Applicant", null, null);
startManagingCursor(cursor);
while (cursor.moveToNext()) {
// Could use getColumnIndexOrThrow() to get indexes
adapterForListview.add(cursor.getString(cursor
.getColumnIndex(FName))
+ " "
+ cursor.getString(cursor.getColumnIndex(LName)));
// i++;
// Log.i("Id", String.valueOf(item_id[i]));
}
item_id = new long[cursor.getCount()];
lv_arr = new String[cursor.getCount()];
int i = 0;
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
item_id[i] = cursor.getLong(cursor.getColumnIndex(_ID));
lv_arr[i] = cursor.getString(cursor.getColumnIndex(FName)) + " "
+ cursor.getString(cursor.getColumnIndex(LName));
Log.v("ite_id:" + i, "" + item_id[i]);
i++;
}
cursor.close();
}
}
答案 0 :(得分:0)
尝试使用SimpleCursorAdapter
这样的
String[] from = new String[] { "YourDatabseColumnName" };
int[] to = new int[] {android.R.id.text1};
SimpleCursorAdapter playerAdapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_single_choice , cursor, from, to);
虽然过滤只是使用类似这样的东西
public void onTextChanged(CharSequence s, int start, int before, int count)
{
playerAdapter.getFilter().filter(s);
}
A good tutorial here关于使用sqlite和显示数据。
答案 1 :(得分:0)
也许是为了答案迟到,但它可以帮助其他人。
我遇到了同样的问题。解决方案是您还必须在进行过滤时更新列表中的项目。例如,当列表被过滤后你在ListView中得到三个项目时,你的项目列表(这里我指的是ArrayList或其他东西,而不是与ListView混淆)也应该只有三个项目,因此ID总是同步的。
我希望它有所帮助。