如何将解析后的JSON显示到列表视图?

时间:2016-11-16 15:27:23

标签: android arrays json listview android-arrayadapter

我是Android新手,请你好。 我有2个按钮,第一个是获取JSON按钮 第二个是解析JSON按钮

第一个按钮正在工作,第二个按钮正在工作,但它不会显示列表 图。

我只想获取EMAIL,USERNAME和STATUS以将其显示在列表视图中。

这是JSON

{
"Users": [{
    "id": "1",
    "email": "rafael.raybcastro@gmail.com",
    "username": "rbcastro17",
    "password": "7e500d6afb638f362c292c21151bd7b17b0c70b5",
    "code": "Pm5QYqIVwu",
    "status": "Active",
    "fpasscode": "uKlwIByiEW",
    "user_info_id": "nI8D27"
}, {
    "id": "8",
    "email": "johanndelapaz@gmail.com",
    "username": "johanndelapaz",
    "password": "ba10614defa1d9cee433e92b0041b7f7700bda20",
    "code": "NUXThKq5Fv",
    "status": "Active",
    "fpasscode": null,
    "user_info_id": "tDsnRf"
}] }

这是主要活动类

public class AdminListofUserActivity extends AppCompatActivity {

String json_string;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin_listof_user);
}

public void getJSON(View view)
{

    new AdminListofUserBackgroundTask().execute();

}

class AdminListofUserBackgroundTask extends AsyncTask<Void, Void, String>
{

    String json_url;
    String JSON_STRING;

    @Override
    protected void onPreExecute() {
        json_url = "http://*ipaddress*/mobilecollabup/admin_listuser.php";
    }

    @Override
    protected String doInBackground(Void... params) {
        try {
            URL url = new URL(json_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            while ((JSON_STRING = bufferedReader.readLine())!=null)
            {


                stringBuilder.append(JSON_STRING+"\n");
            }

                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        TextView textView = (TextView) findViewById(R.id.textview);
        textView.setText(result);
        json_string = result;
    }
}

public void parseJSON(View view)
{

    if (json_string==null)
    {

        Toast.makeText(getApplicationContext(),"First Get JSON", Toast.LENGTH_LONG).show();

    }
    else
    {

        Intent intent = new Intent(this, aDisplayUserListView.class);
        intent.putExtra("json_data",json_string);
        startActivity(intent);

    }

} }

适配器类

public class UserAdapter extends ArrayAdapter {
List list = new ArrayList();
public UserAdapter(Context context, int resource) {
    super(context, resource);
}


public void add(Users object) {
    super.add(object);
    list.add(object);
}

@Override
public int getCount() {
    return list.size();
}


@Override
public Object getItem(int position) {
    return list.get(position);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row;
    row = convertView;
    UserHolder userHolder;
    if(row==null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.row_layout, parent, false);
        userHolder = new UserHolder();
        userHolder.tx_username = (TextView) row.findViewById(R.id.tx_username);
        userHolder.tx_email = (TextView) row.findViewById(R.id.tx_email);
        userHolder.tx_status = (TextView) row.findViewById(R.id.tx_status);
        row.setTag(userHolder);


    }
    else
    {
        userHolder = (UserHolder) row.getTag();
    }

    Users users = (Users)this.getItem(position);
    userHolder.tx_username.setText(users.getUsername());
    userHolder.tx_email.setText(users.getEmail());
    userHolder.tx_status.setText(users.getStatus());

    return row;
}

static class UserHolder
{
    TextView tx_username,tx_email,tx_status;
} }

用户类

public class Users {

private String id,email,username,password,code,status,fpasscode,user_info_id;

public Users(String id, String email, String username, String password, String code, String status, String fpasscode, String user_info_id)
{
    this.setId(id);
    this.setEmail(email);
    this.setUsername(username);
    this.setPassword(password);
    this.setCode(code);
    this.setStatus(status);
    this.setFpasscode(fpasscode);
    this.setUser_info_id(user_info_id);

}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getFpasscode() {
    return fpasscode;
}

public void setFpasscode(String fpasscode) {
    this.fpasscode = fpasscode;
}

public String getUser_info_id() {
    return user_info_id;
}

public void setUser_info_id(String user_info_id) {
    this.user_info_id = user_info_id;
} }

显示列表视图类

public class aDisplayUserListView extends AppCompatActivity {

String json_string;
JSONObject jsonObject;
JSONArray jsonArray;
UserAdapter userAdapter;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adisplay_user_list_layout);
    listView = (ListView) findViewById(R.id.listview);
    userAdapter = new UserAdapter(this, R.layout.row_layout);
    listView.setAdapter(userAdapter);
    json_string = getIntent().getExtras().getString("json_data");
    try {
        jsonObject = new JSONObject(json_string);
        jsonArray = jsonObject.getJSONArray("Users");
        //jsonArray = new JSONArray(json_string);
        int count = 0;
        String id,email,username,password,code,status,fpasscode,user_info_id;
        //String email,username,status;

        while (count>jsonArray.length())
        {
            JSONObject JO = jsonArray.getJSONObject(count);
            id = JO.getString("id");
            email = JO.getString("email");
            username = JO.getString("username");
            password = JO.getString("password");
            code = JO.getString("code");
            status = JO.getString("status");
            fpasscode = JO.getString("fpasscode");
            user_info_id = JO.getString("user_info_id");

            //Users users = new Users(username,email,status);

            Users users = new Users(id,email,username,password,code,status,fpasscode,user_info_id);

            userAdapter.add(users);


            count++;

        }



    } catch (JSONException e) {
        e.printStackTrace();
    }


} }

感谢您的回答!

2 个答案:

答案 0 :(得分:0)

您需要doInBackground()返回包含EMAIL,USERNAME和STATUS字符串的对象ArrayList。您还应该让您的适配器接受该列表作为参数,并在getView()方法中使用*your List of Objects*.get(position);来访问您的每个EMAIL,USERNAME和STATUS字符串。您可以在此处设置UI Cell上的值。

然后,在onPostExecute()中将结果ArrayList传递到适配器并在ListView上设置适配器。

答案 1 :(得分:0)

请参阅此链接,了解如何解析JSON数据。

https://www.simplifiedcoding.net/android-volley-tutorial-to-get-json-from-server/