我使用了一个 sqlite 数据库来填充列表视图。
我是 java 和 android 开发的新手,我进行了搜索,但无法找到解决问题的方法。
我现在想为用户添加一个过滤文本工具,列表视图应该动态更改以仅显示与列表视图上方的编辑文本字段中键入的任何内容相匹配的项目。
我发布了确切的工作代码,没有添加任何编辑文本文本字段以防止混淆。请帮我解决这个问题。
XML(布局文件):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#125156"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_marginBottom="13sp"
android:layout_marginLeft="13sp"
android:layout_marginRight="13sp"
android:layout_marginTop="13sp"
android:layout_weight="0.25"
android:background="#FFFFFF"
android:listSelector="@drawable/yellow"
android:saveEnabled="true"
android:cacheColorHint="#FFFFFF"
android:divider="#125156"
android:dividerHeight="10.0sp"
android:fastScrollEnabled="true"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
android:textAlignment="center"
android:textColor="#FFFFFF" >
</ListView>
</LinearLayout>
对应的 Java 代码:
package com.example.uopengineering;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class PrepopSqliteDbActivity extends ListActivity
{
private static final String DB_NAME = "umangdb.db";
private SQLiteDatabase database;
private ListView listView;
private ArrayList<String> programlist;
private String query;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.programlist);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Intent intent=getIntent();
query=intent.getStringExtra(ACTIVITY_SERVICE);
overridePendingTransition(R.anim.anim_in,R.anim.anim_out);
ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
database = dbOpenHelper.openDataBase();
fillProgramList();
setUpList();
}
private void setUpList()
{
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, programlist));
listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position,long id)
{
Intent intent = new Intent(PrepopSqliteDbActivity.this, viewProgramActivity.class);
String x=(String) listView.getItemAtPosition(position);
Cursor myCursor=database.rawQuery(query+" and prog_name ="+"'"+x+"'",null);
myCursor.moveToFirst();
String y=myCursor.getString(0);
myCursor.close();
intent.putExtra("myextra", y);
startActivity(intent);
}
});
}
private void fillProgramList()
{
programlist = new ArrayList<String>();
Cursor myCursor = database.rawQuery(query+" order by prog_name",null);
myCursor.moveToFirst();
if(!myCursor.isAfterLast())
{
do
{
String programname = myCursor.getString(3);
programlist.add(programname);
}
while (myCursor.moveToNext());
}
myCursor.close();
}
@Override
public void onBackPressed()
{
super.onBackPressed();
overridePendingTransition(R.anim.anim_in2, R.anim.anim_out2);
}
}