Android - раздувание ListView - PullRequest
4 голосов
/ 22 мая 2011

Я пытаюсь заполнить просмотр списка, где в каждой строке есть 2 просмотра текста и кнопка. Я думаю, что у меня почти все работает правильно, но сейчас ListView показывает только 1 элемент в ListView и игнорирует другие данные. У меня также есть 2 xml-файла (shelfrow.xml (2 текстовых поля, 1 кнопка) и shelflist.xml (содержит просмотр списка)). Вот основной код моего класса Shelf.java. (MyListItemModel - класс для хранения каждой книги)

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

и часть кода в моем классе 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;
}

1 Ответ

9 голосов
/ 22 мая 2011

Это потому, что вы создаете View, когда создаете Adapter. Поскольку вы создаете Adapter только один раз, вы только надуваете один View. A View необходимо раздуть для каждой видимой строки в вашем ListView.

Вместо передачи надутого View конструктору 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;
    }
    ...
}

Твое это:

// 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);
    }
    ...
}
...