如何在Android中的自定义ListView中添加图像?

时间:2017-05-03 20:14:29

标签: android listview

我想创建此UI。

Work

在此ListView中,每个列表项中有4个更改 -

1)特别优惠(图片浏览) 2)名称(TextView的) 3)CompanyLogo的(ImageView的) 4)特性(的TextView)

我想在第一个列表项目中显示特价商品图片视图,并在每个列表项目中更改名称,公司徽标,功能。

如果我使用R.drawable.img1而不是错误更改模型字符串的签名到图像视图的int,并且在setImageResource行中也会出错。 你能告诉我应该放置什么"?"在Models.add()行?

我做到了。

主要Activity.java

public class MainActivity extends AppCompatActivity {
private static CustomAdapter adapter;
ArrayList<Model> Models;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listView = (ListView) findViewById(R.id.ls);
    Models = new ArrayList<>();
    Models.add(new Model("?", "PP AUTOMATIVES", "?", "Authorized"));
    Models.add(new Model("?", "CAR CARE MOTOR", "?", "MutliBrand"));
    Models.add(new Model("?", "CAR CARE MOTOR", "?", "MutliBrand"));
    Models.add(new Model("?", "CAR CARE MOTOR", "?", "MutliBrand"));
    adapter = new CustomAdapter(Models, getApplicationContext());
    listView.setAdapter(adapter);
}
}

CustomAdapter.java

   public class CustomAdapter extends ArrayAdapter<Model> {

Context mContext;
private ArrayList<Model> dataSet;

public CustomAdapter(ArrayList<Model> data, Context context) {
    super(context, R.layout.row_layout, data);
    this.dataSet = data;
    this.mContext = context;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Model dataModel = getItem(position);
    ViewHolder viewHolder;
    if (convertView == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.row_layout, parent, false);
        viewHolder.txtName = (ImageView) convertView.findViewById(R.id.name);
        viewHolder.txtType = (TextView) convertView.findViewById(R.id.type);
        viewHolder.txtVersion = (ImageView) convertView.findViewById(R.id.version_number);
        viewHolder.info = (TextView) convertView.findViewById(R.id.feature);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();

    }
    viewHolder.txtName.setImageResource((dataModel.getName()));
    viewHolder.txtType.setText(dataModel.getSpecialoffer());
    viewHolder.txtVersion.setImageResource(((dataModel.getCompanylogo())));
    viewHolder.info.setText(dataModel.getFeature());
    return convertView;
}

private static class ViewHolder {
    ImageView txtName;
    TextView txtType;
    ImageView txtVersion;
    TextView info;
}
}

Model.java

 public class Model {
String specialoffer;
String name;
String companylogo;
String feature;

public Model(String specialoffer, String name, String companylogo, String feature) {
    this.specialoffer = specialoffer;
    this.name = name;
    this.companylogo = companylogo;
    this.feature = feature;
}

public String getSpecialoffer() {
    return specialoffer;
}

public void setSpecialoffer(String specialoffer) {
    this.specialoffer = specialoffer;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getCompanylogo() {
    return companylogo;
}

public void setCompanylogo(String companylogo) {
    this.companylogo = companylogo;
}

public String getFeature() {
    return feature;
}

public void setFeature(String feature) {
    this.feature = feature;
}
}

1 个答案:

答案 0 :(得分:1)

如果您有图片网址。然后将字符串添加到模型并使用picasso

Picasso.
  with(context).
  load(imageurl).
  into(imageview);

如果你想使用你的drawable,那么你使用setImageResource(int)

  

模型(INT,字符串,字符串,字符串)

    Model.class
    Models.add(new Model(R.drawable.abc, "PP AUTOMATIVES", "?", "Authorized"));

    Adapter.class
    viewHolder.txtName.setImageResource((dataModel.getName()));