PullToRefreshListView 2.0 【ArrayList与LinkedList】

本文详细介绍了如何使用Java中的LinkedList和ArrayList类进行数据添加操作,并通过AsyncTask实现了数据刷新,展示了两种集合类在实际应用中的不同用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用LinkedList。。。。LinkedList.addFirst(“helloworld”);


package com.example.helloworld;

import java.util.Arrays;
import java.util.LinkedList;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;


public class MainActivity extends Activity {

     private String[] mStrings = { "123", "1234", "12345"};  

    private PullToRefreshListView lV;
    private ArrayAdapter<String> adapter;
    private LinkedList<String> mListItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListItems = new LinkedList<String>();
        mListItems.addAll(Arrays.asList(mStrings));
        lV = (PullToRefreshListView) findViewById(R.id.mylv);
        adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,mListItems);
        lV.setAdapter(adapter);

        lV.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {

                new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... arg0) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }

                    protected void onPostExecute(Void result) {
                        mListItems.addFirst("dajiahao");
                        adapter.notifyDataSetChanged();
                        lV.onRefreshComplete();
                    };

                }.execute();


            }
        });

    }

}

这里写图片描述


使用ArrayList。。。。arrayList.add(0,”helloworld”);


package com.example.helloworld;

import java.util.ArrayList;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SecondActivity extends Activity {

    private PullToRefreshListView myListView;
    private ArrayList<String> arrayList;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        arrayList = new ArrayList<String>();
        arrayList.add("hello");
        arrayList.add("hongye");
        myListView = (PullToRefreshListView) findViewById(R.id.myListView);
        adapter = new ArrayAdapter<String>(SecondActivity.this,
                android.R.layout.simple_list_item_1, arrayList);
        myListView.setAdapter(adapter);
        myListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {

                new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected Void doInBackground(Void... arg0) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        arrayList.add(0, "secondActivity");
                        adapter.notifyDataSetChanged();
                        myListView.onRefreshComplete();
                        super.onPostExecute(result);
                    }

                }.execute();

            }
        });
    }

}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值