如何逐个显示Bundle数据到textview?

时间:2015-06-29 17:39:08

标签: android bundle

如何将Bundle数据逐个显示到文本视图?

我的活动有10个文本视图。我希望​​将所有包数据显示到文本视图。

这是捆绑格式。

Bundle[{INDUSTRY_TYPE_ID=Retail, CHANNEL_ID=WAP, CHECKSUMHASH=IIUBjkuTPs9xx/TnzKbR0BkWKvsumSCMyrYBIZzAppNTdJGC+U+XyjESvFwCax/ME2dexR05YKDJuZSsgpSxxDtAUb6bGeOs9u/1t9FxxkE=, MOBILE_NO=7428453915, REQUEST_TYPE=DEFAULT, TXN_AMOUNT=4.0, MID=klbGlV59135347348753, EMAIL=singhamit1632@gmail.com, THEME=merchant, payt_STATUS=1, CUST_ID=48, WEBSITE=paytm, ORDER_ID=FCF449}]

拥有捆绑数据的第一个活动

Intent intent = new Intent(PaymentPageActvity.this,CancelPaymentActivity.class);
Bundle b=new Bundle();
b.putString("ss", inResponse.toString());

intent.putExtras(b);
startActivity(intent);

获取数据并在textview上显示的第二个活动

Intent intent = getIntent();

Bundle b = CancelPaymentActivity.this.getIntent().getExtras();
String v = b.getString("ss").toString();
INDUSTRY_TYPE_ID.setText(v);

INDUSTRY_TYPE_ID ==文字视图名称

1 个答案:

答案 0 :(得分:0)

假设您的活动中有Bundle个值和layout(例如ListLayout),您希望为某些内容添加TextViewTextView您的捆绑包中的所有条目。

如果您的布局是静态的,那么您的TextView已经定义,并且您知道哪些包值在TextView industryType = (TextView)findViewById(R.id.textIndustryType); industryType.setText(bundle.getString("INDUSTRY_TYPE_ID")); // ... 中,您可以执行此操作:

private static Map<String, String> keyToTextViewId = new HashMap<String, String>();

static {
    keyToTextViewId.put("INDUSTRY_TYPE_ID", R.id.textIndustryType);
    keyToTextViewId.put("CHANNEL_ID", R.id.textChannelId);
    // ...
}

private TextView getTextViewFor(String bundleKey) {
    String textViewId = keyToTextViewId.get(bundleKey);
    return (textViewId != null ? findViewById(textViewId) : null);
}

private void updateTextViews(Bundle bundle) {
    for (String key : bundle.keySet()) {
        TextView tv = getTextViewFor(key);
        if (tv != null) {
            tv.setText(bundle.get(key).toString());
        }
    }
}

// ...

    Bundle bundle = CancelPaymentActivity.this.getIntent().getExtras();
    updateTextViews(bundle);

或者定义一个将捆绑密钥与文本视图ID相关联的地图并使用它。例如:

layout.addView(TextView)

如果你的布局不是静态的(即你不知道你正在获得什么包,并且需要在运行时创建TextViews),那么它与上面类似,除了你需要创建你的TextViews&amp;也可以随时拨打// Once the TextViews are created, access them through this map private Map<String, TextView> keyToTextView = new HashMap<String, TextView>(); // Helper function to create consistent TextViews, creating & adding a new // one if needed private TextView getTextViewFor(String bundleKey, Layout layout) { TextView tv = keyToTextView.get(bundleKey); if (tv != null) { return tv; } else { tv = new TextView(this); tv.setLayoutParams(new LayoutParams(/* updated as needed */)); keyToTextView.put(key, tv); layout.addView(tv); return tv; } } // ... private void updateTextViews(Bundle bundle, Layout layout) { for (String key : bundle.keySet()) { getTextViewFor(key, layout).setText(bundle.get(key).toString()); } }

$ curl -L -JO "https://www.dropbox.com/s/uex431ou02h2m2b/300x50.gif?dl=1"
...
curl: Saved to filename '300x50.gif'
$ ls
300x50.gif
$ rm 300x50.gif
$ curl -L -o "NewNameImage.gif" "https://www.dropbox.com/s/uex431ou02h2m2b/300x50.gif?dl=1"
...
$ ls
NewNameImage.gif

在后一种情况下,您可能希望使用其他代码来过滤实际显示的键以及按什么顺序显示。您可能还需要为每个TextView定义标签,其中标签包含捆绑密钥(或其一些用户友好版本)。您可以在this answer中找到有关如何执行此操作的详细信息。