Android-在listview中向下滚动时,从mysql数据库查看更多旧数据(而不是图像)
我不想在第一次启动时加载所有数据。它效率不高,所以我可以加载列表中的前10或15项。此外,当我滚动ListView的行时,它们也会被加载。我该怎么做? 这是我的代码:Android-在listview中向下滚动时,从mysql数据库查看更多旧数据(而不是图像),android,mysql,onscroll,onscrolllistener,Android,Mysql,Onscroll,Onscrolllistener,我不想在第一次启动时加载所有数据。它效率不高,所以我可以加载列表中的前10或15项。此外,当我滚动ListView的行时,它们也会被加载。我该怎么做? 这是我的代码: public class BerandaFragment extends Fragment implements OnRefreshListener{ SwipeRefreshLayout swipeContainer; String myJSON; private static final String TAG_RESUL
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);
}
公共类BerandaFragment扩展了RefreshListener上的片段实现{
SwipeFreshLayout swipeContainer;
字符串myJSON;
私有静态最终字符串标记_RESULTS=“result”;
私有静态最终字符串ID=“eventID”;
私有静态最终字符串JUDUL=“JUDUL”;
私有静态最终字符串TANGGAL=“TANGGAL”;
专用静态最终字符串JAM=“JAM”;
私有静态最终字符串LOKASI=“LOKASI”;
私有静态最终字符串KETERANGAN=“KETERANGAN”;
JSONArray-peoples=null;
ArrayList个人主义者;
列表视图列表;
公共静态BerandaFragment newInstance(){
BerandaFragment=新的BerandaFragment();
返回片段;
}
公共贝兰达碎片(){
}
@凌驾
创建视图上的公共视图(更平坦的充气机,
@可为空的视图组容器,@Nullable Bundle savedInstanceState){
//TODO自动生成的方法存根
视图根视图=充气机。充气(R.layout.fragment_beranda,容器,假);
swipeContainer=(SwipeRefreshLayout)rootView.findviewbyd(R.id.swipeContainer);
//设置触发新数据加载的刷新侦听器
swipeContainer.setOnRefreshListener(此);
//配置刷新颜色
swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_绿色_灯,
android.R.color.holo_橙色_灯,
android.R.color.holo_红灯);
list=(ListView)rootView.findViewById(R.id.ListView);
personList=新的ArrayList();
getData();
返回rootView;
}
@凌驾
公共事务主任(活动){
//TODO自动生成的方法存根
超级转速计(活动);
((主活动)活动)。附加的第(1)节;
}
受保护的无效显示列表(){
试一试{
JSONObject jsonObj=新的JSONObject(myJSON);
peoples=jsonObj.getJSONArray(标记结果);
对于(int i=0;i您希望从mysql数据库中获取数据!!就像在facebook或instagram中一样
以下是链接,可能对您有所帮助:)
你想从mysql数据库中获取数据!!就像在facebook或instagram中一样
以下是链接,可能对您有所帮助:)