0

リストビューの値をクリックして新しいxmlレイアウトに移動しようとしていますが、問題はこのエラーが発生することです。

私のlogcatから私はこれしか理解できません

03-24 11:37:20.990: E/AndroidRuntime(793):  at me.mojica.caloriewatch.Search$1.onItemClick(Search.java:261)

これです

    Cursor cursor = (Cursor) parent.getItemAtPosition(position);

これは完全なlogcatです。

03-24 11:37:20.990: E/AndroidRuntime(793): FATAL EXCEPTION: main
03-24 11:37:20.990: E/AndroidRuntime(793): java.lang.ClassCastException: java.lang.String cannot be cast to android.database.Cursor
03-24 11:37:20.990: E/AndroidRuntime(793):  at me.mojica.caloriewatch.Search$1.onItemClick(Search.java:261)
03-24 11:37:20.990: E/AndroidRuntime(793):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-24 11:37:20.990: E/AndroidRuntime(793):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
03-24 11:37:20.990: E/AndroidRuntime(793):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
03-24 11:37:20.990: E/AndroidRuntime(793):  at android.widget.AbsListView$1.run(AbsListView.java:3423)
03-24 11:37:20.990: E/AndroidRuntime(793):  at android.os.Handler.handleCallback(Handler.java:725)
03-24 11:37:20.990: E/AndroidRuntime(793):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-24 11:37:20.990: E/AndroidRuntime(793):  at android.os.Looper.loop(Looper.java:137)
03-24 11:37:20.990: E/AndroidRuntime(793):  at android.app.ActivityThread.main(ActivityThread.java:5039)
03-24 11:37:20.990: E/AndroidRuntime(793):  at java.lang.reflect.Method.invokeNative(Native Method)
03-24 11:37:20.990: E/AndroidRuntime(793):  at java.lang.reflect.Method.invoke(Method.java:511)
03-24 11:37:20.990: E/AndroidRuntime(793):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-24 11:37:20.990: E/AndroidRuntime(793):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-24 11:37:20.990: E/AndroidRuntime(793):  at dalvik.system.NativeStart.main(Native Method)

そして私のsearch.javaのために

package me.mojica.caloriewatch;


import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AlphabetIndexer;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SectionIndexer;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ArrayAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.content.DialogInterface.OnClickListener;

public class Search extends Activity implements OnClickListener {



   int arlene = 0;
   int textlength = 0;

   ListView listContent1, transact;
   EditText search, food, calories;
   Button FSearch;
   TextView result;

   SimpleCursorAdapter cursorAdapter, cursorAdap;
   Cursor cursor;
   private String[] listview_array;
   private ArrayList<String> array_sort = new ArrayList<String>();
   private SQLiteAdapter mySQLiteAdapter;

   /*   Called when the activity is first created.  */


   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.search);
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);



  listContent1 = (ListView) findViewById(R.id.lFoodlist1);
  listContent1.setFastScrollEnabled(true); //must be enabled
  // transact = (ListView)findViewById(R.id.transact);
  search = (EditText) findViewById(R.id.eFsearch);
  food = (EditText) findViewById(R.id.editText1);
  calories = (EditText) findViewById(R.id.editText2);
  FSearch = (Button) findViewById(R.id.bFsearch);
  result = (TextView) findViewById(R.id.textView3);

  /*
     Create/Open a SQLite database
     and fill with dummy content
     and close it
    */
  mySQLiteAdapter = new SQLiteAdapter(this);
  mySQLiteAdapter.openToWrite();
  mySQLiteAdapter.deleteAll();

  mySQLiteAdapter.insert("Adobong Baboy (1 bowl)", "339");
  mySQLiteAdapter.insert("Adobong Kangkong ( 1 bowl)", "84");
  mySQLiteAdapter.insert("Adobong Manok (1 bowl) ", "624");
  mySQLiteAdapter.insert("Adobong Pusit (1 bowl)", "121");
  mySQLiteAdapter.insert("Arroz Caldo (1 bowl)", "500");
  mySQLiteAdapter.insert("Chicken Afritada (1 bowl)", "323");
  mySQLiteAdapter.insert("Chicken Asado (1 bowl)", "205");
  mySQLiteAdapter.insert("Chicken Empanada (1 bowl)", "150");
  mySQLiteAdapter.insert("Chicken Tinola (1 bowl)", "267");
  mySQLiteAdapter.insert("Dinuguan (1 bowl)", "255");
  mySQLiteAdapter.insert("Fried Bangus (1 pc)", "162");
  mySQLiteAdapter.insert("Ginataang Kalabasa (1 bowl)", "225");
  mySQLiteAdapter.insert("Ginataang Langka (1 bowl)", "167");
  mySQLiteAdapter.insert("Ginataang Pinakbet (1 bowl)", "155");
  mySQLiteAdapter.insert("Igado (1 bowl)", "297");
  mySQLiteAdapter.insert("Kare Kare (1 bowl)", "129");
  mySQLiteAdapter.insert("Laing (1 bowl)", "202");
  mySQLiteAdapter.insert("Lechon Kawali (1 bowl)", "245");
  mySQLiteAdapter.insert("Pakbet (1 bowl)", "233");
  mySQLiteAdapter.insert("Paksiw na Isda (1 bowl)", "73");
  mySQLiteAdapter.insert("Paksiw na Pata (1 bowl)", "342");
  mySQLiteAdapter.insert("Pinakbet (1 bowl)", "110");
  mySQLiteAdapter.insert("Pork Afritada (1 bowl)", "402");
  mySQLiteAdapter.insert("Pork Asado (1 bowl)", "533");
  mySQLiteAdapter.insert("Pork Barbaque on Stick (1 stick)", "111");
  mySQLiteAdapter.insert("Pork Embutido (1 bowl)", "350");
  mySQLiteAdapter.insert("Pork Mechado (1 bowl)", "157");
  mySQLiteAdapter.insert("Pork Nilaga (1 bowl)", "181");
  mySQLiteAdapter.insert("Pork Pochero (1 bowl)", "798");
  mySQLiteAdapter.insert("Pork Sinigang (1 bowl)", "290");
  mySQLiteAdapter.insert("Relyenong Bangus (1 bowl)", "390");
  mySQLiteAdapter.insert("Salmon Sinigang (1 bowl)", "190");
  mySQLiteAdapter.insert("Sisig (1 bowl)", "293");
  mySQLiteAdapter.insert("Tilapia (1 pc)", "100");
  mySQLiteAdapter.insert("Tinolang Manok (1 bowl)", "843");
  mySQLiteAdapter.insert("Tortang Talong (1 pc)", "291");
  mySQLiteAdapter.insert("Balut (1 egg)", "188");
  mySQLiteAdapter.insert("Beefsteak (1 bowl)", "210");
  mySQLiteAdapter.insert("Bibingka", "100");
  mySQLiteAdapter.insert("Biko (1 slice)", "100");
  mySQLiteAdapter.insert("Buko Pie (1 slice)", "145");
  mySQLiteAdapter.insert("Cassava Cake (1 slice)", "100");
  mySQLiteAdapter.insert("Chicsilog (1 meal)", "284");
  mySQLiteAdapter.insert("Coorned Beef (1 meal)", "267");
  mySQLiteAdapter.insert("Danggit (1 pc)","98");
  mySQLiteAdapter.insert("Ice Cream", "157");
  mySQLiteAdapter.insert("Embotido", "187");
  mySQLiteAdapter.insert("Ensaymada", "410");
  mySQLiteAdapter.insert("Espasol", "50");
  mySQLiteAdapter.insert("Fishball (1 pc)", "9");
  mySQLiteAdapter.insert("Fruit Salad (1 bowl)", "211");
  mySQLiteAdapter.insert("Galapong (1 slice)", "100");
  mySQLiteAdapter.insert("Ginataan (1 bowl)", "167");
  mySQLiteAdapter.insert("Halaya", "70");
  mySQLiteAdapter.insert("Halo halo (1 cup)", "80");
  mySQLiteAdapter.insert("Hotdog (1 pc)", "183");
  mySQLiteAdapter.insert("Kutsinta", "100");
  mySQLiteAdapter.insert("Leche Flan", "228");
  mySQLiteAdapter.insert("Lomi", "222");
  mySQLiteAdapter.insert("Longganisa (1 pc)", "170");
  mySQLiteAdapter.insert("Lumpiang Shanghai (1 pc)", "169");
  mySQLiteAdapter.insert("Palabok", "305");
  mySQLiteAdapter.insert("Palitaw", "100");
  mySQLiteAdapter.insert("Pancit", "416");
  mySQLiteAdapter.insert("Pansit Palabok", "305");
  mySQLiteAdapter.insert("Pastillas (1 pc)", "76");
  mySQLiteAdapter.insert("Porksilog (1 meal)", "555");
  mySQLiteAdapter.insert("Pulvoron (1 pc)", "40");
  mySQLiteAdapter.insert("Puto (1 pc)", "112");
  mySQLiteAdapter.insert("Puto Bumbong (1 pc)", "50");
  mySQLiteAdapter.insert("Puto Pao (1 bun)", "120");
  mySQLiteAdapter.insert("Qwek qwek (1 pc)", "14");
  mySQLiteAdapter.insert("Saging na Saba", "122");
  mySQLiteAdapter.insert("Sapin sapin (1 slice)", "100" );
  mySQLiteAdapter.insert("Scrambled Egg", "100");
  mySQLiteAdapter.insert("Sinangag (1 cup)", "300");
  mySQLiteAdapter.insert("Siopao", "240");
  mySQLiteAdapter.insert("Suman (1 pc )", "31");
  mySQLiteAdapter.insert("Taho (1 cup)", "180");
  mySQLiteAdapter.insert("Tapsilog (1 meal)", "364");
  mySQLiteAdapter.insert("Tikoy (1 slice)", "100");
  mySQLiteAdapter.insert("Tuyo (1 pc)", "57");
  mySQLiteAdapter.insert("Yema (1 pc)", "20");

  mySQLiteAdapter.insert("Apple", "80");
  mySQLiteAdapter.insert("Apricots", "17");
  mySQLiteAdapter.insert("Avocado", "30");
  mySQLiteAdapter.insert("Banana", "105");
  mySQLiteAdapter.insert("Blueberries", "82");
  mySQLiteAdapter.insert("Cherry", "4");
  mySQLiteAdapter.insert("Coconut", "159");
  mySQLiteAdapter.insert("Eggplant", "132");
  mySQLiteAdapter.insert("Grape Fruit", "62");
  mySQLiteAdapter.insert("Guava", "61");
  mySQLiteAdapter.insert("Kiwi Fruit", "25");
  mySQLiteAdapter.insert("Lemon", "24");
  mySQLiteAdapter.insert("Limes", "20");
  mySQLiteAdapter.insert("Lychee", "4");
  mySQLiteAdapter.insert("Mango", "107");
  mySQLiteAdapter.insert("Melon", "188");
  mySQLiteAdapter.insert("Orange", "62");
  mySQLiteAdapter.insert("Peach", "38");
  mySQLiteAdapter.insert("Pineapple (1 slice)", "42");
  mySQLiteAdapter.insert("Pomelo (per section)", "72");
  mySQLiteAdapter.insert("Rambutan", "7");
  mySQLiteAdapter.insert("Raspberries", "82");
  mySQLiteAdapter.insert("Strawberry", "8");
  mySQLiteAdapter.insert("Tomato", "15");
  mySQLiteAdapter.insert("Watermelon (per slice)", "46");


  mySQLiteAdapter.close();

  /* 
     Open the same SQLite database
     and read all it's content.
    */
  mySQLiteAdapter = new SQLiteAdapter(this);
  mySQLiteAdapter.openToRead();
  listview_array = mySQLiteAdapter.qall();

  listContent1.setAdapter(new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, listview_array));
  listContent1.setOnItemClickListener(listContentOnItemClickListener); 
  result.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        // Toast.makeText(Search.this, Toast.LENGTH_LONG).show();      
    }

  });

  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.this,
  android.R.layout.simple_list_item_1, array_sort));
  }

  });

  Cursor cursor = mySQLiteAdapter.queueAll();
  startManagingCursor(cursor);

  String[] from = new String[] { SQLiteAdapter.KEY_FOODNAME, SQLiteAdapter.KEY_CALORIES };
  int[] to = new int[] { R.id.tv1, R.id.tv2 };

  SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from,
        to);

      listContent1.setAdapter(cursorAdapter);
      listContent1.setOnItemClickListener(listContentOnItemClickListener);
   }


   private ListView.OnItemClickListener listContentOnItemClickListener = new     ListView.OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         Cursor cursor = (Cursor) parent.getItemAtPosition(position);

     String item_content1 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_FOODNAME));
     String item_content2 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CALORIES));

     arlene = arlene + Integer.parseInt(item_content2);
     String item = String.valueOf(" Food Name: " + item_content1) + "\n" + " Calories: "
           + item_content2;

     Toast.makeText(Search.this, item, Toast.LENGTH_LONG).show();

     food.setText(item_content1);
     calories.setText(String.valueOf(arlene));
     result.setText(String.valueOf(arlene));

     // define a key to store value into sharedpreferences
     String key = food.getText().toString() + ":" + calories.getText().toString();

     // here i assume that the combinasion food:calories are unique
     SavePreferences(key, key); 

     /*
        mySQLiteAdapter.openToWrite();
        String data1 = food.getText().toString();
        String data2 = calories.getText().toString();
        mySQLiteAdapter.insertFood(data1, data2);
        updateList();
       */
     SavePreferences("computed", result.getText().toString());
  }
   };

   protected void SavePreferences(String key, String value) {
  // TODO Auto-generated method stub
  SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
  SharedPreferences.Editor editor = sharedPreferences.edit();
  editor.putString(key, value);
  editor.commit();

   }

       protected void LoadPreferences() {
      // // TODO Auto-generated method stub
       SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
      String strSavedMem1 = sharedPreferences.getString("computed", "");
      // String strSavedMem2 = sharedPreferences.getString("calories", "");
      //
       result.setText(strSavedMem1);
       }
      // calories.setText(strSavedMem2);
      // }

class MyCursorAdapter extends SimpleCursorAdapter implements SectionIndexer{

    AlphabetIndexer alphaIndexer;

            public MyCursorAdapter(Context context, int layout, Cursor c,
                            String[] from, int[] to) {
                    super(context, layout, c, from, to);

   alphaIndexer=new AlphabetIndexer(c, cursor.getColumnIndex(SQLiteAdapter.KEY_FOODNAME), "        ABCDEFGHIJKLMNOPQRSTUVWXYZ");

            }
   // you have just to instanciate the indexer class like this

   //cursor,index of the sorted column,a string representing the alphabet (pay attention on the        blank char at the beginning of the sequence)

   @Override

   public int getPositionForSection(int section) {                

           return alphaIndexer.getPositionForSection(section); //use the indexer

   }



   @Override

   public int getSectionForPosition(int position) {                       

    return alphaIndexer.getSectionForPosition(position); //use the indexer

   }



   @Override

   public Object[] getSections() {

           return alphaIndexer.getSections(); //use the indexer

   }

            }

      protected void onDestroy() {

         // TODO Auto-generated method stub

         super.onDestroy();

         mySQLiteAdapter.close();

      }

      private void updateList() {
         // TODO Auto-generated method stub
         cursor.requery();
      }

   @Override
   public void onClick(DialogInterface arg0, int arg1) {
       // TODO Auto-generated method stub

   }

   }
4

1 に答える 1

0

問題はonTextChangedです:あなたは使用する必要があります/持っている必要があります:cursorAdapter.getFilter().filter(s.toString());

検索エディットテキストの入力に使用する文字列をクエリし、リストビューを使用してアイテムの位置を取得し、onItemClickでカーソルを取得することをお勧めしますCursor cursor = (Cursor) listContent1.getItemAtPosition(position);

于 2013-03-24T16:53:53.343 に答える