2017-01-10 10 views
0

URLから取得したJSONデータを検索しようとしていますが、ユーザーが検索文字列を入力するとアプリケーションがクラッシュします。どうすれば修正できますか?ここでListViewを使用してAndroidでJSONデータを検索する

は私のコードです:

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private String TAG = MainActivity.class.getSimpleName(); 

    private ProgressDialog pDialog; 
    private ListView lv; 

    private EditText filterText; 

    private ArrayAdapter<String> listAdapter; 

    // URL to get contacts JSON 
    private static String url = "http://210.213.86.195:14344/inventory/getallitemname"; 

    ArrayList<HashMap<String, String>> itemList; 

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

     filterText = (EditText)findViewById(R.id.editText); 

     itemList = new ArrayList<>(); 

     lv = (ListView) findViewById(R.id.list_item); 

     new GetItems().execute(); 

     filterText.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       MainActivity.this.listAdapter.getFilter().filter(s); 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 

     } 
     ); 
    } 

    /** 
    * Async task class to get json by making HTTP call 
    */ 
    private class GetItems extends AsyncTask<Void, Void, Void> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Showing progress dialog 
      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Please wait..."); 
      pDialog.setCancelable(false); 
      pDialog.show(); 

     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      HttpHandler sh = new HttpHandler(); 

      // Making a request to url and getting response 
      String jsonStr = sh.makeServiceCall(url); 

      Log.e(TAG, "Response from url: " + jsonStr); 

      if (jsonStr != null) { 
       try { 
        JSONObject jsonObj = new JSONObject(jsonStr); 

        // Getting JSON Array node 
        JSONArray items = jsonObj.getJSONArray("Data"); 

        // looping through All Contacts 
        for (int i = 0; i < items.length(); i++) { 
         JSONObject c = items.getJSONObject(i); 

         String ItemCode = c.getString("ItemCode"); 
         String ItemName = c.getString("ItemName"); 

         // tmp hash map for single item 
         HashMap<String, String> item = new HashMap<>(); 

         // adding each child node to HashMap key => value 
         item.put("ItemCode", ItemCode); 
         item.put("ItemName", ItemName); 

         // adding item to item list 
         itemList.add(item); 
        } 

       } catch (final JSONException e) { 
        Log.e(TAG, "Json parsing error: " + e.getMessage()); 
        runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Toast.makeText(getApplicationContext(), 
            "Json parsing error: " + e.getMessage(), 
            Toast.LENGTH_LONG) 
            .show(); 
         } 
        }); 

       } 
      } else { 
       Log.e(TAG, "Couldn't get json from server."); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), 
           "Couldn't get json from server. Check LogCat for possible errors!", 
           Toast.LENGTH_LONG) 
           .show(); 
        } 
       }); 

      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      // Dismiss the progress dialog 
      if (pDialog.isShowing()) 
       pDialog.dismiss(); 
      /** 
      * Updating parsed JSON data into ListView 
      * */ 
      ListAdapter adapter = new SimpleAdapter(
        MainActivity.this, itemList, 
        R.layout.list_item, new String[]{"ItemCode", "ItemName"}, new int[]{R.id.ItemCode, 
        R.id.ItemName}); 

      lv.setAdapter(adapter); 
     } 

    } 

} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.jsonparser.MainActivity"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <ListView 
     android:id="@+id/list_item" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="10dp" 
     android:layout_below="@+id/editText" 
     android:layout_alignLeft="@+id/editText" 
     android:layout_alignStart="@+id/editText" /> 
</RelativeLayout> 

list_item.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="wrap_content" 
    android:orientation="vertical" 
    android:padding="@dimen/activity_horizontal_margin"> 

    <TextView 
     android:id="@+id/ItemName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="2dip" 
     android:paddingTop="6dip" 
     android:textColor="@color/colorPrimaryDark" 
     android:textSize="16sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/ItemCode" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="2dip" 
     android:textColor="@color/colorAccent" /> 
</LinearLayout> 
+0

ますonTextChangedのeditText値を渡さなければならない場合は、このリンクを参照してください。フィルタの十分な数 [リンク](http://www.androidbegin.com/tutorial/android-search-listview-using-filter/) 希望すると助かります –

+0

ログcat –

答えて

0

どこでもコードでそれをinitalizedしていない: -

MainActivity.this.listAdapter 

ので、デフォルトでは、ローカルアダプタ変数を使用しているpostExecuteで、nullです。

0

adapter = new ArrayAdapter<String>(this, 
        R.layout.list_view_rows, R.id.listview, ListofStringYouWantToDisplay); 

lv = (ListView) findViewById(R.id.list_item); 

以下聞くを置く - ListofStringYouWantToDisplayがString []であるか、使用前に

あなたの問題は、あなたが初期化されていないアダプタを表示したいのArrayList

関連する問題