在Recycler视图中使用数组列表

问题描述 投票:0回答:6

我是回收者视图的新手。当我将ArrayList添加到RecycleView的自定义适配器时,它显示“无关紧要的正式和实际参数”

我的自定义适配器代码是..

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;


public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.ViewHolder> {
private String[] mDataset;

ArrayList<HashMap<String ,String>> productList;
Context myContext;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView dispname;
    public TextView dispphno;
    public ViewHolder(View v) {
        super(v);
        dispname=(TextView) itemView.findViewById(R.id.dispname);
        dispphno=(TextView)itemView.findViewById(R.id.dispphno);
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public MenuAdapter(ArrayList<HashMap<String,String>> productList) {

    this.productList=productList;
    //this.myContext=myContext;
}


// Create new views (invoked by the layout manager)
@Override
public MenuAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                 int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.menu_card, parent, false);
    // set the view's size, margins, paddings and layout paramete
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    //holder.mTextView.setText(mDataset[position]);
    HashMap<String,String> map=productList.get(position);
    holder.dispname.setText(map.get("name"));
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mDataset.length;
}
}

问题出在这里..

adapter=new MenuAdapter(productsList);
mRecyclerView.setAdapter(adapter);

我将适配器初始化为,

private RecyclerView.Adapter adapter;

我明白了

android arraylist android-recyclerview recycler-adapter custom-adapter
6个回答
0
投票

qazxsw poi你传递的构造函数必须采用以下格式:qazxsw poi

并在productsList中设置ArrayList<HashMap<String ,String>>长度

ArrayList

0
投票

在getItemCount()中更改mDataset.length - > productList.size()。

getItemCount()

0
投票

你在哪里传递数据集数据中的数据。替换为产品List.size()。

将代码替换为:

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return productsList.size();
}

0
投票

我发现了错误。错误是使用类的名称MenuAdapter。 MenuAdapter是一个内置类。所以我更改了名称,现在它运行了。 :)谢谢大家的建议.. !!!


0
投票

我发现有一些错误。首先使用这样的

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mDataset.length;
    }
}

然后

 @Override
public int getItemCount() {
//return mDataset.length
  return productsList.size();
}

然后

MenuAdapter menuAdapter = new MenuAdapter(Myclass.this, productsList)

在这里改变这一行。问题在于扩大布局

public MenuAdapter(Context myContext, ArrayList<HashMap<String,String>> productList) {  
    this.productList=productList;
    this.myContext=myContext;
}

0
投票

您必须将相同的数组列表传递给适配器构造函数,请仔细检查是否正确传递了它。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.