如何获取所有文本视图值并通过intent android传递它

时间:2015-10-31 20:44:04

标签: android android-fragments android-intent textview

如何获取所有textviews值并将其通过意图立即传递给另一个活动?这是我的源代码:

class ViewHolder extends RecyclerView.ViewHolder {

    public ImageView imgThumbnail;
    public TextView tvspecies;
    public View view;
    public ViewHolder(View itemView) {
        super(itemView);
        view = itemView;
        imgThumbnail = (ImageView) view.findViewById(R.id.img_thumbnail);
        tvspecies = (TextView) view.findViewById(R.id.tv_species);
        ArrayList<TextView> list = new ArrayList<TextView>();

        view.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {


                String intentSt = ((TextView) view.findViewById(R.id.tv_species)).getText().toString();

            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

对于传递值,请检查此问题:How do I pass data between Activities in Android application?

您也可以更改

String intentSt = ((TextView) view.findViewById(R.id.tv_species)).getText().toString();

String intentSt = tvspecies.getText().toString();

答案 1 :(得分:0)

我不确定你到底要做什么(也许你不需要使用intent发送值),但你可以使用putExtra为字符串数组做到这一点:

Intent intent = new Intent(some_action);
intent.putExtra(key, String[] values);

如果您需要保留对每个textview的引用,可以使用regualrd putExtra(key,String),其中key是您添加到textview的标记或stringify其id。

如果您想动态获取值,可以使用:

Intent intent = new Intent();
for (int i = 1; i < NUM_OF_TEXT_VIEWS; i++){
   String key = "txtView_" + i;
   int iterTxtViewId = getResources().getIdentifier(key, "id", getPackageName());
   TextView iterTxtView = (TextView)findViewById(iterTxtViewId);
   String value = iterTxtView.getText();

   intent.putExtra(key, value);
   //or
   //String anotherKey = (String)iterTxtView.getTag();
   //intent.putExtra(anotherKey, value);
}