Android的 - 充气的ListView [英] Android - Inflating ListView

查看:174
本文介绍了Android的 - 充气的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想填充列表视图,其中每行有2 textviews和一个按钮。我想,我几乎有它正常工作,但现在ListView控件只显示在ListView 1项,而忽略其他数据。我也有2 XML文件(shelfrow.xml(2文本框,1个按钮)和shelflist.xml(包括列表视图))。
这里是我Shelf.java类的核心code。 (MyListItemModel是一类用于存储每本书)

 列表< MyItemModel> myListModel =新的ArrayList< MyItemModel>();
尝试{
JSONArray项= json.getJSONArray(项目);
的for(int i = 0; I< entries.length();我++){
     MyItemModel项目=新MyItemModel();
     JSONObject的E = entries.getJSONObject(I)
     alKey.add(e.getInt(钥匙));
     item.id = I;
     item.title = e.getString(标题);
     item.description = e.getString(说明);      myListModel.add(项目);
 }}赶上(JSONException E){
Log.e(log_tag,错误分析数据+ e.toString());
}
//这是我认为这个问题 - 错误:在类型LayoutInflater的方法在inflate(INT,ViewGroup中)不适用的参数(INT,货架)
MyListAdapter适配器=新MyListAdapter(getLayoutInflater()膨胀(R.layout.shelfrow,这一点)。);adapter.setModel(myListModel);
setListAdapter(适配器);
LV = getListView();
lv.setTextFilterEnabled(真);

和一些code在我的课MyListAdapter

  @覆盖
  公共查看getView(INT位置,查看convertView,父母的ViewGroup){ 如果(convertView == NULL){
   convertView =渲染器;    }
    MyListItemModel项目= items.get(位置);
     //通过您的自定义布局LIST_ITEM里面的那些取代那些R.ids。
     TextView的标签=(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按钮=(按钮)convertView.findViewById(R.id.btn_download);
    button.setOnClickListener(item.listener);
    //}
    返回convertView;
}


解决方案

这是因为你吹大查看当你创建适配器。因为你只创建适配器一次,你只吹有一个查看。 A 查看需要被夸大了你的 ListView的每一可见行

而不是传递充气查看来的构造 MyListAdapter

  MyListAdapter适配器=新MyListAdapter(getLayoutInflater()膨胀(R.layout.shelfrow,这一点)。);...@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){    如果(convertView == NULL){
        convertView =渲染器;
    }
    ...
}

你的是:

  //删除您创建了一个需要查看的构造。
MyListAdapter适配器=新MyListAdapter();...@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){    如果(convertView == NULL){
        //充气一个新的查看每一个新行需要一次。
        convertView = LayoutInflater.from(上下文).inflate(R.layout.shelfrow,父母,假);
    }
    ...
}

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

这篇关于Android的 - 充气的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆