我通过我的api获得了一个json数组。 我想在我的视图中显示数据,但我不知道如何生成例如textviews并将数据放在每个视图中。 (我的愿望是将数据显示在html表格中,但目前我很好理解如何将数据放入动态文本视图中 )
String id = jsonobject.getString("id");
String category = jsonobject.getString("category");
String content = jsonobject.getString("content");
保持正确的数据。
以下方法在我的异步任务中:
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result != "error") {
JSONArray jsonArray;
TextView textview = (TextView) findViewById(R.id.jokeContent);
try {
jsonArray = new JSONArray(result);
for(int i=0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
String id = jsonobject.getString("id");
String category = jsonobject.getString("category");
String content = jsonobject.getString("content");
}
} catch (Exception e) {
Toasty.error(context, "Catch , test1!", Toast.LENGTH_SHORT, true).show();
}
} else {
Toasty.error(context, "Else , test2!", Toast.LENGTH_SHORT, true).show();
}
}
答案 0 :(得分:0)
你可以先从你的版面添加一个表格布局,然后在你的布局文件上创建一个类似这样的行:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/row">
<TextView
android:id="@+id/ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="20"
android:text="TextView" />
<TextView
android:id="@+id/Category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="20"
android:text="TextView" />
<TextView
android:id="@+id/Content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="20"
android:text="TextView" />
</TableRow>
然后你可以添加行,并使用inflater
在活动中填写这样的信息for(int i=0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
String id = jsonobject.getString("id");
String category = jsonobject.getString("category");
String content = jsonobject.getString("content");
TableLayout tableLayout = (TableLayout)findViewById(R.id.table);
TableRow tablerow = (TableRow)getLayoutInflater().inflate(R.layout.row, tableLayout,false);
((TextView)tablerow.findViewById(R.id.ID)).setText(id);
((TextView)tablerow.findViewById(R.id.Category)).setText(category);
((TextView)tablerow.findViewById(R.id.Content)).setText(content);
tableLayout.addView(tablerow);
}
希望有所帮助
答案 1 :(得分:0)
如果我正确理解了您的问题,您需要创建一个textView并在获得Asynk任务的响应后更新它。 这个想法是......
在Activity中查找TextView,而不是在Asynk任务中查找。在你的情况下
TextView textview =(TextView)findViewById(R.id.jokeContent);
将数据放入AsyncTask中的某些公共资源,并将其置于Activity中。
E.x。 (Kotlin)通过界面
interface AsyncResponse {
fun finishProcess(result: String)
}
...
class AsyncExecuter constructor(val asyncResp: AsyncResponse)
...
override fun onPostExecute(result: String) { //AsynkTask
super.onPostExecute(result)
asyncResp.finishProcess(result) //asynkResp is interfase which is put to AsynkTask constructor
}
Activity实现我自己的AsynkResp接口并调用AsynkTask
override fun onResume() {
super.onResume()
AsyncExecuter(this).execute()
}
并在更新TextView
的活动中从接口实现方法 override fun finishProcess(output: String) {
textview?.text = output
}
主要思想是使用UI进行操作应该从Activity而不是从并行线程完成。
答案 2 :(得分:0)
在这个例子中,我已经为一个带前缀的数组列表创建了一个列表。我使用java来创建动态视图而不是XML。
public class MainActivity extends AppCompatActivity { String[] wordlist = new String[] { "Shahi Paneer", "Shahi Thali","Shahi Parantha","Kadai Paneer","Mix Parantha", }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); ListView list = new ListView(this); list.setBackgroundColor(Color.rgb(240,255,255) ); list.setAdapter(new MyAdapter(this, wordlist)); setContentView(list); } private class MyAdapter extends ArrayAdapter<String> { public MyAdapter(Context context, String[] strings) { super(context, -1, -1, strings); } @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) @Override public View getView(int position, View convertView, ViewGroup parent) { Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/BLESD___ .ttf"); Typeface rupeeSymbol = Typeface.createFromAsset(getAssets(), "fonts/IndianRupee.ttf"); Typeface maze = Typeface.createFromAsset(getAssets(), "fonts/The Heart Maze Demo.ttf"); Typeface epicselfie = Typeface.createFromAsset(getAssets(), "fonts/My Epic Selfie Demo.ttf"); Typeface novaoval = Typeface.createFromAsset(getAssets(), "fonts/NovaOval.ttf"); LinearLayout listLayout = new LinearLayout(MainActivity.this); listLayout.setLayoutParams(new AbsListView.LayoutParams( AbsListView.LayoutParams.WRAP_CONTENT, AbsListView.LayoutParams.WRAP_CONTENT)); listLayout.setId(View.generateViewId()); listLayout.setOrientation(LinearLayout.VERTICAL); LinearLayout layout1 = new LinearLayout(MainActivity.this); layout1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); layout1.setOrientation(LinearLayout.HORIZONTAL); listLayout.addView(layout1); ImageView imgView = new ImageView(MainActivity.this); imgView.setImageResource(R.drawable.thali); imgView.setPadding(20,20,20,10); imgView.setAdjustViewBounds(true); layout1.addView(imgView); LinearLayout layout2 = new LinearLayout(MainActivity.this); layout2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); layout2.setOrientation(LinearLayout.HORIZONTAL); listLayout.addView(layout2); TextView listText = new TextView(MainActivity.this); listText.setTextColor(Color.BLACK); listText.setPadding(30,0,0,0); listText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40); listText.setTypeface(maze,maze.BOLD); listText.setId(View.generateViewId()); layout2.addView(listText); LinearLayout cartlayout = new LinearLayout(MainActivity.this); cartlayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); cartlayout.setOrientation(LinearLayout.HORIZONTAL); cartlayout.setPadding(0,0,30,0); cartlayout.setGravity(Gravity.RIGHT); // layout3.setPadding(0,10,0,20); layout2.addView(cartlayout); ImageView addCartView = new ImageView(MainActivity.this); addCartView.setImageResource(R.drawable.add); // addCartView.setPadding(200,0,0,0); cartlayout.addView(addCartView); TextView NOFProd = new TextView(MainActivity.this); NOFProd.setId(View.generateViewId()); NOFProd.setTypeface(rupeeSymbol); NOFProd.setPadding(10,20,10,0); NOFProd.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25); // NOFProd.s NOFProd.setText("0"); cartlayout.addView(NOFProd); ImageView removeCartView = new ImageView(MainActivity.this); removeCartView.setImageResource(R.drawable.remove); // removeCartView.setPadding(20,5,20,0); cartlayout.addView(removeCartView); TextView descText = new TextView(MainActivity.this); descText.setTextColor(Color.DKGRAY); descText.setPadding(30,0,0,0); descText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); descText.setTypeface(epicselfie); descText.setText("Swadist Bhojan"); descText.setId(View.generateViewId()); listLayout.addView(descText); // price and others Textview LinearLayout layout3 = new LinearLayout(MainActivity.this); layout3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); layout3.setOrientation(LinearLayout.HORIZONTAL); layout3.setPadding(0,10,0,20); listLayout.addView(layout3); TextView pricesymbol = new TextView(MainActivity.this); pricesymbol.setId(View.generateViewId()); pricesymbol.setTypeface(rupeeSymbol); pricesymbol.setPadding(30,10,0,0); pricesymbol.setText("`"); layout3.addView(pricesymbol); TextView priceText = new TextView(MainActivity.this); priceText.setId(View.generateViewId()); priceText.setTypeface(novaoval); priceText.setPadding(10,10,0,0); priceText.setText(": 200"); layout3.addView(priceText); TextView rating = new TextView(MainActivity.this); rating.setId(View.generateViewId()); rating.setPadding(50,0,0,0); rating.setTypeface(novaoval); rating.setText("Rating : 3.5"); layout3.addView(rating); TextView any = new TextView(MainActivity.this); any.setPadding(50,0,0,0); any.setTypeface(novaoval); any.setId(View.generateViewId()); any.setText("anything "); layout3.addView(any); listText.setText(super.getItem(position)); return listLayout; } } }