簡體   English   中英

Android-在列表視圖中滾動底部時,從mysql數據庫中查看更多舊數據(不是圖像)

[英]Android - view more old data (not image) from mysql database, When scroll bottom in listview

我不想在啟動時第一次加載所有數據。 它效率不高,所以我可以加載列表中的前10或15個項目。 當我滾動它們時,還會加載ListView的行。 我怎樣才能做到這一點? 這是我的代碼:

public class BerandaFragment extends Fragment implements OnRefreshListener{

SwipeRefreshLayout swipeContainer;

String myJSON;

private static final String TAG_RESULTS="result";
private static final String ID = "eventID";
private static final String JUDUL= "judul";
private static final String TANGGAL = "tanggal";
private static final String JAM = "jam";
private static final String LOKASI = "lokasi";
private static final String KETERANGAN = "keterangan";

JSONArray peoples = null;

ArrayList<HashMap<String, String>> personList;

ListView list;

public static BerandaFragment newInstance(){

    BerandaFragment fragment = new BerandaFragment();
   return fragment;

}

public BerandaFragment(){

}

@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.fragment_beranda, container, false); 

    swipeContainer = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
    // Setup refresh listener which triggers new data loading
    swipeContainer.setOnRefreshListener(this);  
    // Configure the refreshing colors
    swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright, 
            android.R.color.holo_green_light, 
            android.R.color.holo_orange_light, 
            android.R.color.holo_red_light);

    list = (ListView) rootView.findViewById(R.id.listView);
    personList = new ArrayList<HashMap<String,String>>();
    getData();

    return rootView;
}


@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);

    ((MainActivity) activity).onSectionAttached(1);

}

protected void showList(){
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray(TAG_RESULTS);

        for(int i=0;i<peoples.length();i++){
            JSONObject c = peoples.getJSONObject(i);
            String eventID = c.getString(ID);
            String judul = c.getString(JUDUL);
            String tanggal = c.getString(TANGGAL);
            String jam = c.getString(JAM);
            String lokasi = c.getString(LOKASI);
            String keterangan = c.getString(KETERANGAN);

            HashMap<String,String> persons = new HashMap<String,String>();

            persons.put(ID,eventID);
            persons.put(JUDUL,judul);
            persons.put(TANGGAL,tanggal);
            persons.put(JAM,jam);
            persons.put(LOKASI,lokasi);
            persons.put(KETERANGAN,keterangan);

            personList.add(persons);
        }

        Collections.sort(personList,new Comparator<HashMap<String, String>>() {
            @Override
            public int compare(HashMap<String, String> arg0,
                    HashMap<String, String> arg1) {
                // TODO Auto-generated method stub
                String id0 = arg0.get("ID");
                String id1 = arg1.get("ID");
                if (id0 == null) return -1;
                return id1.compareTo(id0);
            }
        });

        ListAdapter adapter = new SimpleAdapter(
                getActivity(), personList, R.layout.list_beranda,
                new String[]{ID,JUDUL,TANGGAL,JAM,LOKASI,KETERANGAN},
                new int[]{R.id.eventID, R.id.judul, R.id.tanggal, R.id.jam, R.id.lokasi, R.id.keterangan}
        );

        list.setAdapter(adapter);


    } catch (JSONException e) {
        e.printStackTrace();
    }

}

public void getData(){
    class GetDataJSON extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://kpdewantika.esy.es/semua_event.php");

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {
                // Oops
            }
            finally {
                try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result){
            myJSON=result;
            showList();
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
}

@Override
public void onRefresh() {
    // TODO Auto-generated method stub
    new Handler().postDelayed(new Runnable() {
        @Override public void run() {
            personList.clear();
            getData();
            swipeContainer.setRefreshing(false);
        }
    }, 5000);
}

當我在底部滾動時,如何在列表視圖中加載更多數據行(不是圖像),因為在數據庫中,我直到+100行數據。 我在互聯網上看到過教程,但沒有成功。 也許在這里,解決了我的問題。

謝謝。

您想從mysql數據庫中獲取數據! 就像在Facebook或instagram等中完成一樣,這是可能對您有幫助的鏈接:) https://github.com/jparkie/GivesMeHopeAndroidClient/blob/new-v2/AnthologyForGMH_6.apk

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM