无法使用putExtra方法从RecyclerView项目单击解析到另一个活动

时间:2018-06-02 17:56:42

标签: android android-recyclerview onitemclicklistener

我想通过单击RecyclerView项并使用putExtra方法将RecyclerView数据解析为另一个活动。 其中显示Toast消息和意图另一个活动但不解析数据。

我不明白这个问题!! PLZ!帮助我......

这里是DataAdapter类,

DataAdapter.java

225

}

这里是DataModel类,

DataModel.java

public class DataAdapter extends android.support.v7.widget.RecyclerView.Adapter<DataAdapter.DataViewHolder>{
Context ctx;
List<DataModel> dataModelList;


public DataAdapter(Context ctx, List<DataModel> dataModelList) {
    this.ctx = ctx;
    this.dataModelList = dataModelList;
}

@Override
public DataViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(ctx);
    View view = inflater.inflate(R.layout.row_township_union,null);
    return new DataViewHolder(view,dataModelList);
}

@Override
public void onBindViewHolder(DataViewHolder holder, final int position) {
    final DataModel dataModel = dataModelList.get(position);

    holder.textViewUnion.setText(dataModel.getTownship_union());


}

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


class DataViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder{
    TextView textViewUnion;
    List<DataModel> dataModelList;
    CardView cardView;


    public DataViewHolder(View itemView, List<DataModel> data) {
        super(itemView);
        dataModelList = data;
        textViewUnion = itemView.findViewById(R.id.tvUnion);
        cardView = itemView.findViewById(R.id.card);

        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();

                Toast.makeText(ctx, "clicked !", Toast.LENGTH_LONG).show();
                Intent  i= new Intent(ctx, AreaSelect.class);
                i.putExtra("key",dataModelList.get(position));
                ctx.startActivity(i);
            }
        });
    }
}

}

AreaSelect.java

public class DataModel implements Parcelable {
String township_union;

public DataModel(String township_union) {
    this.township_union = township_union;
}

public String getTownship_union() {
    return township_union;
}

public void setTownship_union(String township_union) {
    this.township_union = township_union;
}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.township_union);
}

protected DataModel(Parcel in) {
    this.township_union = in.readString();
}

public static final Parcelable.Creator<DataModel> CREATOR = new Parcelable.Creator<DataModel>() {
    @Override
    public DataModel createFromParcel(Parcel source) {
        return new DataModel(source);
    }

    @Override
    public DataModel[] newArray(int size) {
        return new DataModel[size];
    }
};

}

1 个答案:

答案 0 :(得分:0)

根据你发布的内容,你试图恢复一个字符串,但你已经在活动的附加内容中发布了一个parcelable:

i.putExtra("key",dataModelList.get(position));

所以这个:

Bundle extras = getIntent().getExtras(); 
union = extras.getString("key");

应该是:

 Bundle extras = getIntent().getExtras(); 
 DataModel union = extras.getParcelable("key");

如果您想保留原始代码,则可以从模型中发布属性:

i.putExtra("key",dataModelList.get(position).getTownship_union());