我正在开发一个应用程序,它将所有来自服务器的数据都放在列表视图中,因为我正在使用lazyadapter。现在我想刷新列表视图中的所有数据(每当单击按钮删除当前数据并仅更新来自服务器的新数据) )。我正在使用notifyDataSetChanged().
它不工作。请帮助我
我的代码:
Activity
public class JamLocationsActivity extends Activity {
private ListView listView;
private String[] imageUrls;
private ArrayList<String> imagesArray;
private ArrayList<String> descArray;
private ArrayList<String> dateTimeArray;
private ArrayList<String> titleArray;
private ArrayList<String> userArray;
private LazyAdapter adapter;
private ArrayList<HashMap<String, String>> latlngArray;
private RecieveDataDTO receivingDatDto;
private Button btn;
private static ImageLoader imageLoader;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar bar = getActionBar();
bar.setDisplayShowHomeEnabled(false);
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowCustomEnabled(true);
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
imageUrls=Constants.IMAGES;
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.action_bar_jamlocation);
setContentView(R.layout.activity_jam_locations);
imageLoader = ImageLoader.getInstance();
JamLocationsActivity.imageLoader.init(ImageLoaderConfiguration.createDefault(this));
imagesArray =
(ArrayList<String>)getIntent().getSerializableExtra("imgArry");
descArray =
(ArrayList<String>)getIntent().getSerializableExtra("descArry");
dateTimeArray =
(ArrayList<String>)getIntent().getSerializableExtra("dateTimeArry");
titleArray =
(ArrayList<String>)getIntent().getSerializableExtra("titleArr");
userArray =
(ArrayList<String>)getIntent().getSerializableExtra("userArr");
latlngArray =(ArrayList<HashMap<String,
String>>)getIntent().getSerializableExtra("latlngArr");
Log.e("ARRAY", latlngArray.toString());
receivingDatDto = new RecieveDataDTO();
receivingDatDto.setImageArr(imagesArray);
receivingDatDto.setTitleArr(titleArray);
receivingDatDto.setUserArr(userArray);
receivingDatDto.setDateTimeArr(dateTimeArray);
receivingDatDto.setDescArr(descArray);
//receivingDatDto.setLatLngArr(latlngArray);
adapter = new LazyAdapter(JamLocationsActivity.this,receivingDatDto);
listView=(ListView)findViewById(R.id.listView1);
listView.setAdapter(adapter);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
adapter.clearAdapter();
adapter.notifyDataSetChanged();
}
});
}
懒惰适配器
public class LazyAdapter extends BaseAdapter{
static Context context;
private TextView timeTextView;
private ListView listView;
private String imageurl[]=null;
private ArrayList<String> imgUrl;
private ArrayList<String> images;
private ArrayList<String> users;
private ArrayList<String> dateTimes;
private ArrayList<String> titles;
private ArrayList<String> descriptions;
private ArrayList<HashMap<String, String>> latlngs;
DisplayImageOptions doption=null;
private ImageLoadingListener animateFirstListener =null;
RecieveDataDTO receivedata= new RecieveDataDTO();
// static ImageLoader imageLoader = ImageLoader.getInstance();
public LazyAdapter(Context c,RecieveDataDTO dto){
context=c;
//this.imageurl=url;
// this.imgUrl = url;
this.images = dto.getImageArr();
this.users = dto.getUserArr();
this.descriptions=dto.getDescArr();
this.dateTimes=dto.getDateTimeArr();
this.titles=dto.getTitleArr();
this.latlngs=dto.getLatLngArr();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
//return imageurl.length;
return users.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view=convertView;
ViewHolder viewHolder;
Log.e("ADAPTER", images.toString());
//viewHolder.mydate =
java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
AQuery androidAQuery = new AQuery(context);
if(convertView==null){
LayoutInflater inflater=(LayoutInflater)context.
getSystemService(context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.custom_data_list, null);
viewHolder = new ViewHolder();
viewHolder.shareBtn=(Button)view.findViewById(R.id.shareBtn);
viewHolder.mapButton=(Button)view.findViewById(R.id.reportBtn);
viewHolder.imageView2=(ImageView)view.findViewById(R.id.imageView2);
viewHolder.imageView1=(ImageView)view.findViewById(R.id.imageView1);
animateFirstListener = new AnimateFirstDisplayListener();
viewHolder.addressTxtView=(TextView) view.findViewById(R.id.adddTextId);
viewHolder.headTextView = (TextView)view.findViewById(R.id.headTextId);
viewHolder.dateTextView=(TextView) view.findViewById(R.id.dateTextId);
viewHolder.userTextView=(TextView)view.findViewById(R.id.textView1);
view.setTag(viewHolder);
}else{
viewHolder=(ViewHolder)view.getTag();
}
if(images.get(position).equals("")){
viewHolder.imageView2.setVisibility(View.GONE);
}
else{
androidAQuery.id(viewHolder.imageView2).image(images.get(position), true, true);
/*ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(images.get(position), viewHolder.imageView2,
animateFirstListener);*/
}
// androidAQuery.id(viewHolder.imageView2).image(Constants.IMAGES[position], true,
true);
viewHolder.addressTxtView.setText(descriptions.get(position));
viewHolder.dateTextView.setText(dateTimes.get(position));
viewHolder.headTextView.setText(titles.get(position));
viewHolder.userTextView.setText(users.get(position));
if(users.get(position).equals("police")){
viewHolder.imageView1.setImageResource(R.drawable.common_signin_btn_icon_dark);
}
else{
viewHolder.imageView1.setImageResource(R.drawable.common_signin_btn_icon_light);
}
/* ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage("", viewHolder.imageView2);
imageLoader.displayImage(imgUrl.get(position), viewHolder.imageView2, animateFirstListener);*/
viewHolder.shareBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(context,AndroidFacebookSample.class);
intent.putExtra("fbVal", descriptions.get(position));
intent.putExtra("trTime", dateTimes.get(position));
context.startActivity(intent);
}
});
viewHolder.mapButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(context,SingleJamLocationActivity.class);
context.startActivity(intent);
}
});
viewHolder.imageView2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("You", "Clicked Me");
Intent intent = new Intent(context, ImagePerActivity.class);
intent.putExtra("imageurlpostion", images);
intent.putExtra("imagepostion", position);
context.startActivity(intent);
}
});
return view;
}
private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
static final List<String> displayedImages = Collections.synchronizedList(new
LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}