Skip to content
Advertisement

Cannot resolve method ‘setText(Model)’

I am trying to make grid view have to texts but I faced this problem no suitable method found for setText(Model)i am tried to make to listView but doesn’t go well so I am asked and this code is best code I reached into it .. till now … why it Cannot resolve method ‘setText(Model)’ ?

problem Image

This is my Adapter …

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

And this is model 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);
    }

Advertisement

Answer

change your code to this:

((TextView) view.findViewById(R.id.title)).setText(catList.get(position).getCat());
((TextView) view.findViewById(R.id.subTitle)).setText(catList.get(position).getSubTitle());
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement