2016-04-22 165 views
0

你好,我只需要通過在左邊添加一個圖像數組來完成我的自定義列表視圖。目前我已經添加了完美的文本,只需添加圖像即可。我猜你把它們設置爲一個類似於我已經爲文本完成的數組,但只需要看看它是如何完成的。將圖像添加到自定義列表視圖

ListviewAdapter

public class ListViewAdapter extends ArrayAdapter<String> { 

String[] features={}; 
String[] clicks={}; 


Context c; 
LayoutInflater inflater; 




public ListViewAdapter(Context context, String[] features, String[] clicks) { 
    super(context, R.layout.custom_row, features); 

    this.c = context; 
    this.features = features; 
    this.clicks = clicks; 





} 

public class ViewHolder{ 

    TextView txtphone; 
    TextView txtcall; 


} 

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


     inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.custom_row, null); 
    } 

    final ViewHolder holder = new ViewHolder(); 


    holder.txtphone = (TextView) convertView.findViewById(R.id.txtphone); 
    holder.txtcall= (TextView) convertView.findViewById(R.id.txtcall); 


    holder.txtphone.setText(features[position]); 
    holder.txtcall.setText(clicks[position]); 


    return convertView; 


} 
} 

主要活動

String[] feature= {"Phone", "Email", "Website", "Opening Times"}; 
String[] click = {"click", "click", "click", "click", "click"}; 
int[] images ={R.drawable.ic_menu_gallery, R.drawable.ic_menu_send}; 

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



    contactUs = (ListView)findViewById(R.id.contactUsListView); 




    ListViewAdapter adapter = new ListViewAdapter(this,feature,click); 
    contactUs.setAdapter(adapter); 


    contactUs.setOnItemClickListener(new Itemlist()); 

} 

正如你可以看到我已經加了兩個圖片到我的主要活動的頂部,但只需要一點點建議將它應用到我的列表視圖

+0

你google一下這個初始化適配器。? –

+0

看到這個鏈接。它的幫助你..http://www.vogella.com/tutorials/AndroidListView/article.html –

回答

0

首先,您需要將ImageView添加到您的custom_row佈局中。假設您已經添加了ID imgView。您應該擁有與ListView中的項目相同數量的圖像。現在,你需要在適配改變代碼如下

public class ListViewAdapter extends ArrayAdapter<String> { 

String[] features={}; 
String[] clicks={}; 
int[] images={}; 


Context c; 
LayoutInflater inflater; 




public ListViewAdapter(Context context, String[] features, String[] clicks, int[] images) { 
    super(context, R.layout.custom_row, features); 

    this.c = context; 
    this.features = features; 
    this.clicks = clicks; 
    this.images = images; 





} 

public class ViewHolder{ 

    TextView txtphone; 
    TextView txtcall; 
    ImageView imgView;  

} 

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


     inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.custom_row, null); 
    } 

    final ViewHolder holder = new ViewHolder(); 


    holder.txtphone = (TextView) convertView.findViewById(R.id.txtphone); 
    holder.txtcall= (TextView) convertView.findViewById(R.id.txtcall); 
    holder.imgView= (ImageView) convertView.findViewById(R.id.imgView); 


    holder.txtphone.setText(features[position]); 
    holder.txtcall.setText(clicks[position]); 
    holder.imgView.setImageResource(images[position]); 


    return convertView; 


} 
} 

images大小相同featuresclicks

int[] images ={R.drawable.ic_menu_gallery, R.drawable.ic_menu_send, R.drawable.ic_menu_send, R.drawable.ic_menu_send, R.drawable.ic_menu_send}; 

而且如下

ListViewAdapter adapter = new ListViewAdapter(this,feature,click,images); 
相關問題