我是新來的Android請好。 我有2個按鈕,第一個是得到JSON按鈕 二是解析JSON按鈕如何顯示解析的JSON到列表視圖?
第一個按鈕是工作,第二是工作,但它不會顯示在列表 視圖。
我只想讓EMAIL,USERNAME和STATUS將它顯示到列表視圖中。
這是JSON
{
"Users": [{
"id": "1",
"email": "[email protected]",
"username": "rbcastro17",
"password": "7e500d6afb638f362c292c21151bd7b17b0c70b5",
"code": "Pm5QYqIVwu",
"status": "Active",
"fpasscode": "uKlwIByiEW",
"user_info_id": "nI8D27"
}, {
"id": "8",
"email": "[email protected]",
"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();
}
} }
謝謝您的解答!
有很多糟糕的代碼在這裏,我可以看到很多地方的一些問題*威力*發生。嘗試使用調試器來檢測問題出在哪裏。你得到正確的解析json到對象?我的parseJson()被觸發?是否打開了一個DisplayUserListView?你不能只發佈一個問題,「這是行不通的」 - 告訴我們*什麼*不是準確的? :) – SadClown