2014-05-22 70 views
0

我已經在Gridview佈局的應用程序中實現了Universal Image Loader。正在更新listView/refreshing適配器

當我按下一個按鈕,我想要添加一個新的imageUrl到Url數組。

我設法做到了這一點,但是當我刷新我的imageList時,我仍然只能看到在啓動數組中的圖像。

如何更新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; 
} 

} 

回答

1

您的方法onTakeImageClick()沒有任何效果,如果您需要將圖像URL添加到ViewImages Activity,則一種方法是通過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上查看。

+0

謝謝,我會檢查出來! – theOGloc

+0

嗯,我只能添加一個圖像到列表中。 – theOGloc

0

如果您發送的意圖IMAGEURL,您還可以發送IMAGEURL的數組,所以你就可以添加更多的圖像

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