gpt4 book ai didi

android - 在 AsyncTask Android 的 MainLayout 中设置 LayoutInflator

转载 作者:行者123 更新时间:2023-11-30 03:27:55 25 4
gpt4 key购买 nike

我想将 Layout Inflator 添加到我的主视图,但它没有添加。我的应用程序崩溃了。

这是我的代码:

mainLayout = (LinearLayout) findViewById(R.id.user_list);
li = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
tempView = li.inflate(R.layout.user_rev_listitem, null);

私有(private)类 MyTask 扩展 AsyncTask { 私有(private)上下文 mContext; 私有(private) View rootView;

    public MyTask(Context applicationContext, View tempView) {
// TODO Auto-generated constructor stub
this.mContext=applicationContext;
this.rootView=tempView;

}

@Override
protected void onPostExecute(String result) {


tareef_nm = (TextView) rootView.findViewById(R.id.tareef_name);
tareef_des = (TextView) rootView.findViewById(R.id.tareef);

for (int i = 0; i < myList.size(); i++){


JSONObject p = myList.get(i);



try {

tareef_nm.setText(p.getString(TAG_UserName));
tareef_des.setText(p.getString(TAG_UserReviews));



} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}




mainLayout.addView(tempView);


}



super.onPostExecute(result);
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub




super.onPreExecute();
}

@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);

}

@Override
protected String doInBackground(String... params) {


String myRespone = null;
String url = params[0];
HttpClient client = new DefaultHttpClient();

HttpGet Get = new HttpGet(url);

try {
HttpResponse response = client.execute(Get);

HttpEntity entity = response.getEntity();
myRespone = EntityUtils.toString(entity);

} catch (ClientProtocolException e) {

e.printStackTrace();

Log.e("My webservice Response", "ClientProtocolException");

} catch (IOException e) {

Log.e("My webservice Response", "IOException");

e.printStackTrace();
}
JSONObject jsonObj;

if (myRespone != null) {
try {



jsonObj = new JSONObject(myRespone);
JSONArray jsonArray = jsonObj.getJSONArray("Results");


for (int i = 0; i <= jsonArray.length() - 1; i++) {
myList.add(jsonArray.getJSONObject(i));

}



} catch (Exception e) {
e.printStackTrace();
}
} else {
// do nothing
}
return null;
}

}

最佳答案

您的应用程序崩溃是因为您在每次迭代中都将相同的 tempView 添加到 mainLayout。我不明白你在做什么。如果你想修复,在每次迭代中你必须再次膨胀它,例如:

for (int i = 0; i < myList.size(); i++){

        tempView = li.inflate(R.layout.user_rev_listitem, null);
JSONObject p = myList.get(i);

....

关于android - 在 AsyncTask Android 的 MainLayout 中设置 LayoutInflator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17941139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com