Android - 从 mysql 数据库中查看更多旧数据(不是图像),当在列表视图中滚动底部时
Posted
技术标签:
【中文标题】Android - 从 mysql 数据库中查看更多旧数据(不是图像),当在列表视图中滚动底部时【英文标题】:Android - view more old data (not image) from mysql database, When scroll bottom in listview 【发布时间】:2016-02-01 08:47:57 【问题描述】:我不想在启动时第一次加载所有数据。它效率不高,所以我可以加载列表中的前 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
tryif(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 行数据。我在互联网上看到了教程,但没有成功。也许在这里,解决了我的问题。
谢谢。
【问题讨论】:
【参考方案1】:您想从 mysql 数据库中获取数据!就像在 facebook 或 instagram 等中完成的一样 这是链接可能对您有帮助:) https://github.com/jparkie/GivesMeHopeAndroidClient/blob/new-v2/AnthologyForGMH_6.apk
【讨论】:
以上是关于Android - 从 mysql 数据库中查看更多旧数据(不是图像),当在列表视图中滚动底部时的主要内容,如果未能解决你的问题,请参考以下文章
Mysql中如何查看Sql语句的执行时间(建议多准备点初始数据效果更佳)
将数据发送到 php 脚本并通过 android 保存在 mysql 表中时,加上符号更改为空格