更新listView /刷新适配器

时间:2014-05-22 13:26:29

标签: java android universal-image-loader

我已经使用gridview布局将Universal Image Loader实现到我的应用程序中。

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

我设法做到了,但是当我刷新我的imageList时,我仍然只看到StartUp上的数组中的图像。

如何使用新数组更新gridView?

修改

MainActivity

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(); 

}




}

ImageListAdapter

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;
}

}

2 个答案:

答案 0 :(得分:1)

您的方法onTakeImageClick()没有任何效果,如果您需要将图像网址添加到ViewImages活动,一种方法是通过intent.putExtra()方法。

Intent intent = new Intent(this, ViewImages.class);
intent.putExtra("ImageName" , "ImageUrl");
startActivity(intent);

你可以通过以下方式获取额外费用: -

getIntent().getStringExtra("ImageName");

其他东西在你的imageViewAdapter中,方法getItem()返回null而不是返回

list.get(arg0);

此方法将返回位置arg0处的视图。

答案 1 :(得分:0)

如果你是在意图中发送imageUrl,你也可以发送imageurl数组,这样你就可以添加更多图像

   Intent intent = new Intent(this, ViewImages.class);
   intent.putExtra("ImageName" , new String[]{"ImageUrl","ImageUrl","ImageUrl"});
   startActivity(intent);