2013-05-06 3 views
0

Я думаю, чтобы показать вид списка в андроида из JSONListView Show Blank

DashboardActivity.java

public class DashboardActivity extends ListActivity { 

    private static String url = "http://git.drieanto.net/LagiDimanaAPI/index.php/user/get_following/"; 

    private static String KEY_FOLLOWING = "following"; 
    private static String KEY_SUCCESS = "success"; 
    private static String KEY_ERROR = "error"; 
    private static String KEY_ERROR_MSG = "error_msg"; 
    private static String KEY_REGID = "regid"; 
    private static String KEY_ID_USER = "id_user"; 
    private static String KEY_NAMA = "nama"; 
    private static String KEY_EMAIL = "email"; 
    private static String KEY_JKEL = "jenis_kelamin"; 
    private static String KEY_TLAHIR = "tanggal_lahir"; 
    private static String KEY_INSTANSI = "instansi"; 
    private static String KEY_JABATAN = "jabatan"; 
    private static String KEY_DIBUAT_AT = "dibuat_at"; 
    private static String KEY_AVATAR = "avatar"; 
    private static String KEY_STATUS = "status"; 
    UserFunctions userFunctions; 

    // following JSONArray 
     JSONArray following = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     /** 
     * Dashboard Screen for the application 
     * */   
     // Check login status in database 
     userFunctions = new UserFunctions(); 
     if(userFunctions.isUserLoggedIn(getApplicationContext())){ 
      setContentView(R.layout.dashboard); 

      DatabaseHandler db = new DatabaseHandler(getApplicationContext()); 

      HashMap user = db.getUserDetails(); 



      String regid = (String) user.get("regid"); 
     // Hashmap for ListView 
      ArrayList<HashMap<String, String>> followingList = new ArrayList<HashMap<String, String>>(); 

     // Creating JSON Parser instance 
      JSONParser jParser = new JSONParser(); 

      // getting JSON string from URL 
      JSONObject json = jParser.getJSONFromUrl(url+regid); 

      try { 
       // Getting Array of Contacts 
       following = json.getJSONArray(KEY_FOLLOWING); 

       // looping through All Contacts 
       for(int i = 0; i < following.length(); i++){ 
        JSONObject c = following.getJSONObject(i); 

        // Storing each json item in variable 
        String nama2 = c.getString(KEY_NAMA); 
        String email2 = c.getString(KEY_EMAIL); 
        String avatar2 = c.getString(KEY_AVATAR); 
        String status = c.getString(KEY_STATUS); 


        // creating new HashMap 
        HashMap<String, String> map = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        map.put(KEY_NAMA, nama2); 
        map.put(KEY_EMAIL, email2); 
        map.put(KEY_AVATAR, avatar2); 
        map.put(KEY_STATUS, status); 


        // adding HashList to ArrayList 
        followingList.add(map); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      /** 
      * Updating parsed JSON data into ListView 
      * */ 
      ListAdapter adapter = new SimpleAdapter(this, followingList, 
        R.layout.list_item, 
        new String[] { KEY_NAMA, KEY_EMAIL, KEY_AVATAR, KEY_STATUS }, new int[] { 
          R.id.nama, R.id.email, R.id.avatar, R.id.status }); 

      setListAdapter(adapter); 



      /*btnLogout.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View arg0) { 
        // TODO Auto-generated method stub 
        userFunctions.logoutUser(getApplicationContext()); 
        Intent login = new Intent(getApplicationContext(), LoginActivity.class); 
        login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(login); 
        // Closing dashboard screen 
        finish(); 
       } 
      });*/ 

     }else{ 
      // user is not logged in show login screen 
      Intent login = new Intent(getApplicationContext(), LoginActivity.class); 
      login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(login); 
      // Closing dashboard screen 
      finish(); 
     } 


    } 
} 

list_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <!-- Name Label --> 
     <TextView 
      android:id="@+id/nama" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#43bd00" 
      android:textSize="16sp" 
      android:textStyle="bold" 
      android:paddingTop="6dip" 
      android:paddingBottom="2dip" /> 
     <!-- Description label --> 
     <TextView 
      android:id="@+id/email" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#acacac" 
      android:paddingBottom="2dip"> 
     </TextView> 

     <TextView 
      android:id="@+id/avatar" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="#acacac" 
      android:paddingBottom="2dip"> 
     </TextView> 
     <!-- Linear layout for cost and price Cost: Rs.100 --> 
     <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <!-- Cost Label --> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#5d5d5d" 
      android:gravity="left" 
      android:textStyle="bold" 
      android:text="Status: " > 
     </TextView> 
     <!-- Price Label --> 
     <TextView 
      android:id="@+id/status" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#acacac" 
      android:textStyle="bold" 
      android:gravity="left"> 
     </TextView> 
     </LinearLayout> 
    </LinearLayout> 

</LinearLayout> 

я получить JSon данные http://git.drieanto.net/LagiDimanaAPI/index.php/user/get_following/XX2

я думаю, что этот код не содержит ошибки, но нет данных, показывающих только черный пробел, а не con Taining список может помочь мне, потому что это так importan для меня благодарственное-х

ответ

1

Да, он не будет показывать любую вещь, потому что вы получаете следующие исключения 1. NetworkOnMainThreadException

2. NullPointerException

Первый потому что вы пытаетесь выполнить работу сети в потоке пользовательского интерфейса.

Во-вторых, ваш JSONObject является null, когда вы пытаетесь получить к нему доступ, вы получите NullPointerException.

Попробуйте использовать Separate thread или AsyncTask для выполнения операций, связанных с сетевыми

+0

я понятия не имею, вы можете дать мне пример? –

+0

@EdwinAndrianto Проверьте этот http://developer.android.com/reference/android/os/AsyncTask.html и поместите свой код, т. Е. Извлеките значение из URL-адреса и разбора JSon в 'doInBackground' и пользовательском интерфейсе, то есть добавьте элементы в listview и т. Д. в 'onPostExecute' – Pragnani