2017-02-28 8 views
-1

的朋友我想創建一個應用程序,它已經從JSON陣列 例如,在創建文本視圖如何使用JSON編程添加文字去Android

"name":"Lenovo","price":"5000","description":"2 gb ram","type":"mobile" 

將改變它知道的價值觀和創建文本字段與此標題名稱,類型,價格

+0

嘗試解析JSON和使用TextView.setText() – shadygoneinsane

+0

@Selva - 提供完整的json – Ragini

+0

你需要先迭代json字符串。然後動態創建TextView – ZeroOne

回答

0

試試這個.. 與LinearLayout中

創建佈局
<LinearLayout 
    android:orientation="vertical" 
    android:id="@+id/linear" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
</LinearLayout> 

然後遍歷您的JSON

LinearLayout linearLayout= (LinearLayout)findViewById(R.id.linear);  
linearLayout.removeAllViews();        

Iterator<String> iter = new JSONObject("JSON STRING").keys(); 
while (iter.hasNext()) { 
    String key = iter.next(); 
    try { 
     Object value = json.get(key); 

     TextView textView= new TextView(this);    
     textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 48)); 
     textView.setGravity(Gravity.CENTER_VERTICAL);      
     textView.setText(key+" : "+value.toString());          
     linearLayout.addView(textView);  


    } catch (JSONException e) { 
     // Something went wrong! 
    } 
} 
0

你需要消費你的json以某種方式,使用框架如gson或類如JSONObject(在網上找到,如果你需要)。

假設你有數據就擺在TextView所以下次你需要創建TextView您的活動:

TextView textView = new TextView(this); //it need context 
textView.setText(yourJSONObject.getText()); 

最後一部分是把TextView到您的佈局,(在這種情況下的LinearLayout)。

yourLinearLayout.addView(addView(textVIew)) 
相關問題