gpt4 book ai didi

java - 在运行通用图像加载器时添加 imageUrl?

转载 作者:行者123 更新时间:2023-12-02 05:48:56 25 4
gpt4 key购买 nike

我已经使用 gridview 布局将通用图像加载器实现到我的应用程序中。

当我按下按钮时,我想将新的 imageUrl 添加到 URL 数组中。

我设法做到了这一点,但是当我刷新 imageList 时,我仍然只能看到启动时数组中的图像。

如何使用新数组更新 gridView?

编辑

主要 Activity

public class MainActivity extends Activity {

Button btnView;
private ListView listView;
Socket client;
PrintWriter printwriter;
EditText textField;
Button button;
String messsage;
ObjectInputStream input;
ObjectOutputStream output;
int counter = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);
setContentView(R.layout.ac_home);

//startRunning();



}

public void onImageGridClick(View view) {
Intent intent = new Intent(this, ViewImages.class);
startActivity(intent);
}


public void onTakeImageClick(View view) throws IOException {
ViewImages.addImg();
}

}

这是 ViewImage 类

public class ViewImages extends Activity {

private ListView listView;

ImageLoader loader;
private static ImageListAdapter adapter;
private static List<String> mItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mItems = new ArrayList<String>();
mItems.add("http://195.178.234.228/images/image8.jpg");
mItems.add("http://195.178.234.228/images/image7.jpg");


adapter = new ImageListAdapter(this, mItems);

//


listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(adapter);
}

public static void addImg(){
mItems.add("http://195.178.234.228/images/image10.jpg");
adapter.notifyDataSetChanged();

}




}

图像列表适配器

public class ImageListAdapter extends BaseAdapter {

List<String> list;


int size;

private Context context;
private ImageLoader imageLoader;

public ImageListAdapter(Context context,List<String> list) {
this.context = context;
this.list = list;
imageLoader = ImageLoader.getInstance();
imageLoader.clearDiscCache();
imageLoader.clearMemoryCache();
}

@Override
public int getCount() {
return list.size();
}


@Override
public Object getItem(int arg0) {
return null;
}



@Override
public long getItemId(int arg0) {
return 0;
}



@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View v = convertView;

ViewHolder vh = null;
if (v == null) {
v = View.inflate(context, R.layout.list_item, null);

vh = new ViewHolder();
vh.imageView = (ImageView) v.findViewById(R.id.imageView);

v.setTag(vh);
}
else {
vh = (ViewHolder)v.getTag();
}

DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisc()
.build();

imageLoader.displayImage(list.get(position), vh.imageView, options);

return v;
}

private class ViewHolder {
ImageView imageView;
}

}

最佳答案

在 gridView 适配器上使用 notifyDataSetChanged 来反射(reflect) gridview 中的更改。每当有新的 imageUrl 添加到 URL 数组时,请通过以下方式刷新适配器:

adapter.notifyDataSetChanged();

关于java - 在运行通用图像加载器时添加 imageUrl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23746466/

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