如何使用putExtra从一个活动获取价值?

时间:2017-01-29 08:34:15

标签: android android-intent android-activity

public class RecyclerAdapterNew extends RecyclerView.Adapter<DataHolderNew> {

    @Override
    public DataHolderNew onCreateViewHolder(ViewGroup parent, int viewType) {

        Context context = parent.getContext();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.table_values, parent, false);
        return new DataHolderNew(view);
    }

    @Override
    public void onBindViewHolder(DataHolderNew holder, int position) {
        Intent intent=new Intent();
        intent.getExtras("transfer",);//i have to transfer the value of the 
        //textview by converting into int and transfer into another class
        holder.table_value.setText("table values: "+(position+1));

    }

    @Override
    public int getItemCount() {
        return 10;
    }
}

2 个答案:

答案 0 :(得分:1)

公共类RecyclerAdapterNew扩展了RecyclerView.Adapter {

@Override
public DataHolderNew onCreateViewHolder(ViewGroup parent, int viewType) {

    Context context = parent.getContext();
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.table_values, parent, false);

    return new DataHolderNew(view);
}

@Override
public void onBindViewHolder(DataHolderNew holder, int position) {
    Intent intent=new Intent();

    int value = intent.getIntExtra("transfer",);
    holder.table_value.setText("table values: "+(position+1)*value);

}



@Override
public int getItemCount() {
    return 10;
}

}

答案 1 :(得分:0)

您可以在调用/调用活动中使用以下内容“ -

Intent intent = new Intent(this,CalledActivity.class);
        intent.putExtra(NameKeyforvalue,value); ......

startActivity(intent)

并在Called / Invoked(接收)活动中使用以下内容: -

variabletoholdvalue = getIntent().getStringExtra(NameKeyforValue);

请注意! getIntExtra,getLongExtra等...(有些需要默认,getStringExtra没有)