まず、最初の 10 個のコンテンツをロードする必要があります。リストビューをスクロールすると、別の 10 個のコンテンツがロードされます。
private String jsonResult;
public static final int DIALOG_DOWNLOAD_JSON_PROGRESS = 0;
private String url = "http://192.1.1.1/xxxx/myphp.php";
String urll = url;
private ListView listView;
private static final String HTTP_REQUEST_FAILED = null;
int[] flags;
String strImageName;
int n = 10000;
String[] mySecondStringArray = new String[n];
String[] strArray;
private ProgressDialog mProgressDialog;
HashMap<String, String> hm;
List<HashMap<String, String>> aList;
boolean flag_loading = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
listView = (ListView) findViewById(R.id.listview);
try {
accessWebService();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Check ur network connection", 10000).show();
}
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
additem();
}
});
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// what is the bottom iten that is visible
// int lastInScreen = firstVisibleItem + visibleItemCount;
if (firstVisibleItem + visibleItemCount == totalItemCount
&& totalItemCount != 0) {
if (flag_loading == false) {
flag_loading = true;
Toast.makeText(getApplicationContext(), "fh", 1000)
.show();
additem();
}
}
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_JSON_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Please wait.....");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
}
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
ListDrwaer();
dismissDialog(DIALOG_DOWNLOAD_JSON_PROGRESS);
}
}
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}
public void ListDrwaer() {
flag_loading = false;
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("products");
aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("name");
hm = new HashMap<String, String>();
hm.put("txt", name);
// hm.put("cur", number);
aList.add(hm);
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
String[] from = { "flag", "txt" };
int[] to = { R.id.flag, R.id.txt };
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.news, from, to);
listView.setAdapter(adapter);
}
public void additem() {
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("tablename");
aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("fieldname");
hm = new HashMap<String, String>();
hm.put("txt", name);
// hm.put("cur", number);
aList.add(hm);
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
String[] from = { "flag", "txt" };
int[] to = { R.id.flag, R.id.txt };
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
R.layout.news, from, to);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
flag_loading = true;
}
}
私のphpファイル
<?php
$host="localhost"; //replace with database hostname
$username="root"; //replace with database username
$password=""; //replace with database password
$db_name="databasename"; //replace with database name
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "SELECT * FROM tablename LIMIT 10";
$result1 = mysql_query($sql);
$json = array();
if(mysql_num_rows($result1)){
while($row=mysql_fetch_assoc($result1)){
$json['tablename'][]=$row;
}
}
mysql_close($con);
echo json_encode($json);
?>
私はこのコードを使用して多くの検索を行いましたが、解決策が見つかりません。誰かが知っている場合は、できるだけ早く回答を投稿してください。