2016-10-14 16 views
10

इम एंड्रॉयड लेआउट में विचारों की स्थापना के लिए DataBinding एपीआई का उपयोग कर के लिए दो तार concat करना चाहते हैं। मेरा लेआउट यहाँ है।मैं एंड्रॉयड में एक TextView, डेटा बाइंडिंग एपीआई

layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 
    <data> 
    <variable name="user" type="testing.sampleapp.com.sampleapp.User"/> 
    </data> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@{ "Hello " + user.firstName}"/> 
</LinearLayout> 

मैं TextView हैलो उपयोगकर्ता नाम प्रदर्शित करना चाहते हैं। डेटा बाइंडिंग एपीआई का उपयोग करके इसे कैसे प्राप्त करें।

उत्तर

33

concate यह कब्र लहजे (`) के साथ

android:text="@{`Hello ` + user.firstName}"/> 
+0

हाँ यह काम किया। धन्यवाद । मैं एकल उलटा कॉमा (') का उपयोग कर रहा था। कब्र उच्चारण (') मेरे लिए काम किया। –

+0

@SasankSunkavalli आपकी मदद करने में प्रसन्न है :), हमेशा डेटा बाइंडिंग का उपयोग करते समय स्ट्रिंग के लिए गंभीर उच्चारण का उपयोग करें। –

3

"Hello %$1s , (whatever you want to add then add here)" तरह strings.xml

में आपका स्ट्रिंग घोषित।

AMD उपयोग String.format(stringResource, upsatename);

+0

मैं जानता हूँ कि यह काम करेगा, लेकिन मैं concat रास्ता –

+0

आप एक से अधिक यहाँ concat का उपयोग कर% $ 1s,% $ 2s ... –

+9

तुम भी अभिव्यक्ति में स्वरूपित स्ट्रिंग संसाधनों का उपयोग कर सकते हैं कर सकते हैं का उपयोग करना चाहते: 'एंड्रॉयड: पाठ =" @ {@ स्ट्रिंग/हैलो (user.firstName)} "' –

2

XML विशेषता के मूल्यों के लिए एकल उद्धरण का समर्थन करता है के बाद से, आप भी ऐसा कर सकते हैं:

android:text='@{"Hello "+user.firstName}' 
0

XML लेआउट में एक concat करने के लिए:

<data> 

/*This is used for android view*/ 
<import type="android.view.View" /> 

/*This is used for android resources*/ 
<import type="com.myapp.R" /> 

/*This is app context*/ 
<variable 
    name="context" 
    type="android.content.Context" /> 

/*This is used for model data*/ 
<variable 
    name="item" 
    type="com.myapp.models.Chapter" /> 
</data> 

android:text="@{item.serialNo.concat(@string/space).concat(item.title)}" 

में रिक्त स्थान के लिए strings.xml मैं जोड़ लिया है कोड:

<string name="space">\u0020</string> 
संबंधित मुद्दे