这是我从服务器检索数据的代码,每次调用listview。如果有人可以帮我追加或者某事
,那么这些项目就会重复我的代码:
package NewsTabFragment;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.subhi.tabhost.AppController;
import com.subhi.tabhost.R;
import java.util.List;
/**
* Created by tb_laota on 9/21/2015.
*/
public class Adapter extends BaseAdapter{
private LayoutInflater inflater;
private Activity activity;
private List<Item> items;
ImageLoader imageLoader= AppController.getmInstance().getmImageLoader();
public Adapter(Activity activity,List<Item> items){
this.activity=activity;
this.items=items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(inflater==null){
inflater=(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if(convertView ==null){
convertView=inflater.inflate(R.layout.custom_layout,null);
}
if(imageLoader==null)
imageLoader=AppController.getmInstance().getmImageLoader();
NetworkImageView imageView= (NetworkImageView) convertView.findViewById(R.id.image_view);
TextView news_title=(TextView)convertView.findViewById(R.id.news_title);
TextView news_text= (TextView) convertView.findViewById(R.id.news_text);
// TextView url= (TextView) convertView.findViewById(R.id.url);
TextView time_stamp= (TextView) convertView.findViewById(R.id.time_stamp);
TextView news_url= (TextView) convertView.findViewById(R.id.news_url);
//getting data for row
Item item=items.get(position);
imageView.setImageUrl(item.getImage(), imageLoader);
news_text.setText(item.getNews_text());
news_url.setText(item.getUrl());
time_stamp.setText(item.getTime_stamp());
news_title.setText(item.getNews_title());
return convertView;
}
}
我在互联网上看过一些教程和博客,但对我来说没什么用。
答案 0 :(得分:3)
执行以下操作:
public class Adapter extends BaseAdapter{
private LayoutInflater inflater;
private Activity activity;
private List<Item> items;
ImageLoader imageLoader= AppController.getmInstance().getmImageLoader();
/* Create a class which contains view elements */
static class Holder{
NetworkImageView imageView;
TextView news_title;
TextView news_text;
TextView news_url
TextView time_stamp
}
public Adapter(Activity activity,List<Item> items){
this.activity=activity;
this.items=items;
}
/* Helper method to use, when updating the list */
public void updateItems(List<Item> items){
if(items != null){
// Either clear the list, or do some logic for skipping duplicates
this.items.clear();
this.items.addAll(items);
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder rowHolder;
if(convertView ==null){
convertView = LayoutInflater.from(activity).inflate(R.layout.custom_layout,null);
rowHolder = new Holder();
rowHolder.imageView = (NetworkImageView) convertView.findViewById(R.id.image_view);
rowHolder.news_title =(TextView)convertView.findViewById(R.id.news_title);
rowHolder.news_text= (TextView) convertView.findViewById(R.id.news_text);
rowHolder.time_stamp= (TextView) convertView.findViewById(R.id.time_stamp);
rowHolder.news_url= (TextView) convertView.findViewById(R.id.news_url);
// tell convertView to set its Tag to the Holder object
convertView.setTag(rowHolder);
}else{
// for recycling
holder = (Holder) convertView.getTag();
}
if(imageLoader==null)
imageLoader = AppController.getmInstance().getmImageLoader();
//getting data for row
final Item item = items.get(position);
// null check the item
if(item != null){
holder.imageView.setImageUrl(item.getImage(), imageLoader);
holder.news_text.setText(item.getNews_text());
holder.news_url.setText(item.getUrl());
holder.time_stamp.setText(item.getTime_stamp());
holder.news_title.setText(item.getNews_title());
}
return convertView;
}
}
然后当您从服务器获取新数据时,只需调用:
mAdapter.updateItems(mItems);
其中mItems是您从服务器检索的List
答案 1 :(得分:1)
每次调用WebService后都可能会添加对象。只需在将新对象添加到ArrayList之前清除数组。