I need to load load text in list but first it should load an empty list in that time it should display a progress bar. I fetch all the contents from the json feeder. But am stuck with how to load the empty list when the process is in backgroud.
/**
* This class loads MainScreen in ListView
*/
package com.smartmedia.salonaudi;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.smartmedia.salonaudi.map.GoogleMapActivityClass;
/**
*
*
*
*/
public class SalonAudiActivity extends ListActivity {
Button map;
Button left;
Button about;
final Handler uiThreadCallback = new Handler();
Runnable runInUIThread;
// WebView about;
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> mylistCopy = mylist;
ArrayList<String> latitude = new ArrayList<String>();
ArrayList<String> longitude = new ArrayList<String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.listplaceholder);
//processDialog = ProgressDialog.show(this, "", getString(R.string.loading), true);
// getDetails();
// Map Button Click
map = (Button) findViewById(R.id.seeonmap);
map.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SalonAudiActivity.this,
GoogleMapActivityClass.class);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
startActivity(intent);
}
});
// Live Button Click
about = (Button) findViewById(R.id.about);
about.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://google.com"));
startActivity(intent);
}
});
JSONObject json = JSONfunctions
.getJSONfromURL("http://audi.smart-media.no/api/getEvents.php");
try {
JSONObject es = json.getJSONObject("events");
JSONArray event = es.getJSONArray("event");
JSONObject[] events = new JSONObject[es.length()];
for (int i = 0; i < event.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = event.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("Time", "" + e.getString("startTime").split(" ")[0]);
map.put("startTime",
""
+ e.getString("startTime").split(" ")[1]
.substring(0, 5)
+ " @ Olympic Stadium");
map.put("title", "" + e.getString("title"));
System.out.println("title" + e.getString("title"));
map.put("ingress", "" + e.getString("ingress"));
map.put("description",
""
开发者_运维百科 + e.getString("description").replaceAll(
"\\<[^>]*>", ""));
map.put("image", "" + e.getString("image"));
map.put("latitude", "" + e.getString("latitude"));
map.put("longitude", "" + e.getString("longitude"));
System.out.println("latitude" + e.getString("latitude"));
mylist.add(map);
}
}
catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.main,
new String[] { "startTime", "title", "description" },
new int[] { R.id.time, R.id.title, R.id.description });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent = new Intent();
intent.setClassName(SalonAudiActivity.this,
"com.smartmedia.salonaudi.DetailView");
HashMap<String, String> map = mylistCopy.get(arg2);
intent.putExtra("title", map.get("title"));
intent.putExtra("image", map.get("image"));
intent.putExtra("startTime", map.get("startTime"));
intent.putExtra("ingress", map.get("ingress"));
intent.putExtra("description", map.get("description"));
startActivity(intent);
}
});
for (int i = 0; i < mylist.size(); i++) {
HashMap<String, String> e = mylist.get(0);
latitude.add(e.get("latitude"));
longitude.add(e.get("longitude"));
}
}
public boolean isConnected() {
Context context = getApplicationContext();
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
}
You have to use AsyncTask for displaying dialog box while data is loading in the list.
Definitely this will solve your problem
There is code ... This will help you to do Your work...
In doInBackground method load all ur json data into arrays or ArrayList and set into the adpater
In PostExecute method , set the adapter to your list..
private class LoadingData extends AsyncTask<String, Void, String> {
private ProgressDialog Dialog = new ProgressDialog(getParent());
@Override
protected String doInBackground(String... params) {
// perform long running operation operation
//Here you have to do your network operations..
return null;
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
try
{
if(Dialog.isShowing())
{
Dialog.dismiss();
}
// do your Display and data setting operation here
}
catch(Exception e)
{
}
//Here depending upon your validation, display appropriate message.
}
@Override
protected void onPreExecute() {
Dialog.setMessage("Loading Data.....");
Dialog.show();
// Things to be done before execution of long running operation. For example showing ProgessDialog
}
@Override
protected void onProgressUpdate(Void... values) {
// Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
}
}
精彩评论