gpt4 book ai didi

android - 如何在android中加载数据时避免黑屏

转载 作者:行者123 更新时间:2023-11-29 16:02:15 26 4
gpt4 key购买 nike

我正在从 android 中的远程服务器获取数据。加载数据时,它显示黑屏并持续 7-8 秒。这有时会导致 android 显示 ANR 对话框。我已将 asynctask 用于数据的后台加载,但黑屏仍然存在。我也看不到 Activity 栏中不确定的进度。我究竟做错了什么?我正在粘贴下面的代码 -

public class ItemActivity extends Activity {

private String str = null;
private ListView listview;
private LazyItemLoadAdapter adapter;
private Item[] item_data = null;

private Item[] filteredItems = null;

private int id;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_item);

ActionBarUtils.setActionBar(this);

id = getIntent().getExtras().getInt("id");

listview = (ListView) findViewById(R.id.listViewAllItems);

AsyncData data=new AsyncData();
data.execute(String.valueOf(id), "http://kurdshopping.net/apj/adlist.php");
try {
item_data=data.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (item_data == null || item_data.length == 0) {
listview.setVisibility(View.GONE);
return;
}
adapter = new LazyItemLoadAdapter(this, R.layout.listview_item_row, item_data);

listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
int itemid = item_data[position].id;
Toast.makeText(ItemActivity.this, String.valueOf(itemid),
Toast.LENGTH_SHORT).show();
if (id == 96) {
Intent carIntent = new Intent(ItemActivity.this,
CarActivity.class);
carIntent.putExtra("id", itemid);
startActivity(carIntent);
} else {
Intent adIntent = new Intent(ItemActivity.this,
AdActivity.class);
adIntent.putExtra("id", itemid);
startActivity(adIntent);
}
}
});

}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
if (item_data == null || item_data.length == 0) {
menu.getItem(0).setEnabled(false);
}
return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.item, menu);
return true;
}


@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
Intent optionsIntent = new Intent(ItemActivity.this,
OptionsActivity.class);
optionsIntent.putExtra("id", id);
startActivityForResult(optionsIntent, 1);
break;
}
return super.onMenuItemSelected(featureId, item);
}

private class AsyncData extends AsyncTask<String, Void, Item[]>{

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
setProgressBarIndeterminateVisibility(true);
}

@Override
protected Item[] doInBackground(String... arg0) {
Item[] item_data=null;
try {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters
.add(new BasicNameValuePair("id", arg0[0]));
str = CustomHttpClient.executeHttpPost(
arg0[1], postParameters);
JSONArray array = new JSONArray(str);
item_data = new Item[array.length()];

for (int i = 0; i < item_data.length; i++) {
JSONObject jdata = array.getJSONObject(i);
String path = "http://kurdshopping.net/thumbs/"
+ jdata.getString("name");
int itemid = jdata.getInt("id");
item_data[i] = new Item();
item_data[i].id = itemid;
item_data[i].imageUrl = path;
item_data[i].city = jdata.getString("city_name");
item_data[i].make = jdata.getString("cc_name");
item_data[i].model = jdata.getString("cm_name");
item_data[i].price = jdata.getString("price");
item_data[i].text = jdata.getString("title") + "\nPrice: "
+ jdata.getString("price");
Log.i("break", "break");
}
} catch (NotFoundException n) {
Log.e("ItemActivity", n.getMessage());
} catch (JSONException j) {
Log.e("ItemActivity", j.getMessage());
} catch (Exception e) {
Log.e("ItemActivity", e.getMessage());
}
return item_data;
}

@Override
protected void onPostExecute(Item[] result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
setProgressBarIndeterminateVisibility(false);
}

}
}

感谢任何帮助。提前致谢。

最佳答案

data.get() 调用完全阻塞 UI 线程,直到 AsyncTask 完成其处理。

您应该将这 2 个语句移到 onPostExecute() 的末尾:

   adapter = new LazyItemLoadAdapter(this, R.layout.listview_item_row, item_data);

listview.setAdapter(adapter);

而且您必须摆脱 data.get() 调用周围的整个 try/catch block 。

最后,作为进一步的细化,你应该考虑是否应该将这两行移动到onResume()中,以便在每次重新显示Activity时刷新数据:

    AsyncData data=new AsyncData();
data.execute(String.valueOf(id), "http://kurdshopping.net/apj/adlist.php");

关于android - 如何在android中加载数据时避免黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23210859/

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