0

我有一个由lazyadapter(baseadpter)从xml文件构建的列表视图

例如:urlxml --> parser ---> baseadapter ---> listview

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

当我拉动刷新时,我得到了新行和旧行。

我是否必须先删除 listview 内容并重复此操作:urlxml --> parser ---> baseadapter ---> listview

怎么了?

编辑一些代码

我的适配器

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

    }

}
4

1 回答 1

0

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

链接:https ://github.com/chrisbanes/Android-PullToRefresh/blob/master/sample/src/com/handmark/pullorefresh/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 提要),请下载 evrything 并在 onPostExecute 中添加正确的项目。

if(!isALreadyInTheList(currentItem))
   mListItems.addFirst(currentItem);
else
   doNothing();
于 2012-10-23T12:09:58.437 回答