I am trying to populate a listview where each row has 2 textviews and a button. I think I nearly have it working properly but right now the ListView only shows 1 item in the ListView and ignores the other data. I also have 2 xml files (shelfrow.xml (2 textfields, 1 button) and shelflist.xml(contains the listview)). Here is the core code of my Shelf.java class. (MyListItemModel is a class for storing each book)
List<MyItemModel> myListModel = new ArrayList<MyItemModel>();
try{
JSONArray entries = json.getJSONArray("entries");
for(int i=0;i<entries.length();i++){
MyItemModel item = new MyItemModel();
JSONObject e = entries.getJSONObject(i);
alKey.add(e.getInt("key"));
item.id = i;
item.title = e.getString("title");
item.description = e.getString("description");
myListModel.add(item);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
//THIS IS THE PROBLEM I THINK - ERROR: The method inflate(int, ViewGroup) in the type LayoutInflater is not applicable for the arguments (int,Shelf)
MyListAdapter adapter = new MyListAdapter(getLayoutInflater().inflate(R.layout.shelfrow,this));
adapter.setModel(myListModel);
setListAdapter(adapter);
lv = getListView();
lv.setTextFilterEnabled(true);
and some of the code in my class MyListAdapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView = renderer;
}
MyListItemModel item = items.get(position);
// replace those R.ids by the ones inside your custom list_item layout.
TextView label = (TextView)convertView.findViewById(R.id.item_title);
label.setText(item.getTitle());
TextView label2 = (TextView)convertView.findViewById(R.id.item_subtitle);
label2.setText(item.getDescription());
Button button = (Button)convertView.findViewById(R.id.btn_download);
button.setOnClickListener(item.listener);
//}
return convertView;
}
It is because you inflate the View
when you create the Adapter
. Since you only create the Adapter
once, you only inflate one View
. A View
needs to be inflated for every visible row in your ListView
.
Instead of passing the inflated View
to the constructor of MyListAdapter
:
MyListAdapter adapter = new MyListAdapter(getLayoutInflater().inflate(R.layout.shelfrow,this));
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = renderer;
}
...
}
Thy this:
// Remove the constructor you created that takes a View.
MyListAdapter adapter = new MyListAdapter();
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
// Inflate a new View every time a new row requires one.
convertView = LayoutInflater.from(context).inflate(R.layout.shelfrow, parent, false);
}
...
}