2013-10-05 3 views
1

localhost에서 json 데이터로 목록보기를 업데이트 중입니다. 모든 데이터가 서버에서 제공된다는 것을 잘 보여줍니다. 이제이 활동에 검색 방법을 추가하려고합니다. 일부 코드를 사용해 보았지만 오류를 발생시키는 방법을 모릅니다. 검색 방법을 적용하는 데 도움주세요.안드로이드 ListView 검색

/* Background Async Task to Load all person data by making HTTP Request 
*/ 
class LoadPersonData extends AsyncTask<String, String, String> { 

protected String doInBackground(String... args) { 

       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       // getting JSON string from URL 
       JSONObject json = jParser.makeHttpRequest(url, "GET", params); 


try { 
        // Checking for SUCCESS TAG 
        int success = json.getInt(TAG_SUCCESS); 

        if (success == 1) { 

         person = json.getJSONArray(TAG_PRODUCTS); 


         for (int i = 0; i < person.length(); i++) { 
          JSONObject c = person.getJSONObject(i); 

          // Storing each one in variable 
          String mp_id = c.getString(TAG_PID); 
          String mp_name = c.getString(TAG_NAME); 
          String mp_gender = c.getString(TAG_GENDER); 

          String mp_age = c.getString(TAG_REPAGE); 
          String repoter_name = c.getString(TAG_REPNAME); 
          String repoter_contact = c.getString(TAG_REPPHONE); 

          // creating HashMap 
          HashMap<String, String> map = new HashMap<String, String>(); 

          // adding each child node to HashMap key => value 
          map.put(TAG_PID, mp_id); 
          map.put(TAG_NAME, "Name: "+ mp_name); 
          map.put(TAG_GENDER, "Gender: "+ mp_gender); 
          map.put(TAG_REPAGE, "Age: "+ mp_age); 
          map.put(TAG_REPNAME, "Report Person: "+ repoter_name); 
          map.put(TAG_REPPHONE, "Reprt Person#: "+ repoter_contact); 

          // adding HashList to ArrayList 
          personList.add(map); 
         } 
        } else { 
         // no person from json 
         // Launch Add New product Activity 
         Intent i = new Intent(getApplicationContext(), 
           AddNewPerson.class); 
         // Closing all previous activities 
         i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
         startActivity(i); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
    return null 
} 


protected void onPostExecute(String file_url) { 

       // updating UI from Background Thread 
       runOnUiThread(new Runnable() { 
        public void run() { 
         /** 
         * Updating parsed JSON data into ListView 
         * */ 
         ListAdapter adapter = new SimpleAdapter(
           AllFoundPerson.this, productsList, 
           R.layout.list_item_found, new String[] { TAG_PID, 
             TAG_NAME, TAG_GENDER, TAG_REPAGE, TAG_REPNAME, TAG_REPPHONE}, 
           new int[] { R.id.fpid, R.id.namefound, R.id.genderfound,R.id.agefound, R.id.repoter_found, R.id.repoterphone_found }); 


setListAdapter(adapter); 

// 내 코드 HERE TILL 잘 작동하지만있어서 APP 아래를 추가 할 때 여기

// Applying Search method 



         inputSearch.addTextChangedListener(new TextWatcher() { 

          @Override 
          public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
           // When user changed the Text 
           AllFoundPerson.this.adapter.getFilter().filter(cs); // Error in adapter, adator is not a field 

          } 

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

          } 

          @Override 
          public void afterTextChanged(Editable arg0) { 
           // TODO Auto-generated method stub       
          } 
         }); 


       } 
      }); 

     } 

    } 
} 

이 필드에 오류가 AllFoundPerson.this.adapter.getFilter().filter(cs); 어댑터가 해결 될 수있다 CRASHED 또는

피사계하지 IS

list_item_found.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="match_parent" 
    android:orientation="vertical" > 


    <TextView 
     android:id="@+id/fpid" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:visibility="gone"/> 



    <TextView 
     android:id="@+id/namefound" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="6dip" 
     android:paddingLeft="6dp" 
     android:textSize="17sp" 
     android:textColor="#FF00FF" 
     android:textStyle="bold" /> 

     <TextView 
     android:id="@+id/genderfound" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="6dp" 
     android:paddingLeft="6dp" 

     android:textSize="17sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/agefound" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="6dip" 
     android:paddingLeft="6dp" 

     android:textSize="17sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/repoter_found" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="6dp" 
     android:paddingLeft="6dp" 

     android:textSize="17sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/repoterphone_found" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="6dp" 
     android:paddingLeft="6dp" 

     android:textSize="17sp" 
     android:textStyle="bold" /> 

</LinearLayout> 

all_found_person.xm L

<?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:orientation="vertical" > 

    <!-- Editext for Search --> 
    <EditText android:id="@+id/inputSearch" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="Enter a name to Search.." 
     android:inputType="textVisiblePassword"/> 
    <!-- List View --> 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

편집 : 내가 오류가 간 클래스 멤버 ArrayAdapter<String> adapter;으로 어댑터를 추가 한 후

하지만 난 응용 프로그램을 실행할 때 나는 목록을 볼 수 있지만 검색 상자에 어떤 키를 입력 할 때 응용 프로그램은 그냥 여기에 추락 내 logcat 정보입니다.

enter image description here

난 내 코드에 약간의 변화를 수행 한 솔루션에 대한 너무 많은 검색 후
10-06 13:47:30.880: D/AndroidRuntime(4069): Shutting down VM 
10-06 13:47:30.880: W/dalvikvm(4069): threadid=1: thread exiting with uncaught exception (group=0xb3fcd4f0) 
10-06 13:47:30.880: D/AndroidRuntime(4069): procName from cmdline: com.DRMS.disas_recovery 
10-06 13:47:30.880: E/AndroidRuntime(4069): in writeCrashedAppName, pkgName :com.DRMS.disas_recovery 
10-06 13:47:30.880: D/AndroidRuntime(4069): file written successfully with content: com.DRMS.disas_recovery StringBuffer : ;com.DRMS.disas_recovery 
+1

이'onPostExecute'는 UI에 호출하는이 runOnUiThread''에 대한 필요를 스레드 없다 변경 후 onpostExcute이 ((Filterable) adapter).getFilter().filter(cs);

검색 방법을 사용 – Raghunandan

+0

@Raghunandan 왜이 줄에 어댑터 오류가 발생하는지 말해주세요. 'AllFoundPerson.this.adapter.getFilter(). filter (cs);'오류 : 어댑터를 해결할 수 없거나 필드가 없습니다. 내가 뭘 더해야하니? 이 튜토리얼이나 코드는 무엇입니까? –

+0

어댑터가 정적 필드가 아닙니다. 어댑터를 클래스 멤버로 선언하십시오 – Raghunandan

답변

0

. 최종 final ListAdapter adapter = new SimpleAdapter()에 어댑터의 수정을 변경하고 대신 AllFoundPerson.this.adapter.getFilter().filter(cs);의 난

inputSearch.addTextChangedListener(new TextWatcher() { 

          @Override 
          public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { 
           // When user changed the Text 
           ((Filterable) adapter).getFilter().filter(cs); 
          } 

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

          } 

          @Override 
          public void afterTextChanged(Editable arg0) { 
           // TODO Auto-generated method stub       
          } 
         });