regarding getting stuff from backend and displaying them as ListView in a Tab


Last Updated:

  1. learning_intern

    learning_intern Member This Topic's Starter

    Joined:
    Jul 28, 2010
    Messages:
    15
    Likes Received:
    0
    hi,
    i am trying to retrieve records from my mysql database and display them as a ListView in tabs . I am sending back( to my php script on the server) a particular value of id and then my php script runs a query and creates a JSON document, that is sent to me and i then store it in an arraylist of HashMaps and then by using a SimpleAdapter I am binding the arraylist to my Listview.

    The PROBLEM is that , when i try to do the above mentioned stuff in a project without tabs, i am able to see the desired result. BUT when i try and displa the listView like this in a Tab, I am unable to see anything, the Tab is just blank. Although i can see hard coded String[] as ListView in my other tabs. I am providing the code for both of my main project class and the other class that gets the records and tries to display in on the Tab screen but is unable to do so. PLEASE help me out -

    my main project class -

    Code (Text):
    1. package dev.sh;
    2.  
    3. import android.app.TabActivity;
    4.  
    5. import android.content.Intent;
    6. import android.content.res.Resources;
    7. import android.os.Bundle;
    8. import android.widget.TabHost;
    9.  
    10. public class androidtab extends TabActivity {
    11.     /** Called when the activity is first created. */
    12.     @Override
    13.     public void onCreate(Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.       // setContentView(R.layout.main);
    16.        
    17.         Resources res = getResources(); //Resource object to get the drawables (the tab icons here)
    18.         TabHost tab_host = getTabHost(); // the activity TabHost
    19.         TabHost.TabSpec spec;
    20.         Intent intent; // reusable Intent for each tab
    21.        
    22.         //Creating an Intent to launch an Activity for the tab
    23.         intent = new Intent().setClass(this, AlphaActivity.class);
    24.         spec = tab_host.newTabSpec("projects").setIndicator("Projects",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
    25.         tab_host.addTab(spec);
    26.      
    27.         intent = new Intent().setClass(this, BetaActivity.class);
    28.         spec = tab_host.newTabSpec("tasks").setIndicator("Tasks",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
    29.         tab_host.addTab(spec);
    30.        
    31.         intent = new Intent().setClass(this, GammaActivity.class);
    32.         spec = tab_host.newTabSpec("display").setIndicator("Display",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);
    33.         tab_host.addTab(spec);
    34.  
    35.         tab_host.setCurrentTab(1);
    36.      
    37.                
    38.     }
    39. }

    my other class that tries to get the backend records and display them as a ListView is -

    Code (Text):
    1. package dev.sh;
    2.  
    3. import java.io.BufferedReader;
    4. import java.io.InputStream;
    5. import java.io.InputStreamReader;
    6. import java.util.ArrayList;
    7. import java.util.HashMap;
    8. import java.util.List;
    9. import java.util.Map;
    10.  
    11. import org.apache.http.HttpEntity;
    12. import org.apache.http.HttpResponse;
    13. import org.apache.http.NameValuePair;
    14. import org.apache.http.client.HttpClient;
    15. import org.apache.http.client.entity.UrlEncodedFormEntity;
    16. import org.apache.http.client.methods.HttpPost;
    17. import org.apache.http.impl.client.DefaultHttpClient;
    18. import org.apache.http.message.BasicNameValuePair;
    19. import org.json.JSONArray;
    20. import org.json.JSONException;
    21. import org.json.JSONObject;
    22.  
    23. import android.app.ListActivity;
    24. import android.os.Bundle;
    25. import android.util.Log;
    26. import android.widget.ListView;
    27. import android.widget.SimpleAdapter;
    28.  
    29. public class BetaActivity extends ListActivity {
    30.     public void onCreate(Bundle savedInstanceState) {
    31.         super.onCreate(savedInstanceState);
    32.        
    33.         String result = "";
    34.        
    35.           //the year data to send
    36.        
    37.           ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    38.        
    39.           nameValuePairs.add(new BasicNameValuePair("task_id","2"));
    40.          
    41.           ListView lv = getListView();
    42.             lv.setTextFilterEnabled(true);
    43.        
    44.            
    45.        
    46.           //http post
    47.        
    48.        try{
    49.        
    50.                  HttpClient httpclient = new DefaultHttpClient();
    51.        
    52.                   HttpPost httppost = new HttpPost("http://192.168.1.8/testhttpphp.php");
    53.      
    54.                   httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    55.      
    56.                 HttpResponse response = httpclient.execute(httppost);
    57.      
    58.                HttpEntity entity = response.getEntity();
    59.      
    60.                   InputStream is = entity.getContent();
    61.                //   FileOutputStream os = new FileOutputStream(file);
    62.      
    63.        
    64.      
    65.           //convert response to string
    66.                   try{
    67.                      
    68.                       BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
    69.          
    70.                       StringBuilder sb = new StringBuilder();
    71.                         String line = null;
    72.                         while ((line = reader.readLine()) != null) {
    73.                                 sb.append(line + "\n");
    74.                         }
    75.          
    76.                       is.close();
    77.          
    78.                
    79.          
    80.                       result=sb.toString();
    81.          
    82.               }catch(Exception e){
    83.          
    84.                       Log.e("log_tag", "Error converting result "+e.toString());
    85.          
    86.               }
    87.          
    88.                
    89.          
    90.               //parse json data
    91.          
    92.               try{
    93.          
    94.                       JSONArray jArray = new JSONArray(result);
    95.                      
    96.                       List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    97.  
    98.                     // -- list item hash re-used
    99.                     Map<String, String> group;
    100.                  // -- create record
    101.          
    102.                       for(int i=0;i<jArray.length();i++){
    103.          
    104.                               JSONObject json_data = jArray.getJSONObject(i);
    105.                        
    106.                               group = new HashMap<String, String>();
    107.                               group.put( "name",json_data.getString("name") );
    108.                               groupData.add(group);
    109.                              
    110.                              
    111.          
    112.                       }//end of for
    113.                      
    114.                       SimpleAdapter adapter = new SimpleAdapter( this, groupData, android.R.layout.simple_list_item_2,
    115.                               new String[] {"name" },
    116.                               new int[]{ android.R.id.text1} );
    117.                       setListAdapter(adapter);
    118.          
    119.               }
    120.          
    121.               catch(JSONException e){
    122.          
    123.                       Log.e("log_tag", "Error parsing data "+e.toString());
    124.          
    125.               }
    126.      
    127.          
    128.        }catch(Exception e){
    129.              
    130.          Log.e("log_tag", "Error in http connection "+e.toString());
    131.  
    132.  }
    133.  
    134. }// end of onCreate
    please tell me why it doesn't work. also i don't know where can i read my Log entries. I use an emulator and do not test my app on a device. i tried running 'adb logcat', it gives me a lot of stuff which doesn't make sense to me, so is there a way i could see my log as a text file or something similar?

    Thanks in advance
     

    Advertisement
  2. learning_intern

    learning_intern Member This Topic's Starter

    Joined:
    Jul 28, 2010
    Messages:
    15
    Likes Received:
    0
    I was able to solve the problem myself, please ignore this problem. i was missing the permission for internet in my manifest file.

    thanks
     

Share This Page

Loading...