0

我想从 json 在 android 中显示列表视图

仪表板活动.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>

我从http://git.drieanto.net/LagiDimanaAPI/index.php/user/get_following/XX2获取 json 数据

我认为这段代码不包含错误,但没有数据显示只有黑色空白不包含列表可以帮助我,因为对我来说非常重要谢谢

4

1 回答 1

1

是的,它不会显示任何东西,因为您得到以下例外 1。NetworkOnMainThreadException

2.NullPointerException

第一个是因为您试图在 UI 线程上执行网络操作。

第二个是因为你的JSONObjectnull,当你试图访问它时,你会得到NullPointerException.

尝试使用Separate threadAsyncTask执行网络相关操作

于 2013-05-06T17:05:04.957 回答