Results 1 to 1 of 1
Thread: listView and listArray methods
- 09-09-2011, 10:08 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 7
- Rep Power
- 0
listView and listArray methods
Hello all -
I'm have issues outputting data to a listView. Not sure why but the app crashes hard. The error logs really don't say much to help me figure out why this it's crashing. Here's the code in it entirety... any help would be great! Thank so much in advance! btw... I'm really new to this forum and java so be genital ;) Also, these's another thread I created about Arrays it was another approach to the same problem. However, after much searching I think this is the direction I should be moving.
The HttpHelper I created.Java Code:import java.util.ArrayList; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.ListActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class StatusActivity extends ListActivity { /** Called when the activity is first created. */ TextView txt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.status); setListAdapter(new ArrayAdapter<String>(this, R.layout.status, this.fetchData())); ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // When clicked, show a toast with the TextView text Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); } }); } public ArrayList<String> fetchData() { // Set url, key, value to retrieve String URL = "http://www.xxx.com/android-json-list.php"; String KEY = "itemCode"; String VALUE = "6107"; HttpHelper httpHelper = new HttpHelper(); String returnedValues = httpHelper.getServerData(URL, KEY, VALUE); ArrayList<String> listItems = new ArrayList<String>(); try{ JSONArray jArray = new JSONArray(returnedValues); for(int i=0; i < jArray.length(); i++){ JSONObject json_data = jArray.getJSONObject(i); listItems.add(json_data.getString("itemDesc")); } } catch(JSONException e){ Log.e("log_tag_activity", "Error parsing data "+e.toString()); } return listItems; } }
Java Code:import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import android.util.Log; public class HttpHelper { public String returnString; public String getServerData(String URL, String KEY, String VALUE) { InputStream is = null; String returnString = ""; //the year data to send ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair(KEY,VALUE)); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(URL); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); } //convert response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); returnString = sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } return returnString; } }
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM -
Declare ListArray
By tommyyyy in forum New To JavaReplies: 1Last Post: 03-20-2009, 10:08 AM -
methods
By Zensai in forum New To JavaReplies: 10Last Post: 12-03-2007, 05:31 AM -
Methods
By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 08:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks