【问题标题】:Android - Inflating ListViewAndroid - 膨胀 ListView
【发布时间】:2011-05-21 22:37:39
【问题描述】:

我正在尝试填充一个列表视图,其中每行有 2 个文本视图和一个按钮。我想我几乎让它正常工作,但现在 ListView 只在 ListView 中显示 1 项并忽略其他数据。我还有 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;
}

【问题讨论】:

    标签: android listview inflate


    【解决方案1】:

    这是因为您在创建 Adapter 时膨胀了 View。由于您只创建了一次Adapter,因此您只需膨胀一个ViewView 需要为您的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);
        }
        ...
    }
    

    【讨论】:

    • 我会这样充气吗?因为它不太好用.. LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);渲染器 = mLayoutInflater.inflate(R.layout.shelf,null);
    • 我对 android 很陌生,对此有点挣扎。也不知道如何正确地将我的适配器与数据桥接。这不太正确 -- //MyListAdapter adapter = new MyListAdapter(getLayoutInflater().inflate(R.layout.shelf, this)); //adapter.setModel(myListModel); //setListAdapter(适配器);
    • 你需要比它不太工作更具体。如果您遇到问题中已经详细说明的问题以外的问题,您可以添加到问题中或打开新问题吗?谢谢。
    • 我的第一篇文章展示了我班级的代码。在我尝试创建 MyListAdapter 的实例之前它工作正常 - 我不知道如何正确编码以将我的 arraylist 传递到 MyListAdapter。我也不知道如何编码膨胀视图的东西..
    • //这是错误的..错误:LayoutInflater 类型中的方法 inflate(int, ViewGroup) 不适用于参数 (int,Shelf) MyListAdapter adapter = new MyListAdapter(getLayoutInflater().膨胀(R.layout.shelfrow,this));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    相关资源
    最近更新 更多