gpt4 book ai didi

android - 在 android 中如何更新自定义 ListView 的内容

转载 作者:行者123 更新时间:2023-11-29 22:19:06 25 4
gpt4 key购买 nike

在我的自定义 ListView 中包含两个 textview 和一个 Imageview 我我使用 AsyncTask 从 Internet 读取与 imageView 相同的文本。因为我同时读取和分配所有三个 View 元素需要很多时间。在这种情况下,当文本部分完成时,我需要在另一个 AsyncTask 中将 url 转换为位图。 作为一种逻辑,它需要一些更新我的 ImageView 资源的概念。但我不这样做 知道怎么做....

提前致谢..

private class AsynchTask extends AsyncTask<Void, Integer, Void> {
URLConnection tc;
BufferedReader in;
URL twitter;
int num=0;
@Override
protected void onPreExecute() {
super.onPreExecute();
try {

mProgressBar.setVisibility(View.VISIBLE);
} catch (Exception e) {
Log.e(TAG,""+e.getMessage());
}
}
@Override
protected Void doInBackground(Void... params) {
try{
twitter = new URL("https://twitter.com/statuses/public_timeline.json");
tc = twitter.openConnection();
my = new ArrayList<HashMap<String,Object>>();
in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
ImageList=new ArrayList<String>();
while ((line = in.readLine()) != null) {

JSONArray ja = new JSONArray(line);

for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
/**Data Insert into the HashMap Object*/
hm=new HashMap<String, Object>();
hm.put(TEXT,jo.getString("text"));
hm.put(USER,jo.getJSONObject("user").getString("name"));
// String str=jo.getJSONObject("user").getString("profile_image_url"); hm.put(URL,"http://twitter.com/#!/"+jo.getJSONObject("user").getString("screen_name"));
// hm.put(IMAGEURL,getDrawable_from_url(str));
ImageList.add(jo.getJSONObject("user").getString("profile_image_url"));
Log.e(TAG,""+num);
my.add(hm);
num++;
Log.e("Count",""+num);
publishProgress(num);
}
num++;
publishProgress(num);
}
} catch (Exception e) {

Log.e(TAG,""+e.getMessage());
}
return null;
}

@Override
protected void onProgressUpdate(Integer... values) {
mProgressBar.setProgress(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mProgressBar.setProgress(0);
mProgressBar.setVisibility(View.GONE);

adapter = new Simpleadapter(HelloWorldActivity.this, my, R.layout.listcontent,
new String[]{TEXT,USER}, new int[]{R.id.text2,R.id.text1});
listView.setAdapter(adapter);
new AsynchTaskForImageLoading().execute();
}
}


/**Method to convert Url to the Bitmap*/


private Bitmap getDrawable_from_url(String url) {


try{
Bitmap x;
HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
connection.setRequestProperty("User-agent","Mozilla/4.0");
connection.connect();
InputStream input = connection.getInputStream();
x = BitmapFactory.decodeStream(input);

return x;
}
catch (Exception e) {
Log.e(TAG,""+e.getMessage());
return null;
}

}

最佳答案

我已经成功地使用了这个 LazyList:https://github.com/thest1/LazyList

根据您的需要,您可以将提供的 stub 图像换成您想要使用的图像。我还使用 1x1 空白 png 来显示无图像。

另外,我在我的代码中所做的一项更改是我强烈建议在使用此程序包时将代码从使用 SD 卡更改为使用内置缓存。为此,您可以将 FileCache.java 文件从使用 .getExternalStorageDirectory() 修改为 .getCacheDir()。

希望这对您有所帮助。

关于android - 在 android 中如何更新自定义 ListView 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7750801/

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