在Recycler视图中使用数组列表

时间:2017-04-06 05:39:11

标签: android arraylist android-recyclerview recycler-adapter custom-adapter

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

我的CustomAdapter代码是..

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;

我明白了

Error while ruuning

6 个答案:

答案 0 :(得分:0)

productsList您传递的构造函数必须采用以下格式:ArrayList<HashMap<String ,String>>

并将ArrayList长度设置为getItemCount(),如此

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

答案 1 :(得分:0)

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

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

然后

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

然后

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

在此处更改此行。问题在于夸大布局

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

答案 2 :(得分:0)

  

更改 mDataset.length - &gt; getItemCount()中的productList.size()

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

答案 3 :(得分:0)

您在哪里传递mDataset数组中的数据。替换为productsList.size()。

用以下代码替换代码:

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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