Retrieve image from drawable and String convert to Int issue - android

i am currently doing a page which retrieve data from php server and now trying to retrieve and image from drawable by using setImageResource but is not working, i dunno what wrong with it and is it possible for me to just save image name in database then retrieve image by using image name?
beside that, i try to do a simple plus minus button for quantity but the apps force stop once i click on the button..
public class FoodDetailActivity extends Activity
{
TextView FoodName;
TextView FoodDesc;
TextView FoodPrice;
ImageView FoodImg;
EditText Number;
Button plus;
Button minus;
Button Addcart;
String fid;
int number;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
private static final String url_food_details = "http://10.0.2.2/android_user/FoodDetail.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_FOOD = "food";
private static final String TAG_FID = "fid";
private static final String TAG_FOODNAME = "food_name";
private static final String TAG_FOODPRICE = "food_price";
private static final String TAG_FOODDESCRIPTION = "food_description";
private static final String TAG_FOODURL = "food_url";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_detail);
// button
plus = (Button)findViewById(R.id.btn_plus);
plus.setOnClickListener(increase);
minus = (Button)findViewById(R.id.btn_minus);
minus.setOnClickListener(decrease);
Addcart = (Button)findViewById(R.id.btn_submit);
Number = (EditText)findViewById(R.id.text_number);
// getting food details from intent
Intent i = getIntent();
// getting food id (fid) from intent
fid = i.getStringExtra(TAG_FID);
// Getting complete product details in background thread
new GetFoodDetails().execute();
}
// Increase number of quantity
private OnClickListener increase = new OnClickListener()
{
public void onClick(View v)
{
String quantity = Number.getText().toString().trim();
number = Integer.parseInt(quantity);
if(number > 0 && number < 99)
{
number = number + 1;
Number.setText(Integer.toString(number));
}
else if(number == 99)
{
number = 1;
Number.setText(Integer.toString(number));
}
}
};
// Decrease number of quantity
private OnClickListener decrease = new OnClickListener()
{
public void onClick(View v)
{
String quantity = Number.getText().toString();
number = Integer.valueOf(quantity);
if(number > 1 && number <= 99)
{
number = number - 1;
Number.setText(Integer.toString(number));
}
else if(number == 1)
{
number = 99;
Number.setText(Integer.toString(number));
}
}
};
class GetFoodDetails extends AsyncTask<String, String, String>
{
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(FoodDetailActivity.this);
pDialog.setMessage("Loading food details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params)
{
// updating UI from Background Thread
runOnUiThread(new Runnable()
{
public void run()
{
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("fid", fid));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = JSONParser.makeHttpRequest(url_food_details, "GET", params);
// check your log for json response
Log.d("Single Food Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1)
{
// successfully received food details
JSONArray foodObj = json.getJSONArray(TAG_FOOD); // JSON Array
// get first product object from JSON Array
JSONObject food = foodObj.getJSONObject(0);
// Loader image - will be shown before loading image
int loader = R.drawable.loader;
String image_url = food.getString(TAG_FOODURL);
// product with this fid found
// Edit Text
FoodName = (TextView)findViewById(R.id.food_name);
FoodPrice = (TextView)findViewById(R.id.food_price);
FoodDesc = (TextView)findViewById(R.id.food_desc);
FoodImg = (ImageView)findViewById(R.id.img_food);
// display product data in EditText
FoodName.setText(food.getString(TAG_FOODNAME));
FoodPrice.setText("RM" + food.getString(TAG_FOODPRICE));
FoodDesc.setText(food.getString(TAG_FOODDESCRIPTION));
// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
// display image
imgLoader.DisplayImage(image_url, loader, FoodImg);
}
else
{
// no food detail found
// Launch error message
AlertDialog.Builder ad = new AlertDialog.Builder(FoodDetailActivity.this);
ad.setTitle("Error");
ad.setMessage("Food Detail is empty!");
ad.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
});
ad.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
protected void onPostExecute(String file_url)
{
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
}
The problem had been solve, i using the ImageLoader retrieve image at server side with url store in database.

In here:
Drawable d = getResources().getDrawable(R.drawable.mcchicken); //<<<<
you are trying to use context of Activity before onCreate call. move Drawable d initialization inside onCreate method of Activity after setContentView as:
Drawable d; //<<< declare d here
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_detail);
d = getResources().getDrawable(R.drawable.mcchicken); //<< initialize d here
....
}
Edit : : inside doInBackground method you are trying to access UI element. instead of updating UI from doInBackground using runOnUiThread. you will need to move all UI related code in onPostExecute which call on Ui thread after doInBackground execution complete.

In your code is much messy material. So nicely from the beginning. First is only question.
Why you are putting inside doInBackground() method runOnUiThread()? If you want to update your UI with some information from task running in background, for this you have onProgressUpdate() or onPostExecute() method which are synchronized with UI Thread and allow its updates. doInBackground() method is directly designated for background processing and you shouldn't broke it.
Then this line:
if (food.getString(TAG_FOODNAME) == "McChicken")
will always return false because you are comparing references and not values. Always you are comparing strings, you have to use equals() method that makes a trick.
And last thing is this:
Drawable d = getResources().getDrawable(R.drawable.mcchicken);
You can't call that before setContentView() is called. Reason is that main purpose of setContentView() is that it creates all instances of UI elements and resources and if you something that requires resources call before this method, always you will get NPE

Related

json search bar in android appthat searches a json file from an api server

i want to have a search bar that searches a number that has been typed in (for example: 115048) and put that in a listview. the json file looks like this http://api.ccapp.it/v1/student/115048/schedule/11
hope someone can help me, the code that i use right now to search a link is like this but it doesnt have a search bar:
public class RoosterviewMd extends ListActivity {
Button mButton;
EditText mEdit;
private ProgressDialog pDialog;
// URL to get contacts JSON
//private static String id = null;
//private static String url = "http://api.ccapp.it/v1/student/" + id + "/schedule/11";
private static String url = "http://api.ccapp.it/v1/student/115048/schedule/12";
// JSON Node names
private static final String TAG_LESSON = "class";
private static final String TAG_ROOM = "room";
private static final String TAG_TEACHER = "teacher";
// contacts JSONArray
JSONArray contacts = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.roosterviewmd);
//Number input
final EditText input = (EditText) findViewById(R.id.editText2);
//buttons for all the days
Button btn2 = (Button) findViewById(R.id.button29);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Toast.makeText(getBaseContext(), "Je ziet je rooster voor maandag al" , Toast.LENGTH_SHORT ).show();
}
});
Button btnOne = (Button)findViewById(R.id.button30);
btnOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), RoosterviewDi.class);
startActivity(intent);
}
});
Button btnTwo = (Button)findViewById(R.id.button31);
btnTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), RoosterviewWo.class);
startActivity(intent);
}
});
Button btnThree = (Button)findViewById(R.id.button32);
btnThree.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), RoosterviewDo.class);
startActivity(intent);
}
});
Button btnFour = (Button)findViewById(R.id.button33);
btnFour.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), RoosterviewVr.class);
startActivity(intent);
}
});
//Buttons end here
contactList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String lesson = ((TextView) view.findViewById(R.id.lesson))
.getText().toString();
String teacher = ((TextView) view.findViewById(R.id.teacher))
.getText().toString();
String room = ((TextView) view.findViewById(R.id.room))
.getText().toString();
}
});
// Calling async task to get json
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(RoosterviewMd.this);
pDialog.setMessage("Give me a second please");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray arr1 = jsonObj.getJSONArray("lessons");
JSONArray arr2 = arr1.getJSONArray(0); //Dag
for (int b = 0; b < arr2.length(); b++) {
JSONObject c = arr2.getJSONObject(b);
String lesson = c.getString(TAG_LESSON);
String teacher = c.getString(TAG_TEACHER);
String room = c.getString(TAG_ROOM);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_LESSON, lesson);
contact.put(TAG_TEACHER, teacher);
contact.put(TAG_ROOM, room);
// adding contact to contact list
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("CCApp", "Couldn't get any data from the url");
Toast.makeText(getBaseContext(),"We are aware of this error and are working on it, in the mean time eat a cookie", Toast.LENGTH_LONG).show();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(RoosterviewMd.this, contactList,
R.layout.list_item, new String[] {TAG_LESSON, TAG_TEACHER,
TAG_ROOM }, new int[] { R.id.lesson,
R.id.teacher, R.id.room });
setListAdapter(adapter);
}
}
}
i hope someone can help me with this
Check out this answer: Get text from web page to string
Basically, you can simply get the text from the page and pass it into a string, and search the string application side for the contents of your edit text.
If you're looking for more functionality with the data from the web site, I would pull the Json into an array of Jsonobjects using something like Gson. You'd then be able to use the data from the web page in a bit more of a structured manner.
Edit: Now to actually answer your question.
You can include an edit text and button in your xml in order to search using a basic search bar kinda thing.
To set a listener on the button, you would do something like:
findViewById(R.id.button).setOnClickListener(new OnClickListener(){
#Override
protected void onClick(View v){
//Here, we can control what the response to the button press is, and grab the text in the edit text field.
String editTextString = findViewById(R.id.edittext).getEditableText().toString();
//Now we have a string used to parse the json or whatever else you need to do.
//May want to add a case here if editTextString is null to prevent runtime errors.
}
}
(Forgive me if there's any minor syntatic errors, just wrote that up quick here in the browser, no API to check on it. :))

Recreate same Listview on item click

I have a Listview and is working well. I'm getting JSON data from remote server and using SimpleAdapter. Basically I get song list from the server. But now, I want to let user select category first. After selecting any category I want to change the URL depending on the selected category, and then populate the listview again. Like, I'm calling getlist.php to get categories. Now if user selects a category named POP, want to call getlist.php?cat=pop to get all pop songs and re-populate the listview where user will see a list of pop songs.
private static String url_json = "http://10.0.2.2/aaa/getlist.php"; //this gives only the categories
private static String url_json = "http://10.0.2.2/aaa/getlist.php?cat=pop"; //this gives all songs those are under category pop
I don't think code is necessary here, if you still need please tell me, I'll update with code given.
Till now I used the following code in onItemClick but not working:
categorySelected = true;
url_json += "?c=Bangla";
new LoadAllProducts().execute();
lv.invalidateViews(); //final ListView lv = getListView();
So, let me summerise the full thing. On category item click, I want to change the URL I'm getting data from, and refresh the Listview with new data. Thanks in advance.
Code: Please have a look at my code and suggest any change.
public class AllRBT extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
//private static String url_all_products = "http://aloashbei.com.bd/vasonapps/getList.php";
private static String url_all_products = "http://10.0.2.2/aaa/getlist.php";
private static Boolean categorySelected = false;
private static String confTitle = "Confirmation needed !";
private static String confBody = "We want to send message from next time you select any ring back tone. This may cost 15 taka by your network operator.";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "rbts";
private static final String TAG_PID = "code";
private static final String TAG_NAME = "name";
private static final String TAG_ARTIST = "artist";
private String mobileNumber = "";
// products JSONArray
JSONArray products = null;
private EditText inputSearch;
SimpleAdapter adapter;
//ListAdapter adapter;
///////////////////////////////////////////////////////////////////////////////////////////////////
private void getMobileNumber(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(confTitle);
alert.setMessage(confBody);//Are you sure want to buy this ring back tones?
// Set an EditText view to get user input
//final EditText input = new EditText(this);
//alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//String m = input.getText().toString();
// Do something with value!
mobileNumber = "017";
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
mobileNumber = "";
}
});
alert.show();
}
public String[] generateMessage(String number, int code){
String opCode = number.substring(0, 3);
String messageBody = "", destination = "";
String[] returnValue;
if(opCode.equals("015")){
messageBody = "TT "+code;
destination = "5000";
}else if(opCode.equals("017")){
messageBody = "WT "+code;
destination = "4000";
}else if(opCode.equals("019")){
messageBody = ""+code;
destination = "2222";
}else if(opCode.equals("016")){
messageBody = "CT "+code;
destination = "3123";
}else if(opCode.equals("018")){
messageBody = "GET "+code;
destination = "8466";
}else if(opCode.equals("011")){
messageBody = "Get"+code;
destination = "9999";
}else{
messageBody = "Invalid number";
}
return new String[] {messageBody, destination};
}
private void sendMessage(String dest, String body, String popupText){
if(popupText != "")
Toast.makeText(getApplicationContext(), popupText, Toast.LENGTH_LONG).show();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(dest, null, body, null, null);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_rbt);
//setContentView(R.layout.activity_all_rbt);
//filterText = (EditText) findViewById(R.id.search_box);
//filterText.addTextChangedListener(filterTextWatcher);
//setListAdapter(new ArrayAdapter<String>(this,
//android.R.layout.list_content,
//getStringArrayList());
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
//filter listView
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3){
// When user changed the Text
AllRBT.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
final ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
//lv.invalidateViews();
//if(false){
categorySelected = true;
url_all_products += "?c=Bangla";
new LoadAllProducts().execute();
lv.invalidateViews();
//}
//Context context = getApplicationContext();
String[] values;
// getting values from selected ListItem
String pid = ((TextView) view.findViewById(R.id.pid)).getText().toString();
if(mobileNumber == ""){
getMobileNumber();
return;
}
values = generateMessage(mobileNumber, Integer.parseInt(pid));
String popup = "Sending message '"+values[0]+"' to "+values[1];
sendMessage(values[1], values[0], popup);
//Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
//toast.show();
}
});
}
// Response from Edit Product Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
#Override
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(AllRBT.this);
pDialog.setMessage("Loading ring back tones. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
//Toast toast = Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG);
//toast.show();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
//Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = 1;//json.getInt(TAG_SUCCESS);
if (success == 1){
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
String artist = c.getString(TAG_ARTIST);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, id);
map.put(TAG_NAME, name);
map.put(TAG_ARTIST, artist);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
//Intent i = new Intent(getApplicationContext(),
// NewProductActivity.class);
// Closing all previous activities
//i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
AllRBT.this.adapter = new SimpleAdapter(
AllRBT.this, productsList,
R.layout.list_item, new String[] { TAG_PID, TAG_NAME, TAG_ARTIST}, new int[] { R.id.pid, R.id.name, R.id.artist });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
Judging from the (very minimal) few lines of code you've provided you are loading stuff into your List that is backing your ListView from an AsyncTask (LoadAllProducts?). If that is the case, be sure to update your ListView's data in the onPostExecute() method you should override, and call something like notifyDataSetChanged() when you finished updating.
For more info on how to use AsyncTasks, check the great number of answers on this topic on SO. For instance, I put an answer with some info on AsyncTasks here: progress dialog is not displaying in async task when asynctask is called from separate class
Update after code was added:
OK, I never used a ListActivity before, but after reading some documentation I think the problem is that calling setListAdapter() a second time will not refresh the ListView (as was mentioned here). Instead of creating a new SimpleAdapter every time I think you should update your productList (clear it, add to it, whatever you want) and then call AllRBT.this.adapter.notifyDataSetChanged(). This should trigger the ListView to re-fetch the data from your adapter, which by now contains your new data.
Also some other remarks that will make your code cleaner:
you need not call runOnUiThread() from onPostExecute(), since onPostExecute() is guaranteed to run on the main thread already (as per AsyncTask contract).
I think you don't need to add an OnItemClickListener by yourself. It seems that a ListActivity already does that for you and you can instead simply override its onListItemClick() method.

Managing the background thread within Android application

I currently have this class below which parses json urls and loads images and texts into a listview with the help of the Lazy Adapter Class and background thread.
Each list item consists of an image view and 2 text views.
I want to create pop up boxes (alert dialog) for each of the generated list items. The alert dialog will have options which will call other applications.
My question :
Would it be wise to code this alert dialog functionality in this class? I'm worried that there is a lot of stuff currently being done in the background and it might affect the app's functionality.
If not could anyone suggest another way to do it. thanks.
Json Activity Class :
public class JsonActivity extends SherlockActivity{
private ProgressDialog progressDialog;
// JSON Node names
static final String TAG_NAME = "name";
static final String TAG_IMAGEURL = "imageurl";
ListView list;
LazyAdapter adapter;
String chartUrl;
String[] urlNames = new String[] {
"urls..."
};
// chartItemList is the array list that holds the chart items
ArrayList<HashMap<String, String>> chartItemList = new ArrayList<HashMap<String,
String>>();
//Holds imageurls
ArrayList<String> imageurls = new ArrayList<String>();
JsonParser Parser = new JsonParser();
// JSONArray
JSONArray chartItems = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chart);
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data from the bundle
int chartIndex = bundle.getInt("chartIndex");
String chartUrl = urlNames[chartIndex];
setTitle(bundle.getString("chartname"));
//url from where the JSON has to be retrieved
String url = chartUrl;
//Check if the user has a connection
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (!info.isConnected()) {
Toast.makeText(this, "Please check your connection and try again.",
Toast.LENGTH_SHORT).show();
}
//if positive, fetch the articles in background
else new getChartItems().execute(chartUrl);
}
//else show toast
else {
Toast.makeText(this, "Please check your connection and try again.",
Toast.LENGTH_SHORT).show();
}
}
class getChartItems extends AsyncTask<String, String, String> {
// Shows a progress dialog while setting up the background task
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(JsonActivity.this);
progressDialog.setMessage("Loading chart...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
//Gets the json data for chart items data and presents it in a list view
#Override
protected String doInBackground(String... args) {
String json = Parser.getJSONFromUrl(args[0]);
String imageurl;
String rank;
String name;
String url;
try{
chartItems = new JSONArray(json);
JSONObject json_data=null;
for(int i=0;i<chartItems.length();i++){
json_data = chartItems.getJSONObject(i);
//Retrieves the value of the name from the json object
name=json_data.getString("name");
//Retrieves the image url for that object and adds it to an arraylist
imageurl=json_data.getString("imageurl");
//imageurls.add(imageurl);
HashMap<String, String> hashMap = new HashMap<String, String>();
// adding each child node to HashMap key => value
//hashMap.put(TAG_RANK, rank);
hashMap.put(TAG_NAME, name);
hashMap.put(TAG_IMAGEURL, imageurl);
// adding HashMap to ArrayList
chartItemList.add(hashMap);
}
;
}
catch (JSONException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
public void run() {
list=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter = new LazyAdapter(JsonActivity.this, chartItemList);
list.setAdapter(adapter);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
});
return null;
}
//Removes the progress dialog when the data has been fetched
protected void onPostExecute(String args) {
progressDialog.dismiss();
}
}
}
My answer for this is Yes, it is wise enough to implement one more level network communication as far as your use case justifies it.
This depends on communication channel (EDGE/ 3G/ 4G/ WiFi) and use case of the application. Technically it is pretty much possible as far as you are doing this in background. It also depends on the size of the list which you are loading. Best way to check this is by implementing plug-able code and try it out.

Starting intent after Async task gets over

I created a login activity for my Android app. After the user enters the correct credentials, the login activity will switch over to the homepage but I don't know why my code won't switch and there is no error shown in my logcat. The manifest was also properly defined.
This is my login activity:
public class LoginEmployerActivity extends Activity {
Button btnLoginEmployer;
Button btnLinkToEmployerRegisterScreen;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;
TextView forgotPassword;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_CNAME = "cname";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
private ProgressDialog pDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_employer);
// Importing all assets like buttons, text fields
inputEmail = (EditText) findViewById(R.id.loginEmployerEmail);
inputPassword = (EditText) findViewById(R.id.loginEmployerPassword);
btnLoginEmployer = (Button) findViewById(R.id.btnLoginEmployer);
btnLinkToEmployerRegisterScreen = (Button) findViewById(R.id.btnLinkToEmployerRegisterScreen);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
forgotPassword = (TextView) findViewById(R.id.link_to_forgetPassword);
// Login button Click Event
btnLoginEmployer.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Checking for server respond
new LoginEmployer().execute();
}
}
});
// Link to Register Screen
btnLinkToEmployerRegisterScreen
.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterEmployerActivity.class);
startActivity(i);
finish();
}
});
// Link to forgot password link
forgotPassword.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Switching to forgot password screen
Intent i = new Intent(getApplicationContext(),
ForgotPasswordEmployerActivity.class);
startActivity(i);
}
});
}
// Background ASYNC Task to login by making HTTP Request
class LoginEmployer extends AsyncTask<String, String, String> {
// Before starting background thread Show Progress Dialog
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginEmployerActivity.this);
pDialog.setMessage("Authenticating...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
// Checking login in background
protected String doInBackground(String... params) {
runOnUiThread(new Runnable() {
public void run() {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
EmployerFunctions employerFunctions = new EmployerFunctions();
JSONObject json = employerFunctions.loginUser(email,
password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
loginErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if (Integer.parseInt(res) == 1) {
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandlerEmployer dbe = new DatabaseHandlerEmployer(
getApplicationContext());
JSONObject json_user = json
.getJSONObject("user");
// Clear all previous data in database
employerFunctions
.logoutUser(getApplicationContext());
dbe.addUser(
json_user.getString(KEY_NAME),
//json_user.getString(KEY_CNAME),
json_user.getString(KEY_EMAIL),
json.getString(KEY_UID),
json_user.getString(KEY_CREATED_AT));
// Launch Employer homePage Screen
Intent homepage = new Intent(
getApplicationContext(),
HomepageEmployerActivity.class);
// Close all views before launching Employer
// homePage
homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homepage);
// Close Login Screen
finish();
} else {
// Error in login
loginErrorMsg
.setText("Invalid username/password");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
// After completing background task Dismiss the progress dialog
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
EDITED CODE AFTER MOVING INTENT STATEMENT TO onPostExecute METHOD
public class LoginEmployerActivity extends Activity {
Button btnLoginEmployer;
Button btnLinkToEmployerRegisterScreen;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;
TextView forgotPassword;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_CNAME = "cname";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
private ProgressDialog pDialog;
boolean loginVerify= false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_employer);
// Importing all assets like buttons, text fields
inputEmail = (EditText) findViewById(R.id.loginEmployerEmail);
inputPassword = (EditText) findViewById(R.id.loginEmployerPassword);
btnLoginEmployer = (Button) findViewById(R.id.btnLoginEmployer);
btnLinkToEmployerRegisterScreen = (Button) findViewById(R.id.btnLinkToEmployerRegisterScreen);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
forgotPassword = (TextView) findViewById(R.id.link_to_forgetPassword);
// Login button Click Event
btnLoginEmployer.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Checking for server respond
new LoginEmployer().execute();
}
}
});
// Link to Register Screen
btnLinkToEmployerRegisterScreen
.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterEmployerActivity.class);
startActivity(i);
finish();
}
});
// Link to forgot password link
forgotPassword.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Switching to forgot password screen
Intent i = new Intent(getApplicationContext(),
ForgotPasswordEmployerActivity.class);
startActivity(i);
}
});
}
// Background ASYNC Task to login by making HTTP Request
class LoginEmployer extends AsyncTask<String, String, String> {
// Before starting background thread Show Progress Dialog
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginEmployerActivity.this);
pDialog.setMessage("Authenticating...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
// Checking login in background
protected String doInBackground(String... params) {
runOnUiThread(new Runnable() {
public void run() {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
EmployerFunctions employerFunctions = new EmployerFunctions();
JSONObject json = employerFunctions.loginUser(email,
password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
loginErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
if (Integer.parseInt(res) == 1) {
loginVerify = true;
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandlerEmployer dbe = new DatabaseHandlerEmployer(
getApplicationContext());
JSONObject json_user = json
.getJSONObject("user");
// Clear all previous data in database
employerFunctions
.logoutUser(getApplicationContext());
dbe.addUser(
json_user.getString(KEY_NAME),
json_user.getString(KEY_CNAME),
json_user.getString(KEY_EMAIL),
json.getString(KEY_UID),
json_user.getString(KEY_CREATED_AT));
} else {
// Error in login
loginErrorMsg
.setText("Invalid username/password");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
// After completing background task Dismiss the progress dialog
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
if ( loginVerify == true )
{
// Launch Employer homePage Screen
Intent homepage = new Intent(getApplicationContext(),
HomepageEmployerActivity.class);
// Close all views before launching Employer
// homePage
homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homepage);
// Close Login Screen
finish();
}
}
}
}
You are calling the intent to start a new activity inside the doInBackgorund() which runs on a non-UI thread and the Activity needs to be run on a UI thread. That is why your Login activity is never stopped.
Put the code to go to the new activity inside onPostExecute() or onProgressUpdate().
Here is something you can do.
Declare a global variable loginVerfied = false;
When your doInBackground verifies that the authenticity of the user, make loginVerified = true , otherwise keep it false.
Then inside onPostExecute()
if(loginVerifed == true)
{
Intent homepage = new Intent(getApplicationContext(),HomepageEmployerActivity.class
homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homepage);
finish();
}
EDIT :
Also, you have declared class LoginEmployer extends AsyncTask<String, String, String>, so to call it you need to use new LoginEmployer.execute(""); (you are missing the double quotes and not passing any String to the Task so it does not match it's parameters).
The first parameter in the definition of the AsyncTask is the datatype of the value being passed to it when execute() function is called. The second parameter is the datatype related to displaying progress during the time when the background thread runs. And the third parameter specifies the return value of the result.
More about AsyncTask here.
So, here is what you need to do now.
Declare the Async Task like this.
class LoginEmployer extends AsyncTask<String, Void, String> and make a call to it by using new LoginEmployer.execute(""). Make sure to return null from your doInBackground().
Hope this solves your problem now!
Add a checker to your AsyncTask such as
// Background ASYNC Task to login by making HTTP Request
class LoginEmployer extends AsyncTask<String, String, String> {
boolean validUser = false;
Then once the user is validated inside your background task set the value to true
if (Integer.parseInt(res) == 1) {
// user successfully logged in
// Store user details in SQLite Database
validUser = true; //set valid to true
Now in postExecute check if the user is valid
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
if ( validUser )
{
Intent homepage = new Intent( LoginEmployerActivity.this,
HomepageEmployerActivity.class);
// Close all views before launching Employer
// homePage
homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homepage);
}
I don't think you have added the Intent code here that will help you switch to another Activity.
protected void onPostExecute(String file_url) {
// dismiss the dialog once done // Intent Code Missing.
pDialog.dismiss();
You should do a UI work in UI thread and Non-UI work in Non-UI thread, thats a rule from the arrival of HoneyComb version of android.
You have added the below code in doInBackground(), That should be in onPostExcute()
Intent homepage = new Intent( getApplicationContext(), HomepageEmployerActivity.class);
homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homepage);

AsyncTask displaying progress bar

I am very new to android. I got two activities A, B . Activity A parse the data from the sever and iterate through the levels. and calls the activity B through intent. Activity B takes some time to display the data so I am trying to display the progress bar. Here is my code.
public class Display extends Activity {
ProgressDialog dialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.attributequestions);
new asynctask().execute();
}
class asynctask extends AsyncTask<Context,Void,Void>{
Survey[] surveyque=null;
// i hace created seperated class forsurvey that has info about data
String list[];
private ProgressDialog Dialog;
#Override
protected void onPreExecute()
{
Dialog=ProgressDialog.show(Display.this, "Parsing Data", "Please wait..........");
}
#Override
protected void onPostExecute(Void unused)
{
try
{
if(Dialog.isShowing())
{
Dialog.dismiss();
}
Intent intent=getIntent();
}
catch(Exception e)
{
Log.d("Onsitev4", "error");
}
}
#Override
protected Void doInBackground(Context... params) {
try {
LinearLayout layout1 = (LinearLayout) findViewById(R.id.linearLayout1);
//getting exception here. I dont understant why
// I have declared layout params and displaying activities in another class
ButtonView c = new ButtonView();
c.layout=layout1;
c.context =getBaseContext();
DbCoreSqlSurveys surveys=new DbCoreSqlSurveys(getBaseContext());
Document doc =surveys.getSurveySet();
surveyquestions= GetSurveyLevels(doc,c );
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
}
return null;
}
}
public SurveyObject[] GetSurveyLevels(Document doc, ButtonView c) {
NodeList nlQuestions = doc.getElementsByTagName("Survey");
SurveyObject[] allsurveys = new SurveyObject[nlQuestions.getLength()];
for (int i = 0; i < nlQuestions.getLength(); i++){
Node survey = nlQuestions.item(i);
String f =survey.getNodeName();
Log.d("OnsiteV4", "survey " + f);
NodeList surveyChildNodes = survey.getChildNodes();
SurveyObject s=new SurveyObject();
for (int j = 0; j < surveyChildNodes.getLength(); j++){
Node surveyChild = surveyChildNodes.item(j);
String h =surveyChild.getNodeName();
Log.d("OnsiteV4", "survey child node = " + h);
if (h !="#text"){
Surveys t = Surveys.valueOf(h);
switch(t){
case KeySurvey:
s.KeySurvey=surveyChild.getTextContent();
displaySurveyLink(s.SurveyDescription,"",c,0,s.SurveyDescription,"","","","");
break;
case SurveyDescription:
s.SurveyDescription=surveyChild.getTextContent();
displaySurveyLink(s.SurveyDescription,"",c,0,s.SurveyDescription,"","","","");
break;
case SurveyUserCode:
s.SurveyUserCode=surveyChild.getTextContent();
break;
case Level1:
if(surveyChild.hasChildNodes()){
s.Level1= processLevel1Nodes(surveyChild,c,s.SurveyDescription);
}
break;
default:
break;
}
}
allsurveys[i]=s;
}
}
return allsurveys;
}
// methods iterating through levels that is not showed
private void displaySurveyLink(final String description, String tag, ButtonView c, int indentation, final String surveyDescription, final String level1description, final String level2description, final String level3description, final String level4description)
{
if (description == null || tag == null){
return;
}
final TextView tv = c.addButton(description,tag,indentation);
tv.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
final Intent intent = new Intent();
intent.setClass(v.getContext(),ActivityB.class);
intent.putExtra("KeyLevel",tv.getTag().toString());
intent.putExtra("SurveyDescription",surveyDescription);
intent.putExtra("level1description",level1description);
intent.putExtra("level2description",level2description);
intent.putExtra("level3description",level3description);
intent.putExtra("level4description",level4description);
intent.putExtra("Description",description);
if (tv.getTag() != null){
if (tv.getTag().toString() != ""){
startActivity(intent);
}
}
}
});
}
}
I am getting exception in doinbackground. I am confused . please help me..
You are getting an exception because you are accessing UI elements on a non-UI thread. The main thread that the application creates is the UI thread, and that's where all of your visual elements are created and therefore the only thread in which you should access them.
To appropriately use AsyncTask, you run your long-running operations in doInBackground, and you use onPreExecute, onPostExecute and onProgressUpdated to work with the UI (show/hide progress dialogs, update views, etc). Whenever I use an AsyncTask and I want to show progress, I override onProgressUpdated giving it parameter type Integer and I call publishProgress from doInBackground. This would require a change of the base class signature from AsyncTask<Context,Void,Void> to AsyncTask<Context,Integer,Void>. You can use other object types for this as well...I just use Integer as an example if you want to show the percentage of the task that is complete, for example.
It's becoz your code should throwing exception as you are doing UI stuff in the doinbackgound of asyc task. Please remove all the UI related work from doingbackgound method.

Categories

Resources