使用EditText进行Android SQLite Listview数据库搜索

时间:2015-06-24 08:16:30

标签: android sqlite listview android-edittext android-filter

我有一个listview来自database,其中包含词组(英文)及其翻译(马来语)。界面类似于image below.

http://i.stack.imgur.com/LJIqY.jpg

首先,我的目标是按短语搜索listview。在搜索keyword中输入EditText时,我希望listview相应地过滤。我已经准备好了EditText

但我不知道如何开始。阅读很多教程让我很困惑。我甚至不确定我的方法是否正确。

这是活动ListGreetings.java`

ListView userList;
GreetingAdapter greetAdapter;
EditText inputSearch;
ArrayList<Frasa> frasaArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_greetings);

    //database activities
    DB db = new DB(this);

    try {
        db.create();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }

    if(db.open()) {
        //get Frasa data
        frasaArray = db.getFrasa("Greetings");


        /**
         * set item into adapter
         */
        greetAdapter = new GreetingAdapter(ListGreetings.this, R.layout.list_item_greet, frasaArray);
        userList = (ListView) findViewById(R.id.listGreetings);
        userList.setItemsCanFocus(false);
        userList.setAdapter(greetAdapter);
    }

    //inputSearch.setOnQueryTextListener(this);

        //Searching start here
        inputSearch = (EditText) findViewById(R.id.inputSearch);
        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // When user changed the Text
            }
        });
}

GreetingAdapter.java

public class GreetingAdapter extends ArrayAdapter<Frasa> {
int layoutResourceId;
Context context;
Resources resources;
ArrayList<Frasa> data = new ArrayList<Frasa>();
MediaPlayer speak;

//recorder variables
MediaPlayer playRecorder = null;
MediaRecorder recorder = null;
String mFileName = null;
final String LOG_TAG = "malay4U_Record";
boolean mStartRecording = true;
boolean mStartPlaying = true;

public GreetingAdapter(Context context, int layoutResourceId, ArrayList<Frasa> data)
{
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.resources = this.context.getResources();
    this.data = data;
    this.mFileName =  Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/audiorecordtest.3gp";

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    UserHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new UserHolder();
        holder.textPhrase = (TextView) row.findViewById(R.id.textViewPhrase);
        holder.textTranslate = (TextView) row.findViewById(R.id.textViewTranslate);
        holder.btnSpeak = (Button) row.findViewById(R.id.buttonSpeak);
        holder.btnRecord = (Button) row.findViewById(R.id.buttonRecord);
        holder.btnPlay = (Button) row.findViewById(R.id.buttonPlay);
        row.setTag(holder);
    } else {
        holder = (UserHolder) row.getTag();
    }
    final Frasa frasa = data.get(position);
    holder.textPhrase.setText(frasa.getPhrase());
    holder.textTranslate.setText(frasa.getTranslate());

    //button speak action
    holder.btnSpeak.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            String direct = frasa.getPronounce(); //audio file directory where in database stored as raw/filename

            int resID = resources.getIdentifier(direct, "String", "hairulhazri.malayforyou"); //convert string to resouce ID
            speak = MediaPlayer.create(context, resID);
            speak.start();
        }
    });

    //button Record action

    holder.btnRecord.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            onRecord(mStartRecording);
            if (mStartRecording) {
                Log.i("Record Button Clicked", "**********");
                Toast.makeText(context, "Recording....",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, "Recording finished.",
                        Toast.LENGTH_LONG).show();
            }
            mStartRecording = !mStartRecording;


        }
    });

    //button play record action
    holder.btnPlay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i("Play Button Clicked", "*******");
            onPlay(mStartPlaying);

            mStartPlaying = !mStartPlaying;
        }
    });
    return row;

}

static class UserHolder {
    SearchView inputSearch;
    TextView textPhrase;
    TextView textTranslate;
    Button btnSpeak;
    Button btnRecord;
    Button btnPlay;
}

}

在数据库中运行getFrasa

public ArrayList<Frasa> getFrasa(String situation ) {

    //ArrayList of Frasa class objects
    ArrayList<Frasa> arrFrasa = null;
    String selectQuery = "SELECT * FROM Frasa WHERE situation = " +"'"+situation+"'";

    db = SQLiteDatabase.openDatabase( DB_PATH + DB_NAME , null, SQLiteDatabase.OPEN_READWRITE);
    Cursor curFrasa = db.rawQuery(selectQuery, null);

    if (curFrasa != null && curFrasa.moveToFirst()) {
        arrFrasa = new ArrayList<Frasa>();
        while (curFrasa.isAfterLast() == false) {
            //Frasa is a class with list of fields
            Frasa fra = new Frasa();
            fra.setId(curFrasa.getInt(curFrasa.getColumnIndex(Frasa.KEY_ID)));
            fra.setPhrase(curFrasa.getString(curFrasa.getColumnIndex(Frasa.KEY_PHRASE)));
            fra.setTranslate(curFrasa.getString(curFrasa.getColumnIndex(Frasa.KEY_TRANSLATE)));
            fra.setPronounce(curFrasa.getString(curFrasa.getColumnIndex(Frasa.KEY_PRONOUNCE)));
            arrFrasa.add(fra);
            curFrasa.moveToNext();
        }
    }


    curFrasa.close();
    db.close();
    return arrFrasa;
}

我应该在哪里申请filtering?在ListGreetingsGreetingAdapter内?怎么样?

提前谢谢。

0 个答案:

没有答案