2017-06-30 12 views
1

私のアプリはすべての画像をListViewに表示しています。それは速かったが、私はonCreateOptionsMenuを追加した。それからアプリは非常に遅く、1つの画像しか表示しません。これはスレッドと関係がありますか?onCreateOptionsMenuを使用してAndroidアプリの速度が低下しています

これはMainActivityのための私のコードです:

public class MainActivity extends AppCompatActivity { 

    static ArrayList<Product> arrayList; 
    ListView lv; 

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

     arrayList = new ArrayList<>(); 
     lv = (ListView) findViewById(R.id.listView); 

     runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      new ReadJSON().execute("http://10.0.3.2:5000/products/id/6"); 
      } 
     }); 
    } 

    class ReadJSON extends AsyncTask<String, Integer, String> { 

     @Override 
     protected String doInBackground(String... params) { 
      return readURL(params[0]); 
     } 

     @Override 
     protected void onPostExecute(String content) { 
      try { 
       JSONObject jsonObject = new JSONObject(content); 
       JSONArray jsonArray = jsonObject.getJSONArray("products"); 

       for(int i =0;i<jsonArray.length(); i++){ 
        JSONObject productObject = jsonArray.getJSONObject(i); 
        arrayList.add(new Product(
          productObject.getString("image"), 
          productObject.getString("title"), 
          productObject.getString("price"), 
          productObject.getString("description") 
       )); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      CustomListAdapter adapter = new CustomListAdapter(
        getApplicationContext(), R.layout.custom_list_layout, arrayList 
     ); 
      lv.setAdapter(adapter); 

      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { // list item click opens a new detailed activity 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        Product product = arrayList.get(position); // getting the model 
        Intent intent = new Intent(MainActivity.this, DetailActivity.class); 
        intent.putExtra("title", product.getName()); 
        intent.putExtra("price", product.getPrice()); 
        intent.putExtra("img", product.getImage()); 
        intent.putExtra("description", product.getDescription()); 
        intent.putExtra(ShoppingCartHelper.PRODUCT_INDEX, position); 
        startActivity(intent); 
       } 
      }); 
     } 
    } 

    private static String readURL(String theUrl) { 
     StringBuilder content = new StringBuilder(); 
     try { 
      // create a url object 
      URL url = new URL(theUrl); 
      // create a urlconnection object 
      URLConnection urlConnection = url.openConnection(); 
      // wrap the urlconnection in a bufferedreader 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
      String line; 
      // read from the urlconnection via the bufferedreader 
      while ((line = bufferedReader.readLine()) != null) { 
       content.append(line + "\n"); 
      } 
      bufferedReader.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return content.toString(); 
    } 

    public static ArrayList<Product> getProduct(Resources res) { 
     return arrayList; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu, menu); 

     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if(id == R.id.shoppingcart) { 
      Intent viewShoppingCartIntent = new Intent(getBaseContext(), ShoppingCartActivity.class); 
      startActivity(viewShoppingCartIntent); 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

} 

EDIT 1:メニューリソースファイル:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

<item 
    android:id="@+id/shoppingcart" 
    android:icon="@drawable/shop_1600" 
    android:title="Item" 
    app:showAsAction="always" /> 
</menu> 

EDIT 2:これは、問題が発生する最小限の例です。 。

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    } 

    public boolean onCreateOptionsMenu(Menu menu) { 

    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.menu, menu); 

    return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if(id == R.id.shoppingcart) { 
     Intent viewShoppingCartIntent = new Intent(getBaseContext(), ShoppingCartActivity.class); 
     startActivity(viewShoppingCartIntent); 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
    } 
} 
+1

runOnUiThread内でAsyncタスクを実行することはお勧めできません。非同期タスク自体は、どのコードが別々のスレッドで実行され、UIスレッド上で実行されているかを既に処理しています。 –

答えて

1

onCreateOptionsMenuで重労働をしていないので、そこに問題はありません。

AsyncTaskrunOnUiThreadの内部で実行することはお勧めできません。 AsyncTaskは、どのコードが別のスレッドで実行され、UIスレッドで実行されているかを既に処理しています。ちょうどこのようonCreate内部

コールは:

new ReadJSON().execute("http://10.0.3.2:5000/products/all"); 
1

私は問題が何であるかを知っています。これは、私がmenu.xmlに読み込んでいるイメージです。既にフォルダにあった画像をロードしても問題はありません。しかし、過去の画像をdrawableフォルダにコピーしてロードすると、この問題が発生します。

関連する問題