2016-06-10 25 views
-1

我有用於讀取數據庫的應用程序。代碼工作完美,但是當我使用if .. then ... else ImageView它不起作用。可能是我正在使用我的代碼。任何人都可以幫助我解決我的代碼?圖片在android中不能更改

我的屏幕拍攝應用http://prntscr.com/beuid8

這樣

Toolbar toolbar; 
ImageView image; 
// Progress Dialog 
private ProgressDialog pDialog; 
// Creating JSON Parser object 
JSONParser jParser = new JSONParser(); 

private ListView listView; 

ArrayList<HashMap<String, String>> productsList; 

// url to get all products list 
private static String url_all_products = "http://192.168.1.111/log_inputitem.php"; 

// JSON Node names 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_PRODUCTS = "inputitemlog"; 
private static final String TAG_LOGO = "Item Logo"; 
private static final String TAG_NAME = "Name"; 
private static final String TAG_CODEITEM = "Item Code"; 
private static final String TAG_UPGRADEITEM = "Item Upgrade"; 
private static final String TAG_DATEITEM = "Item Date"; 

// products JSONArray 
JSONArray products = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_logitem); 
    // ListView listView = (ListView) findViewById(android.R.id.list); 
    // Hashmap for ListView 
    productsList = new ArrayList<HashMap<String, String>>(); 

    // Loading products in Background Thread 
    new LoadAllProducts().execute(); 

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    image = (ImageView) findViewById(R.id.thumbnail); 

    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    //listView = (ListView) findViewById(R.id.lista); 
    TypedValue typedValueColorPrimaryDark = new TypedValue(); 
    ListLogItem.this.getTheme().resolveAttribute(R.attr.colorPrimary, typedValueColorPrimaryDark, true); 
    final int colorPrimaryDark = typedValueColorPrimaryDark.data; 
    if (Build.VERSION.SDK_INT >= 21) { 
     getWindow().setStatusBarColor(colorPrimaryDark); 
    } 
} 

// Get listview 
protected ListView getListView(){ 
    if (listView == null) { 
     listView = (ListView) findViewById(R.id.logitem); 
    } 
    return listView; 
} 

// Response from Edit Product Activity 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    // if result code 100 
    if (resultCode == 100) { 
     // if result code 100 is received 
     // means user edited/deleted product 
     // reload this screen again 
     Intent intent = getIntent(); 
     finish(); 
     startActivity(intent); 
    } 
} 

/** 
* Background Async Task to Load all product by making HTTP Request 
* */ 
class LoadAllProducts extends AsyncTask<String, Void, String> { 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(ListLogItem.this); 
     pDialog.setMessage("Loading. Please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    /** 
    * getting All products from url 
    * */ 
    protected String doInBackground(String... args) { 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     // getting JSON string from URL 
     JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params); 

     // Check your log cat for JSON reponse 
     Log.d("All Products: ", json.toString()); 

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

      if (success == 1) { 
       // products found 
       // Getting Array of Products 
       products = json.getJSONArray(TAG_PRODUCTS); 

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

        // Storing each json item in variable 
        String logo = c.getString(TAG_LOGO); 
        String name = c.getString(TAG_NAME); 
        String codeitem = c.getString(TAG_CODEITEM); 
        String upgradeitem = c.getString(TAG_UPGRADEITEM); 
        String dateitem = c.getString(TAG_DATEITEM); 

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

        // adding each child node to HashMap key => value 
        map.put(TAG_LOGO, logo); 
        map.put(TAG_NAME, name); 
        map.put(TAG_CODEITEM, codeitem); 
        map.put(TAG_UPGRADEITEM, upgradeitem); 
        map.put(TAG_DATEITEM, dateitem); 

        // adding HashList to ArrayList 
        productsList.add(map); 
       } 
      } else { 
       Toast.makeText(getApplicationContext(), "Doesn't have log item now", Toast.LENGTH_SHORT).show(); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog after getting all products 
     pDialog.dismiss(); 
     // updating UI from Background Thread 
     //runOnUiThread(new Runnable() { 
     // public void run() { 
     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(
       ListLogItem.this, productsList, 
       R.layout.list_inputitem, new String[]{ 
       TAG_LOGO, TAG_NAME, TAG_CODEITEM, TAG_UPGRADEITEM, TAG_DATEITEM}, 
       new int[]{R.id.thumbnail, R.id.title, R.id.rating, R.id.genre, R.id.releaseYear}); 
     if (TAG_LOGO == "ig"){ 
      image.setImageResource(R.drawable.flat); 
     } 
     // updating listview 
     getListView().setAdapter(adapter); 
     //} 
     // }); 
    } 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    MenuInflater inflater = getMenuInflater(); 
    getMenuInflater().inflate(R.menu.menu_news, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

任何人我的代碼可以幫助我,使我的ImageView的工作? 之前感謝

回答

0

看起來您正確地獲取圖像的名稱,但因爲這些圖像名稱不在您的應用程序中的任何位置(這是我的猜測)SimpleAdapter沒有要匹配的資源Id給他們;如果您收到像「pretty_car.png」這樣的圖像名稱,並且您將其傳遞給SimpleAdapter,那麼對於它的任何內容都無關緊要,不知道應該從應用內容中加載資源中的哪個圖像。您唯一的選擇似乎是創建一個自定義適配器,它根據文件名檢索圖像。