Search code examples
androidandroid-recyclerviewonitemclicklistener

cannot data parse to another activity from RecyclerView item click, using putExtra method


I want to parse RecyclerView data to another activity by clicking RecyclerView item and using putExtra method. Where show Toast message and intent another activity but not parse data.

I do not understand the problem!! plz!!! help me....

here the DataAdapter class,

DataAdapter.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);
            }
        });
    }
}

}

here the DataModel class,

DataModel.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];
    }
};

}

AreaSelect.java

public class AreaSelect extends AppCompatActivity {
TextView textView;
String union;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_area_select);

    textView = findViewById(R.id.tvUnion);

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

    textView.setText(union);
}

}


Solution

  • From what you've posted you're trying to rerieve a string, but you've posted a parcelable in the extras of your activity:

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

    So this:

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

    Should be:

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

    If you want to keep your original code, then you can post the attribute from your model instead:

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