i want know why getview() function is not call maybe override is wrong? i want make custom listlayout what's wrong in listviewadapter?
public class ListViewAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Word> items;
public ListViewAdapter(Context mContext, ArrayList<Word> items) {
this.mContext = mContext;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.wordlist_layout, viewGroup, false);
}
TextView wordview1 = view.findViewById(R.id.wordView1);
System.out.println("wordlist layout = " items.get(i).w1);
wordview1.setText(items.get(i).w1);
return view;
}
// this is my Main activity code
private ArrayList<Word> items;
private ListViewAdapter listviewadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.submit_Button);
btn.setOnClickListener(view -> {
ListView word_listview = findViewById(R.id.Wordle_Word_List);
items = new ArrayList<Word>();
items.add(new Word('a'));
listviewadapter = new ListViewAdapter(getApplicationContext(), items);
word_listview.setAdapter(listviewadapter);
});
}
i dont know what is missing in my code In ListviewAdapter I dont know why getview() function not call
CodePudding user response:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(mContext).inflate(R.layout.wordlist_layout, viewGroup, false);
TextView wordview1 = view.findViewById(R.id.wordView1);
wordview1.setText(items.get(i).w1);
return view;
}
CodePudding user response:
I think your problem is the condition in its body, not the whole function. There is no need to check if it is null or not. you can simply inflate the view as mentioned in this article:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.wordlist_layout, viewGroup, false);
TextView wordview1 = view.findViewById(R.id.wordView1);
System.out.println("wordlist layout = " items.get(i).w1);
wordview1.setText(items.get(i).w1);
return view;
}