我无法从适配器成功缩进

时间:2018-09-05 11:46:36

标签: android

我正在为RecyclerView设置此适配器,我无法从该适配器上缩进,我不知道为什么有些人可以指导我这里发生的事情,因为我也不是编程专家安卓

下面是我的Adapter类和Activity类。

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDatasetname;
private Integer[] mexp;
private Context context;
public ToggleButton select;
Integer selectedcountint=0;
private Bitmap[] mpro;
private String[] mloc;
private String[] mobj;Context m;
private String[] mselected;
 public ArrayList<String> nselected = new ArrayList<>();



public static class MyViewHolder extends RecyclerView.ViewHolder{

    public CardView mCardView;
    public TextView mTextView;
    public TextView texp;
    public Button pdf;
    public ToggleButton select;
    public ImageView pro;

    public TextView loc;
    public View layout;
    Context context;
    private RecyclerView.ViewHolder s;

    public MyViewHolder(View v){

        super(v);


        mCardView = (CardView) v.findViewById(R.id.card_view);
        mTextView = (TextView) v.findViewById(R.id.tv_text);
       texp = (TextView) v.findViewById(R.id.setexp);
        pdf = (Button) v.findViewById(R.id.moreinfo);
        select = (ToggleButton) v.findViewById(R.id.select);
        pro = (ImageView) v.findViewById(R.id.setpropic);
        loc = (TextView) v.findViewById(R.id.setlocation);

    }

    void bind(String name, Integer exp, String location, final Bitmap image, final String obj ) {
        mTextView.setText(name);

       texp.setText(String.valueOf(exp)+" yrs");
        pro.setImageBitmap(image);
        loc.setText(location);


        pdf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Dashboard.this, com.parse.starter.View.class);
                intent.putExtra("no", "6382551203");
                startActivity(intent);

            }
        });

    }


}

public MyAdapter(Context s,String[] myDataset,Integer[] exp,Bitmap[] pro,String[] loc,String[] obj){


    mDatasetname = myDataset;

    mexp=exp;
    mpro=pro;
    mloc=loc;

    mobj=obj;
    m=s;


}



@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

    context=parent.getContext();

    View vs = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
    MyViewHolder vh = new MyViewHolder(vs);
    return vh;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position){


    holder.bind(mDatasetname[position],mexp[position],mloc[position],mpro[position],mobj[position]);

}


@Override
public int getItemCount() { return mDatasetname.length; }

}

4 个答案:

答案 0 :(得分:2)

解决方案:

代替此:

Intent intent = new Intent(Dashboard.this, com.parse.starter.View.class);
intent.putExtra("no", "6382551203");
startActivity(intent);

这样写:

Intent intent = new Intent(Dashboard.this, (your_destination_class_name).class);
intent.putExtra("no", "6382551203");
startActivity(intent);

例如:

Intent intent = new Intent(Dashboard.this, RegisterActivity.class);
intent.putExtra("no", "6382551203");
startActivity(intent);

如果目标类的名称是View.class,则将其更改为其他名称。

如果上述方法无效,

替换:

View vs = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);

具有:

View vs = LayoutInflater.from(m).inflate(R.layout.card_item, parent, false);

然后尝试。希望它能起作用。

答案 1 :(得分:1)

  first make constructor, adapter wont work with out constructor, its a big miss please check

    holder.pdf.setOnClickListener(new View.OnClickListener() {
                @Override
                   public void onClick(View v) {

                    Intent intent = new Intent(context,YourIndentClassName.class);
                    intent.putExtra("no", "6382551203");
                    context.startActivity(intent);
                }
            });

答案 2 :(得分:0)

使用此代码编辑代码:

pdf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, com.parse.starter.View.class/*should be a proper class name*/);
                intent.putExtra("no", "6382551203");
                context.startActivity(intent);

            }
        });

答案 3 :(得分:-1)

holder.pdf.setOnClickListener(new View.OnClickListener() {
            @Override
               public void onClick(View v) {

                Intent intent = new Intent(context,YourIndentClassName.class);
                intent.putExtra("no", "6382551203");
                context.startActivity(intent);
            }
        });

强文本  在RecyclerVieAdapter的onBindViewHolder方法中使用此代码