【问题标题】:Adding images to custom Listview将图像添加到自定义列表视图
【发布时间】:2016-04-22 13:18:53
【问题描述】:

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

列表视图适配器

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

}

如您所见,我在主要活动的顶部添加了两张图片,但只需要一些建议即可将其应用到我的列表视图

【问题讨论】:

标签: android listview android-custom-view


【解决方案1】:

首先,您需要将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;


}
}

使imagesfeaturesclicks 大小相同

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多