2015-03-31 24 views
0

我有一個列表視圖和BaseAdapter添加一個圖像和一個文本行。我的問題是我如何加載列表視圖中的圖像根據文本name.i知道如果我根據文本數組添加圖像到數組我可以完成這項工作。但是從服務call.Every時間的文字排列順序我的文字串負荷然後改變我的形象的加載順序是不是correct..so可以在任何一個點我有辦法讓這個done.Thank你如何根據文本名稱加載列表視圖中的圖像

BaseAdapter類

public class MenuListAdapter extends BaseAdapter { 

private Context context; 
private List<String> mtitle; 

private int[] micon; 
private LayoutInflater inflater; 

Typeface tf; 

public MenuListAdapter(Context context, List<String> modules, int[] icon) { 
    this.context = context; 
    this.mtitle = modules; 
    this.micon = icon; 

} 

@Override 
public int getCount() { 
    return mtitle.size(); 
} 

@Override 
public Object getItem(int arg0) { 
    return arg0; 
} 

@Override 
public long getItemId(int arg0) { 
    return arg0; 
} 

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

    TextView title; 
    ImageView icon; 

    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View itemView = inflater.inflate(R.layout.nav_drawer_list_item, parent, 
      false); 

    title = (TextView) itemView.findViewById(R.id.title); 
    icon = (ImageView) itemView.findViewById(R.id.icon); 

    Typeface type = Typeface.createFromAsset(context.getAssets(),"fonts/font.ttf"); 

    title.setText(mtitle.get(position)); 
    title.setTypeface(type); 
    icon.setImageResource(micon[position]); 

    return itemView; 

} 

} 

圖像陣列

private int[] photo; 

photo = new int[]{R.drawable.btn_0011_home,R.drawable.btn_0010_accounts, R.drawable.btn_0009_contacts, R.drawable.btn_0008_opportunities, 
       R.drawable.btn_0007_leads,R.drawable.btn_0006_calendar,R.drawable.btn_0005_documents,R.drawable.btn_0004_emails,R.drawable.btn_0003_campaigns, R.drawable.btn_0002_calls, R.drawable.btn_0001_meeting, 
       R.drawable.btn_0000_tasks, R.drawable.ic_launcher} 

MenuListAdapter menuAdapter = new MenuListAdapter(
         getApplicationContext(), moduleName, photo); 
+1

檢查MODULENAME,照片都r等於 – Jai 2015-03-31 05:39:49

+0

我怎麼能檢查。 .i將圖像添加爲R.drawable.btn_001 – kosala 2015-03-31 05:43:43

+0

嘗試添加邏輯以動態地爲標題找到適當的圖像。那可能嗎? – deniz 2015-03-31 05:47:16

回答

1

您可以使用HashMap中用於此目的。我認爲文本和圖像數量是相等的。您將文本作爲鍵和圖像名稱作爲值。

HashMap<String, Integer> imageData = new HashMap<String, Integer>(); 
imageData.put("Accounts", Integer.valueOf(R.drawable.btn_0010_accounts)); 

構造器會被更改爲:

private HashMap<String, Integer> imageData; 
public MenuListAdapter(Context context, List<String> modules, HashMap<String, Integer> imageData) { 
this.context = context; 
this.mtitle = modules; 
this.imageData = imageData; 

} 

getView()方法,您可以將圖像作爲

@Override 
public View getView(int position, View arg1, ViewGroup parent) { 
    ........ 
    String textKey = mtitle.get(position); 
    Integer imageResource = imageData.get(textKey); 
    icon.setImageResource(imageResource.intValue()); 
    ......... 
} 
1

一般來說,你應該做的是上傳的圖片與同名服務器,因爲它是在數據庫中,以便您可以進行匹配BAS編輯這些像你的情況imageLoader.setImageresource(url+textview.getText.toString()+".png",imageview,options...);

嘗試以下步驟 1)改變類似於companynames提拉名在你的數據庫像R.drawable.companyname1這樣的,保持一個字符串數組名稱以如int相同的位置array.String array = {「R.drawable.companyname1」,... etc}; 2)getview方法內

holder.text.setTag(position); 
    holder.text.setText(tempValues.getCompanyName()); 
    if(holder.text.getText().toString().equalsIgnorecase(array[Integer.parseInt(textview.GetTag().toString())){ 
    holder.image.setImageResource(       array[Integer.parseInt(textview.GetTag().toString())); 
    } 
+0

嘿我非常抱歉第一次我上傳了錯誤的類的問題。現在我編輯它。請檢查this.my bad.sorry。 – kosala 2015-03-31 05:49:17

1

您可以使用Map<Integer, ObjectPojo>(ObjectPojo可以包含任何你需要的屬性),而不是List。根據要求,您可以修改像getItem, getCount, getView

方法圖像 - icon.setImageResource(map.get(integerId));

相關問題