将图像添加到自定义Listview

时间:2016-04-22 13:18:54

标签: android listview android-custom-view

您好我只需要通过在左侧添加一个图像数组来完成我的自定义列表视图。目前我已经添加了完全正常的文本,只需要添加图像。我猜你把它们设置为一个类似于我已经为文本做过的数组,但只需看看它是如何完成的。

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());

}

正如您所看到的,我已在主要活动的顶部添加了两个图像,但只需要一些建议将其应用到我的列表视图

1 个答案:

答案 0 :(得分: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);