1

这是我的搜索活动代码

search.addTextChangedListener(new TextWatcher()
  {
  public void afterTextChanged(Editable s)
  {
                                                                  // Abstract Method of TextWatcher Interface.
  }
  public void beforeTextChanged(CharSequence s,
  int start, int count, int after)
  {
  // Abstract Method of TextWatcher Interface.
  }
  public void onTextChanged(CharSequence s,
  int start, int before, int count)
  {
  textlength = search.getText().length();
  array_sort.clear();
  for (int i = 0; i < listview_array.length; i++)
  {
  if (textlength <= listview_array[i].length())
  {
  if(search.getText().toString().equalsIgnoreCase(
  (String)
  listview_array[i].subSequence(0,
  textlength)))
  {
      array_sort.add(listview_array[i]);
      }
   }}

  listContent1.setAdapter(new ArrayAdapter<String>
  (Search_Food.this,
  android.R.layout.simple_list_item_1, array_sort));
  }

  });

我的 onclicklistener 代码

 AlertDialog.Builder adb = new AlertDialog.Builder(
                 Search_Food.this);
          adb.setTitle("Food Item");
                  adb.setMessage("Selected Food is = "
                  + listContent1.getItemAtPosition(position));
                  adb.setPositiveButton("Ok", null);
                  adb.show();

我的数据库适配器

public LinkedList<String> search(String string) {
    // TODO Auto-generated method stub
    LinkedList<String> results = new LinkedList<String>();
    Cursor cursor = null;
    String search = string;
    try{
        cursor = this.sqLiteDatabase.query(true, MYDATABASE_TABLE , new String[] { KEY_ID , KEY_FOODNAME , KEY_CALORIES  }, KEY_FOODNAME  + " = ?" + "COLLATE NOCASE", 
                new String[] { search }, null, null, null, null);

        if(cursor!=null && cursor.getCount()>0 && cursor.moveToFirst()){

            int foodName = cursor.getColumnIndex(KEY_FOODNAME );
            int keyColories = cursor.getColumnIndex(KEY_CALORIES );

           // boolean moveToNext = cursor.moveToNext();
            //do{
                results.add(
                    new String(

                       cursor.getString(foodName) + "                              " +
                        cursor.getString(keyColories)
                    )
                );
            //}while(moveToNext);
        }
    }catch(Exception e){
        Log.e(APP_NAME, "An error occurred while searching for "+search+": "+e.toString(), e);
    }finally{
        if(cursor!=null && !cursor.isClosed()){
            cursor.close();
        }
    }

    return results;
}

现在,当我在搜索的列表视图上单击数据时,我想将 getitemposition 提取到 2 中,这就是我的列。食物名称和卡路里。我怎样才能分开这两个值。

请回答。帮助。谢谢你

4

0 回答 0