2015-01-17 108 views
0

我正在從事一個android項目,我應該從mysql數據庫中列出數據並設置圖像的src,當它位於ListView內部時,圖像名稱取自可繪製資源。下面是設計ListView的代碼,在數據庫中TAG_DRAP是表明這樣@drawable/equip1Android將Imageview src動態地設置到列表視圖

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(
        AllProductsActivity.this, productsList, 
        R.layout.list_item_equipes, new String[] { TAG_PID, 
          TAG_NAME,TAG_DRAP,TAG_DRAP}, 
        new int[] { R.id.pid, R.id.name,R.id.surname,R.id.icon }); 


      //getResources().getIdentifier("TAG_DRAP", "drawable", context.getPackageName()) 
      // updating listview 

      setListAdapter(adapter); 
     } 
    }); 

} 

回答

0

你只需要創建並在項目中使用自定義列表適配器。 在您的自定義列表適配器中,您可以動態地從每個列表項中獲取圖像視圖並設置您想要的值。

這裏和示例:

public class ListAdapter extends ArrayAdapter<Item> { 

public ListAdapter(Context context, int resource, List<Item> items) { 
    super(context, resource, items); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

View v = convertView; 

if (v == null) { 

    LayoutInflater vi; 
    vi = LayoutInflater.from(getContext()); 
    v = vi.inflate(R.layout.itemlistrow, null); 

} 

Item p = getItem(position); 

if (p != null) { 

    TextView tt = (TextView) v.findViewById(R.id.id); 
    TextView tt1 = (TextView) v.findViewById(R.id.categoryId); 
    TextView tt3 = (TextView) v.findViewById(R.id.description); 

    if (tt != null) { 
     tt.setText(p.getId()); 
    } 
    if (tt1 != null) { 

     tt1.setText(p.getCategory().getId()); 
    } 
    if (tt3 != null) { 

     tt3.setText(p.getDescription()); 
    } 
} 

return v; 

}

這裏是鏈接:list-adapter custom