마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

리스트뷰 검색기능 도와주세요

0 추천
public class CalDicActivity extends Activity {
 private static String DB_PATH = "/sdcard/";
    private static String DB_NAME = "testjye.sqlite";
    private int listcount = 0;
 private String[] foodsList = null; //food배열
 private String[] calList = null;     //kcal배열
 private String[] gramList = null;  //gram배열
 ArrayAdapter<String> arrayAdapter;
 private ArrayList<String> listString;
 private ArrayList<String> listString2;
 private EditText input; //검색창
 private String Error = null;  
 private String stredit; 
 
   ....

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.caldic); 
        
         input=(EditText)findViewById(R.id.input);
       //  input.addTextChangedListener(textWatcherInput);//텍스트가 변할때 이벤트
         stredit = input.getText().toString(); //키
    
        try {
   createDataBase();
  } catch (IOException ioe) {
   Toast.makeText(this, "DB 파일을 생성할 수 없습니다.", Toast.LENGTH_LONG).show();
  }
  
        listcount = 0;
     try { 
      Cursor cursor;
      SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH+DB_NAME, null, 1);//sqlite도우미로 db선언하고 
      String[] FROM = { "*"   };  //foodcala테이블의 컬럼선택
      cursor = db.query("foodcala", FROM, null, null, null, null, null);
      startManagingCursor(cursor);
      
      foodsList = new String[cursor.getCount()]; //배열초기화
         calList = new String[cursor.getCount()];
         gramList = new String[cursor.getCount()];
         
      while(cursor.moveToNext()) //배열에 저장
   {
       String foods = cursor.getString(1); 
       String cal = cursor.getString(2);
       String gram = cursor.getString(3);
       
       foodsList[listcount] = foods;
       calList[listcount] = cal; 
       gramList[listcount] = gram;

       listcount++;
   }
       if(db != null)
       db.close();
          } catch (Exception e) { 
      Toast.makeText(this, "ERROR IN CODE:"+e.toString(), Toast.LENGTH_LONG).show();
     }     
     if (listcount > 0)
     {
       ArrayList<String> listString = new ArrayList<String>(); //문재배열초기화
       ArrayList<String> listString2 = new ArrayList<String>(); //문재배열초기화
       
      for (int i = 0; i < foodsList.length; i++) //list수만큼대입
      {
       listString.add(foodsList[i]+"     "+calList[i]+"kcal");//+"     ("+gramList[i]+"gram)");
       
       if(foodsList[i]==stredit){ //검색어와 foods 이름이 같으면 listString2에도 배열저장
     listString2.add(foodsList[i] + calList[i]);        
       }
      }
         ListView listView = (ListView)findViewById(R.id.list); //리스트뷰에연결
         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getBaseContext(),
       android.R.layout.simple_list_item_1, listString);
        listView.setAdapter(arrayAdapter); 
      
     }
     input.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) {
        if(s.length()==0)
        {
         ListView listView = (ListView)findViewById(R.id.list); //리스트뷰에연결
         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getBaseContext(),
            android.R.layout.simple_list_item_1, listString);
             listView.setAdapter(arrayAdapter); 
        }else
        {
           ListView listView = (ListView)findViewById(R.id.list); //리스트뷰에연결
           ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getBaseContext(),
               android.R.layout.simple_list_item_1, listString2);
                listView.setAdapter(arrayAdapter);
          }
       }
   });
        
        if (Error != null) {   
            Toast.makeText(CalDicActivity.this, Error, Toast.LENGTH_LONG).show();   
        } else {   
            //Toast.makeText(parent, "Source: " + Content, Toast.LENGTH_LONG).show();   
        }   
    }   
}

외부 디비를 리스트뷰로 뿌려주고 검색하는 기능을 구현하려고 합니다.

우선 액티비티를 키면unregisterIRListener() 가 뜨고, edittext를 건드리면 오류를 뿜으며 죽습니다..ㅜㅜ

어떻게 수정하면 될까요?? 도와주세요ㅜㅜ

익명사용자 님이 2014년 11월 15일 질문

1개의 답변

0 추천
혹시 logcat을 올려주실수있나요?
Djleeee (13,180 포인트) 님이 2014년 11월 15일 답변
...