我正在尝试为 listview 创建自定义适配器。我已经使用 ArrayList 来存储'CustomListDataModel'
类的对象。但是,当我在'getView()'
中设置图片或 textview 资源时,例如
viewHolder.image_view.setImageResources(customListDataModelArrayList.get(pos).getImage_id());
显示getImage_id()
是“无法解决” 。
这是我的问题。我在getImage_id()
中设置为获取方法的'CustomListDataModel'
方法是为了清楚地说明我的问题,这是我的代码:
public class MainActivity extends AppCompatActivity {
ListView list_view;
CustomListAdapter customListAdapter;
ArrayList <CustomListDataModel> customListDataModelArrayList =new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PopulateData();
initView();
}
public void initView(){
list_view = (ListView) findViewById(R.id.list_view);
customListAdapter = new CustomListAdapter(MainActivity.this, customListDataModelArrayList);
list_view.setAdapter(customListAdapter);
}
public void PopulateData(){
for (int i=0;i<10;i++){
CustomListDataModel customListDataModel = new CustomListDataModel();
switch (i)
{
case 0:
customListDataModel.setImageName("Angry Bird Red");
customListDataModel.setImage_id(R.drawable.angry_bird);
customListDataModel.setImageDiscription("Angry Bird is red in color");
break;
case 1:
customListDataModel.setImageName("Angry Bird Black");
customListDataModel.setImage_id(R.drawable.angry_bird);
customListDataModel.setImageDiscription("Angry Bird is black in color");
break;
case 2:
customListDataModel.setImageName("Angry Bird Blue");
customListDataModel.setImage_id(R.drawable.angry_bird);
customListDataModel.setImageDiscription("Angry Bird is blue in color");
break;
case 3:
customListDataModel.setImageName("Angry Bird Red");
customListDataModel.setImage_id(R.drawable.angry_bird);
customListDataModel.setImageDiscription("Angry Bird is red in color");
break;
case 4:
customListDataModel.setImageName("Angry Bird Black");
customListDataModel.setImage_id(R.drawable.angry_bird);
customListDataModel.setImageDiscription("Angry Bird is black in color");
break;
case 5:
customListDataModel.setImageName("Angry Bird Blue");
customListDataModel.setImage_id(R.drawable.angry_bird);
customListDataModel.setImageDiscription("Angry Bird is blue in color");
break;
case 6:
customListDataModel.setImageName("Angry Bird Green");
customListDataModel.setImage_id(R.drawable.angry_bird);
customListDataModel.setImageDiscription("Angry Bird is green in color");
break;
case 7:
customListDataModel.setImageName("Angry Bird Yellow");
customListDataModel.setImage_id(R.drawable.angry_bird);
customListDataModel.setImageDiscription("Angry Bird is yellow in color");
break;
case 8:
customListDataModel.setImageName("Angry Bird White");
customListDataModel.setImage_id(R.drawable.angry_bird);
customListDataModel.setImageDiscription("Angry Bird is white in color");
break;
case 9:
customListDataModel.setImageName("Angry Bird Red");
customListDataModel.setImage_id(R.drawable.angry_bird);
customListDataModel.setImageDiscription("Angry Bird is white in color");
break;
}
customListDataModelArrayList.add(customListDataModel);
}
}
}
public class CustomListDataModel {
private String ImageName="";
private String ImageDiscription;
private int Image_id;
public String getImageName() {
return ImageName;
}
public void setImageName(String imageName) {
ImageName = imageName;
}
public String getImageDiscription() {
return ImageDiscription;
}
public void setImageDiscription(String imageDiscription) {
ImageDiscription = imageDiscription;
}
public int getImage_id() {
return Image_id;
}
public void setImage_id(int image_id) {
Image_id = image_id;
}
}
public class CustomListAdapter extends BaseAdapter{
Activity activity;
ArrayList customListDataModelArrayList = new ArrayList<>();
LayoutInflater layoutInflater = null;
public CustomListAdapter(Activity activity, ArrayList customListDataModelArrayList){
this.activity=activity;
this.customListDataModelArrayList = customListDataModelArrayList;
layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return customListDataModelArrayList.size();
}
@Override
public Object getItem(int i) {
return customListDataModelArrayList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
public static class ViewHolder{
ImageView image_view;
TextView tv_name,tv_discription;
}
ViewHolder viewHolder = null;
// this method is called each time for arraylist data size.
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View vi=view;
final int pos = position;
if(vi == null){
// create viewholder object for list_rowcell View.
viewHolder = new ViewHolder();
vi = layoutInflater.inflate(R.layout.list_rowcell,null);
viewHolder.image_view = (ImageView) vi.findViewById(R.id.image_view);
viewHolder.tv_name = (TextView) vi.findViewById(R.id.tv_name);
viewHolder.tv_discription = (TextView) vi.findViewById(R.id.tv_discription);
vi.setTag(viewHolder);
}else {
viewHolder= (ViewHolder) vi.getTag();
}
viewHolder.image_view.setImageResource(customListDataModelArrayList.get(pos).getImage_id());
viewHolder.tv_name.setText(customListDataModelArrayList.get(pos).getImageName());
viewHolder.tv_discription.setText(customListDataModelArrayList.get(pos).getImageDiscription());
return vi;
}
}
答案 0 :(得分:0)
在CustomListAdapter内,您应该声明ArrayList的类型,以便您可以访问item方法。
ArrayList<CustomListDataModel> customListDataModelArrayList;
或者您可以对此问题使用偏见
((CustomListDataModel)customListDataModelArrayList.get(pos)).getImage_id();