从xml更新列表视图的问题

时间:2012-10-23 12:03:55

标签: android listview adapter

我有一个由lazyadapter(baseadpter)从xml文件构建的listview

例如:urlxml - >解析器---> baseadapter --->列表视图

当我修改服务器上的xml文件时,我想刷新listview并查看其中的行已更改。顺便说一句,我使用lib“拉动刷新”。

当我拉动刷新时,我得到了新的行来处理旧行。

我是否必须首先删除listview内容并重复此操作: urlxml - >解析器---> baseadapter --->列表视图

出了什么问题?

编辑一些代码

我的适配器

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>> ();

    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 

    public LazyAdapter(Activity a,  ArrayList<ArrayList<String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.get(0).size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)

        vi = inflater.inflate(R.layout.elenco_articoli_items, null);


        String Tt = data.get(0).get(position);
        String URl = data.get(1).get(position);

        TextView text=(TextView)vi.findViewById(R.id.title);;
        ImageView image=(ImageView)vi.findViewById(R.id.image);
        text.setText(Html.fromHtml(Tt));
        imageLoader.DisplayImage(URl, image,1);
        return vi;
    }

}

我的主要活动

public class Lista_articoli_categoria extends Activity {

    ArrayList<String> images = new ArrayList<String> ();
    ArrayList<String> title = new ArrayList<String> ();
    ArrayList<String> author = new ArrayList<String> ();
    ArrayList<String> description = new ArrayList<String> ();

    private LazyAdapter adapter;
    private RefreshableListView mListView;

    Parser task = new Parser(); 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.elenco_articoli);

        try {

            task.execute();
            final RefreshableListView list = (RefreshableListView) findViewById(R.id.refreshablelistview);    
            adapter= new LazyAdapter(this, fill_data(task.get()));
            mListView = list;   
            list.setAdapter(adapter);
            list.setOnUpdateTask(new OnUpdateTask() {

            public void updateBackground() {
                refresh_data(); 

                try {
                    Thread.sleep(1500);
                    } catch (InterruptedException e) {
                    e.printStackTrace();
                    }
            }

            public void updateUI() {
                    adapter.notifyDataSetChanged();
            }

            public void onUpdateStart() {

            }

            });


        } catch (InterruptedException e) {
                e.printStackTrace();
        } catch (ExecutionException e) {
                e.printStackTrace();
        }

    mListView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
                /*
                TODO     
                */
      }
    });   
}

    @Override
    public void onDestroy()
    {
        mListView.setAdapter(null);
        super.onDestroy();
    }

    public ArrayList<ArrayList<String>> fill_data(ArrayList<Struttura_rss> Datidaxml){

        ArrayList<ArrayList<String>> rec = new ArrayList<ArrayList<String>> ();

           for(int i=0;i<Datidaxml.size();i++){
                    Struttura_rss p = Datidaxml.get(i);
                    title.add(p.getTitle());
                    images.add(p.getImage());
                    description.add(p.getArticolo());
                    author.add(p.getAuthor());
            }           

            rec.add (title);
            rec.add (images);       
            rec.add (description);
            rec.add (author);   

            return rec;
    }



    public void refresh_data(){

        try { 

            Parser task = new Parser(); 
            task.execute();
            adapter= new LazyAdapter(this, fill_data(task.get()));

            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

    }

}

1 个答案:

答案 0 :(得分:0)

如果你看一下这个例子,你应该很容易理解......

链接:https://github.com/chrisbanes/Android-PullToRefresh/blob/master/sample/src/com/handmark/pulltorefresh/samples/PullToRefreshListActivity.java

在刷新时,您只需要下载新项目(例如,通过向Web服务发送时间戳或ID)

    @Override
    protected String[] doInBackground(Void... params) {
        // Simulates a background job.
        try {
            //Go to webservice
        } catch (InterruptedException e) {
        }
        return mStrings;
    }

然后,只解析新项目并在此处添加:

    @Override
    protected void onPostExecute(String[] result) {
        mListItems.addFirst("Added after refresh...");
        mAdapter.notifyDataSetChanged();

        // Call onRefreshComplete when the list has been refreshed.
        mPullRefreshListView.onRefreshComplete();

        super.onPostExecute(result);
    }

编辑:如果你不能只下载所需的项目(RSS feed),请下载evrything并在onPostExecute中添加正确的项目。

if(!isALreadyInTheList(currentItem))
   mListItems.addFirst(currentItem);
else
   doNothing();