无法解析方法 'setText(Model)'
Cannot resolve method 'setText(Model)'
我正在尝试使网格视图必须显示文本,但我遇到了这个问题 no suitable method found for setText(Model)
我试图将其显示为 listView 但效果不佳,所以有人问我,这段代码是我达到的最佳代码进入它..直到现在......为什么它无法解析方法'setText(Model)'?
problem Image
这是我的适配器...
public class CatGridAdapter extends BaseAdapter {
private List<Model> catList;
public CatGridAdapter(List<Model> catList) {
this.catList = catList;
}
@Override
public int getCount() {
return catList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.quizitem, parent, false);
} else {
view = convertView;
}
((TextView) view.findViewById(R.id.title)).setText(catList.get(position));
((TextView) view.findViewById(R.id.subTitle)).setText(catList.get(position));
return view;
}
}
这是模型 class ...
private String cat;
private String SubTitle;
public Model(String cat, String subTitle) {
this.cat = cat;
SubTitle = subTitle;
}
public String getCat() {
return cat;
}
public void setCat(String cat) {
this.cat = cat;
}
public String getSubTitle() {
return SubTitle;
}
public void setSubTitle(String subTitle) {
SubTitle = subTitle;
}
}
MainActivity class ...
catGrid = findViewById(R.id.catGridView);
List<Model> catList = new ArrayList<>();
catList.add(new Model("Cat1","subTitle1"));
catList.add(new Model("Cat2","subTitle2"));
catList.add(new Model("Cat3","subTitle3"));
catList.add(new Model("Cat4","subTitle4"));
catList.add(new Model("Cat5","subTitle5"));
catList.add(new Model("Cat6","subTitle6"));
CatGridAdapter adapter = new CatGridAdapter(catList);
catGrid.setAdapter(adapter);
}
将您的代码更改为:
((TextView) view.findViewById(R.id.title)).setText(catList.get(position).getCat());
((TextView) view.findViewById(R.id.subTitle)).setText(catList.get(position).getSubTitle());
我正在尝试使网格视图必须显示文本,但我遇到了这个问题 no suitable method found for setText(Model)
我试图将其显示为 listView 但效果不佳,所以有人问我,这段代码是我达到的最佳代码进入它..直到现在......为什么它无法解析方法'setText(Model)'?
problem Image
这是我的适配器...
public class CatGridAdapter extends BaseAdapter {
private List<Model> catList;
public CatGridAdapter(List<Model> catList) {
this.catList = catList;
}
@Override
public int getCount() {
return catList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.quizitem, parent, false);
} else {
view = convertView;
}
((TextView) view.findViewById(R.id.title)).setText(catList.get(position));
((TextView) view.findViewById(R.id.subTitle)).setText(catList.get(position));
return view;
}
}
这是模型 class ...
private String cat;
private String SubTitle;
public Model(String cat, String subTitle) {
this.cat = cat;
SubTitle = subTitle;
}
public String getCat() {
return cat;
}
public void setCat(String cat) {
this.cat = cat;
}
public String getSubTitle() {
return SubTitle;
}
public void setSubTitle(String subTitle) {
SubTitle = subTitle;
}
}
MainActivity class ...
catGrid = findViewById(R.id.catGridView);
List<Model> catList = new ArrayList<>();
catList.add(new Model("Cat1","subTitle1"));
catList.add(new Model("Cat2","subTitle2"));
catList.add(new Model("Cat3","subTitle3"));
catList.add(new Model("Cat4","subTitle4"));
catList.add(new Model("Cat5","subTitle5"));
catList.add(new Model("Cat6","subTitle6"));
CatGridAdapter adapter = new CatGridAdapter(catList);
catGrid.setAdapter(adapter);
}
将您的代码更改为:
((TextView) view.findViewById(R.id.title)).setText(catList.get(position).getCat());
((TextView) view.findViewById(R.id.subTitle)).setText(catList.get(position).getSubTitle());