I am using achartEngine to draw linechart in my android application. I am setting data in the code. Now I need to get data from JSON and display it in my graphics, but I don't know how to connect my JSON with achartengine and display it into linechart.
This is my wrong source code :
parsing json
public class ErizaChartEngine extends Activity {
List NabList = new ArrayList();
boolean statuskoneksi= true;
private ProgressDialog Dialog;
protected Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new LineChartAsyncTask().execute();
setContentView(R.layout.layoutchart);
}
public class LineChartAsyncTask extends AsyncTask <String, String, String>{
#Override
protected void onPreExecute (){
super.onPreExecute();
Dialog=new ProgressDialog(ErizaChartEngine.this);
Dialog.setMessage("Mohon Tunggu Sebentar...");
Dialog.setIndeterminate(false);
Dialog.setCancelable(true);
Dialog.show();
}
#Override
protected String doInBackground(String... params) {
String url= "http://www.ab.com/NabChart.htm?id=03&nilai=10";
try {
JSONParser jp= new JSONParser();
JSONArray ja= jp.takeJson(url);
for (int i=0; i<ja.length();i++){
JSONObject jo = ja.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
if (jo.has("lnu_nilai"))
map.put("lnu_nilai", jo.get("lnu_nilai").toString());
if (jo.has("tanggal"))
map.put("tanggal", jo.get("tanggal").toString());
NabList.add(map);
System.out.println("json parser done");
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String file_url) {
Dialog.dismiss();
}
This is my class for drawing graphics :
public void drawNABContentSimpleChart
(
String strtanggal,
String strNilaiNABHMin0)
{
XYSeries nabseries = new XYSeries("nab");
for (int i=0;i<NabList.size();i++){
nabseries.add(strNilaiNABHMin0[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(nabseries);
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.WHITE);
incomeRenderer.setPointStyle(PointStyle.CIRCLE);
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setDisplayChartValues(true);
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("NAB 1 year");
multiRenderer.setXTitle("Year 2012");
multiRenderer.setYTitle("Nilai");
multiRenderer.setZoomButtonsVisible(true);
for(int i=0;i<NabList.size();i++){
multiRenderer.addXTextLabel(strtanggal[i]);
}
multiRenderer.addSeriesRenderer(incomeRenderer);
// multiRenderer.addSeriesRenderer(expenseRenderer);
// Creating an intent to plot line chart using dataset and multipleRenderer
Intent intent = ChartFactory.getLineChartIntent(getBaseContext(), dataset, multiRenderer);
// Start Activity
startActivity(intent);
}
I really need help to solve my problem, because my code still wrong and I don't know how to fix it. is there anyone can help me to solve this?
Just initialize the chart stuff (dataset, renderer) and start parsing your json. Add every data item you read from the file to your series in the dataset. Once you are done, display the chart.
Related
i want to display a percentage data from database mysql into text view in android studio. my rest API use php language. but the code to display the data is still wrong. what i should do?
code in java
public void getPersentase(){
class GetPersentase extends AsyncTask<Void,Void,String>{
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
showPersentase(s);
}
#Override
protected String doInBackground(Void... voids) {
HashMap<String,String> params = new HashMap<>();
RequestHandler requestHandler = new RequestHandler();
return requestHandler.sendPostRequest(konfigurasi.URL_PERSEN_SANGAT_PUAS_PERDATA,params);
}
}
new GetPersentase().execute();
}
public void showPersentase(String json){
try {
JSONObject jsonObject = new JSONObject(json);
JSONArray result = jsonObject.getJSONArray(konfigurasi.TAG_JSON_ARRAY);
JSONObject c = result.getJSONObject(0);
sangatpuasperdata.setText(c.getString(konfigurasi.URL_PERSEN_SANGAT_PUAS_PERDATA));
} catch (JSONException e) {
e.printStackTrace();
}
}
Hey guys I know this is a common problem, Im using a AsyncTaskActivity to have a JSON parser parse some json behind the scences of a app and pump a listview with an arrayadapter. This is my code code in my mainActivity.java. My screen flickers whenever I get to the belwo activity.
setContentView(R.layout.activity_see_schedule_activity);
this.setTitle("Activity Schedule");
//Log.e("ID see schedule#",getIntent().getExtras().getString("id#"));
RequestQueue queue = Volley.newRequestQueue(see_schedule_activity.this);
StringRequest request = new StringRequest(Request.Method.POST, httpUpdateRoutines, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(see_schedule_activity.this, "" + response, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(see_schedule_activity.this, "Data Not Fetched " + error, Toast.LENGTH_SHORT).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
//Log.e("ID see schedule#",getIntent().getExtras().getString("id#"));
map.put("residentid", getIntent().getExtras().getString("id#"));
return map;
}
};
queue.add(request);
new AsyncTaskParseJson(this).execute();
ArrayList<String> schedulelist = getIntent().getExtras().getStringArrayList("tasks_filled");
ArrayAdapter adapter = new ArrayAdapter<String>(this,
R.layout.activity_listview, schedulelist);
ListView listView = (ListView) findViewById(R.id.schedulelist);
listView.setAdapter(adapter);
My AsyncTaskparseJson.java
public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
private Context c;
String schedule;
String activityid;
String routine;
String[] tasks;
ArrayList<String> schedulelist = new ArrayList<String>();
final String TAG = "AsyncTaskParseJson.java";
// set your json string url here
String yourJsonStringUrl = "http://www.wirelesshealth.wayne.edu/nhcheckup/getData(justin).php";
// contacts JSONArray
JSONArray dataJsonArr = null;
public AsyncTaskParseJson(Context applicationContext) {
this.c=applicationContext;
}
#Override
protected void onPreExecute() {}
#Override
protected String doInBackground(String... arg0) {
try {
HashMap<Integer, String> hm = new HashMap<Integer, String>();
-----------------------some hash -----------------
// instantiate our json parser
JsonParser jParser = new JsonParser();
// get json string from url
JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
// get the array of users
dataJsonArr = json.getJSONArray("schedule");
// loop through all users
for (int i = 0; i < dataJsonArr.length(); i++) {
Log.e("doInBackground","YOU GOT HERE");
JSONObject c = dataJsonArr.getJSONObject(i);
// Storing each json item in variable
activityid = c.getString("activityid");
-------------------------------Pasres object c --------------
}
Intent intent = new Intent(c,see_schedule_activity.class);
-- passes back arraylist -------------- intent.putStringArrayListExtra("tasks_filled",schedulelist);
this.c.startActivity(intent);
((Activity)c).finish();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String strFromDoInBg) {
// Intent intent = new Intent(see_schedule_activity.this,see_schedule_activity.class);
// intent.putExtra("tasks_filled",tasks);
// this.c.startActivity(intent);
// ((Activity)c).finish();
}
}
I did a bit of troubleshotting and found out the error has to do with the placement of the intent that's passed a array list. But here's the problem, if I put it in the doinBackground() the screen flashes rapidly (which is prolly becuase it keeeps calling the (setContentView(R.layout.activity_see_schedule_activity);) but if I keep it in onPostExcute nothing happens (hte arraylist isnt pumping the listview). So im a bit stumped, any help would be appreciated thanks!
As I said in my comment, you enter a recursive call between the activity and the AsyncTask as you start the same activity again with an Intent. Instead, as you already pass the activity instance to the AsyncTask, simply create an update method in the activity and use that from the AsyncTask:
// field in see_schedule_activity
private ArrayAdapter<String> adapter;
// which you'll initialize in onCreate()
// with an empty list, as you don't yet have the data
...
adapter = new ArrayAdapter<String>(this,
R.layout.activity_listview, new ArrayList<>());
ListView listView = (ListView) findViewById(R.id.schedulelist);
listView.setAdapter(adapter);
... rest of onCreate()
public void update(List<String> results) {
adapter.clear();
adapter.addAll(results);
}
Then implement correctly the AsyncTask:
public class AsyncTaskParseJson extends AsyncTask<String, Void, List<String>> {
private see_schedule_activity activity;
//... the rest
public AsyncTaskParseJson(see_schedule_activity activity) {
this.activity = activity;
}
#Override
protected List<String> doInBackground(String... arg0) {
// create scheduleList
return scheduleList;
}
#Override
protected void onPostExecute(List<String> results) {
activity.update(results);
}
//...
This is not a good implementation of the AsyncTask but should get you going.
im new programmer in android, i write program to get image url and show the image in list view, my program work correct and can show me the images but when i want to show image i should use static url, but i want to use static link to get all image url with json and show it in list view.
i have json class to get image url but i dont know how to use it in
private String imageUrls[] to get image dynamic url from my static link.
***string url is static link in mainactivity to get image url with json.
my mainactivity class:
public class MainActivity extends Activity {
private final String url="http://192.168.1.4:81/upload/images.php";
ListView list;
LazyAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
adapter = new LazyAdapter(this, imageUrls);
list.setAdapter(adapter);
}
#Override
public void onDestroy() {
list.setAdapter(null);
super.onDestroy();
}
private String imageUrls[] = {
"http://www.technotalkative.com/wp-content/uploads/2012/09/tt_listview1-171x300.png",
"http://www.technotalkative.com/wp-content/uploads/2012/11/f-DayDream-Example-Landscape.png",
};
}
and json class:
class get url extends AsyncTask<String,Void,String>{
#Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(MainActivity.this);
pd.setMessage("login");
pd.show();
}
#Override
protected String doInBackground(String... params) {
List<NameValuePair> parms=new ArrayList<>();
JSONObject json=jParser.makeHTTPRequest(url,"GET");
try {
int t=json.getInt("t");
if(t==1){
s=json.getJSONArray("travel");
for(int i=0;i<s.length();i++){
String url_image=c.getString("url_image");
HashMap<String,String>map=new HashMap<String,String>();
map.put("url_image",url_image);
P.add(map);
}
}else {
Toast.makeText(MainActivity.this,"No Data Found",Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
It is much more convenient to use Gson library gor parsing json response to java objects instead of extracting fields manually. It is also a good practice ( more convenient) to use Retrofit library instead of asynctasks - see https://stackoverflow.com/a/38002999/6175778 post
im writing an app which needs to get some json from my DB, i receive the data, but now im trying to also view a icon beside the information which is shown in a listview.
The line which is making trouble is:
mChart.setTag(URL);
new DownloadImagesTask.execute(mChart); <------
MainActivity:
public class MainActivity extends Activity {
ListView list;
TextView icon;
TextView name;
TextView developer;
TextView size;
Button Btngetdata;
ArrayList<HashMap<String, String>> mList = new ArrayList<HashMap<String, String>>();
private static String url = "http://appwhittle.com/getdata.php";
private static final String TAG_ITEM = "app_item";
private static final String TAG_ICON = "app_icon";
private static final String TAG_NAME = "app_name";
private static final String TAG_DEVELOPER = "app_developer";
private static final String TAG_SIZE = "app_size";
JSONArray mJsonArray = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList = new ArrayList<HashMap<String, String>>();
new JSONParse().execute();
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
icon = (TextView)findViewById(R.id.icon);
name = (TextView)findViewById(R.id.name);
size = (TextView)findViewById(R.id.size);
developer = (TextView)findViewById(R.id.developer);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
#Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
mJsonArray = json.getJSONArray(TAG_ITEM);
for(int i = 0; i < mJsonArray.length(); i++){
JSONObject c = mJsonArray.getJSONObject(i);
String name = c.getString(TAG_NAME);
String size = c.getString(TAG_SIZE);
String developer = c.getString(TAG_DEVELOPER);
String icon = c.getString(TAG_ICON);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ICON, icon);
map.put(TAG_NAME, name);
map.put(TAG_DEVELOPER, "Developer: " + developer);
map.put(TAG_SIZE, size + " MB");
mList.add(map);
list=(ListView)findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, mList,
R.layout.list_v,
new String[] {TAG_NAME, TAG_DEVELOPER, TAG_SIZE }, new int[] {
R.id.name, R.id.developer, R.id.size});
ImageView mChart = (ImageView) findViewById(R.id.icon);
String URL = "http://www...anything ...";
mChart.setTag(URL);
new DownloadImagesTask.execute(mChart);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at "+ mList.get(+position).get(TAG_NAME), Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
DownloadImagesTask:
public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {
ImageView imageView = null;
#Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
#Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
return null;
}
}
Any help is much appreciated! Thanks in advance!
Replace
new DownloadImagesTask.execute(mChart);
with
new DownloadImagesTask().execute(mChart);
Try this. It will work.
You need to change
new DownloadImagesTask.execute(mChart);
to
new DownloadImagesTask().execute(mChart);.
Check after changing, hopefully it works fine. Happy coding :)
Change:
new DownloadImagesTask.execute(mChart);
to:
new DownloadImagesTask(mChart).execute();
You have not initialized the object of your DownloadImagesTask by calling constructor.
new DownloadImagesTask().execute(mChart); use like this. Call default constructor by putting ().
//Suppose you have a class "DataFetcher" which extends "AsyncTask<Void,Void,Void>{}"
//to execute this class write the code as follows
new DataFetcher().execute(); //done
So, I want to display a spinning loading indicator while my ListView is being populated. I successfully have implemented the progress bar, BUT for some reason it disappears BEFORE all of the listings are displayed. What I want is the progressbar to be present during the TOTAL load time of the listings. Basically, what it seems like, each listing is being displayed one at a time, not all at once when they are all loaded.
What I'm doing is
1. Creating a new custom adapter class
2. Populating the ListView in an AsyncTask using this adapter class
3. Setting the ListView to this adapter
This works properly, the progress bar just disappears before all of the listings are displayed. Does anyone have any ideas?
Activity class:
public class MainActivity extends ActionBarActivity {
ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;
// public static Bitmap bitmap;
Button refresh;
ProgressBar progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress=(ProgressBar)findViewById(R.id.progressbar_loading);
// Construct the data source
arrayOfLocations = new ArrayList<Location>();
// Create the adapter to convert the array to views
adapter = new LocationAdapter(this, arrayOfLocations);
FillLocations myFill = new FillLocations();
myFill.execute();
refresh = (Button) findViewById(R.id.refresh);
refresh.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
startActivity(getIntent());
}
});
}
private class FillLocations extends AsyncTask<Integer, Void, String> {
String msg = "Done";
protected void onPreExecute() {
progress.setVisibility(View.VISIBLE);
}
// Decode image in background.
#Override
protected String doInBackground(Integer... params) {
String result = "";
InputStream isr = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://afs.spotcontent.com/"); // YOUR
// PHP
// SCRIPT
// ADDRESS
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
// resultView.setText("connected");
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(isr, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
// parse json data
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
final JSONObject json = jArray.getJSONObject(i);
try {
BitmapWorkerTask myTask = new BitmapWorkerTask(
json.getInt("ID"), json);
myTask.execute();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data " + e.toString());
}
return msg;
}
protected void onPostExecute(String msg) {
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.listView1);
// View header = (View) getLayoutInflater().inflate(
// R.layout.listview_header, null);
// listView.addHeaderView(header);
listView.setAdapter(adapter);
progress.setVisibility(View.GONE);
}
}
}
Adapter class:
public class LocationAdapter extends ArrayAdapter<Location> {
public LocationAdapter(Context context, ArrayList<Location> locations) {
super(context, R.layout.item_location, locations);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Location location = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_location, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvDetails = (TextView) convertView.findViewById(R.id.tvDetails);
TextView tvDistance = (TextView) convertView.findViewById(R.id.tvDistance);
TextView tvHours = (TextView) convertView.findViewById(R.id.tvHours);
ImageView ivIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
// Populate the data into the template view using the data object
tvName.setText(location.name);
tvDetails.setText(location.details);
tvDistance.setText(location.distance);
tvHours.setText(location.hours);
ivIcon.setImageBitmap(location.icon);
// Return the completed view to render on screen
return convertView;
}
}
The reason for that behavior is that you are starting multiple threads.
FillLocations preExecute --> SHOW ProgressBar
BitmapWorkerTask_1 --> new thread
BitmapWorkerTask_2 --> new thread
...
BitmapWorkerTask_N --> new thread
FillLocations postExecute --> HIDE ProgressBar
BitmapWorkerTask_K --> continue execution
BitmapWorkerTask_K+1 --> continue execution
etc.
If you want the list to be displayed until it's all loaded, Simply make BitmapWorker's processing synchronous. If you still want to display the list right away but keep the spinner until it's all finished, then keep a counter in your activity and increase it in preexecute and decrease it in postExecute of BitmapWorker via a setter. Once the counter hits 0, remove hide the progressBar.
In activity:
private int asynchCounter = 0;
private void updateCounter(int delta){
asynchCounter+=delta;
if(asynchCounter<=0){
progress.setVisibility(View.GONE);
}else{
progress.setVisibility(View.VISIBLE);
}
}
And instead of BitmapWorkerTask use
class CountedBitmapWorkerTask extends BitmapWorkerTask {
protected void onPreExecute() {
super.onPreExecute();
updateCounter(1);
}
protected void onPostExecute(String msg) {
super.onPostExecute();
updateCounter(-1);
}
}
I had this exact problem, to solve it I had to write AsyncTask complete listener. Which sends a notification to UI thread, that data was loaded and it has to change something, in this case hide the ProgressBar.
This is the basic example of how this should look like. I am not sure this will work for you after you copy it to your project, but complete listener is what you need, so after studying this case you should be able to find a solution.
AsyncTaskCompleteListener.java - listener interface.
public interface AsyncTaskCompleteListener {
public void onTaskComplete();
}
LoadDataTask.java
class LoadDataTask extends AsyncTask<Object, Object, Object> {
/* Your object types according to your task. */
private AsyncTaskCompleteListener callback; // Callback field
public LoadDataTask(AsyncTaskCompleteListener cb){
this.callback = cb;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Object doInBackground(String... urls) {
/* Your task here */
return result;
}
#Override
protected void onPostExecute(Object o) {
callback.onTaskComplete(); // Set the Callback
}
}
MainActivity.java
public class MainActivity implements AsyncTaskCompleteListener{
/* ...Other methods and fields... */
/* onTaskComplete method which fires after your data is loaded. */
#Override
public void onTaskComplete(){
// Hide ProgressBar
}
}
Self Plug: https://github.com/horvste/EasyWebPageDownloadForAndroid
This would separate the threading from the implementation and solve your problem. This is very similar to what Tony suggested except it's already implemented for you.
Github Readme:
Good for connecting to REST API's, HTML parsing, and many other uses. Using this library is meant to be easy:
Create a class which implements OnProgressUpdate
public class SampleClass implements OnProgressUpdate {
#Override
public void onUpdate(Integer percentProgress) {
}
#Override
public void onUpdateFailure() {
}
#Override
public void onSuccess(StringBuilder result) {
}
#Override
public void onFailure() {
}
}
}
Instantiate DownloadWebPage object
DownloadWebPage webPage = new DownloadWebPage(new SampleClass(), myUrl);
Call .downloadHtml() from the DownloadWebPage
webPage.downloadHtml();
Also if the asynchtask is updating properly and the amount of items is to large look here:
listing a listview is taking too much time and memory in android
Another option would be to only list a certain amount of items then have a next page button or gesture to deal with the ListView loading too slow.