以下是Activity
,它应该从JSON中获取值(用户),并将它们放在ArrayList<User>
中,其中User
是我创建的类。执行此操作后,应该使用这些值填充ListView
。问题是,每当我尝试打开活动时,它都会崩溃。
任何人都可以告诉我,我的问题是什么?这个课程是否适用于我想要的目的?
public class search extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.imageButton2: new StoreUserDataAsyncTask().execute();
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_searchh, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
ArrayList<User> userlist1=null;
public class StoreUserDataAsyncTask extends AsyncTask<Void,Void,ArrayList<User>> {
User user;
GetUserCallback userCallback;
ProgressDialog progressDialog;
Spinner s11 = (Spinner) findViewById(R.id.spinner);
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
String bloodd = s11.getSelectedItem().toString();
String loc = s2.getSelectedItem().toString();
protected ArrayList<User> doInBackground(Void... params) {
User returnedUser=null;
ArrayList<User> userlist=null;
final int CONNECTION_TIMEOUT = 1000 * 15;
final String SERVER_ADDRESS = "http://xxxxx.comli.com/";
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("bloodtype", bloodd));
dataToSend.add(new BasicNameValuePair("location", loc));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "/Search.php");
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
if (jObject.length() == 0) {
userlist = null;
Context context = getApplicationContext();
CharSequence text = "no available donor!";
int duration = Toast.LENGTH_SHORT;
} else {
String resultt = EntityUtils.toString(entity);
JSONArray jsonArray = new JSONArray(resultt);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String fname = jsonObject.getString("firstname");
String lname = jsonObject.getString("lastname");
String blood = jsonObject.getString("blood");
String location = jsonObject.getString("location");
String email = jsonObject.getString("email");
String age = jsonObject.getString("age");
String password = jsonObject.getString("password");
String phone = jsonObject.getString("phone");
int agee = Integer.parseInt(age);
int phonee = Integer.parseInt(phone);
returnedUser = new User(fname, lname, blood, location, email, password, agee, phonee);
userlist.add(returnedUser);
}
}
} catch (Exception e) {
e.printStackTrace();
}
userlist1=userlist;
return userlist;
}
protected void onPostExecute(ArrayList<User> aVoid) {
progressDialog.dismiss();
displayCountryList();
}
}
private void displayCountryList(){
MyCustomAdapter dataAdapter = null;
try {
//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this,
R.layout.listv, userlist1);
ListView listView = (ListView) findViewById(R.id.listView);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
//enables filtering for the contents of the given ListView
listView.setTextFilterEnabled(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
class MyCustomAdapter extends ArrayAdapter<User> {
private ArrayList<User> userlist;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<User> userlist) {
super(context, textViewResourceId, userlist);
this.userlist = new ArrayList<User>();
this.userlist.addAll(userlist);
}
private class ViewHolder {
TextView name;
TextView phone;
TextView email;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.listv, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.textView31);
holder.phone = (TextView) convertView.findViewById(R.id.textView33);
holder.email = (TextView) convertView.findViewById(R.id.textView34);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
User user = userlist.get(position);
holder.name.setText(user.getFirstname());
holder.phone.setText(user.getPhone());
holder.email.setText(user.getEmail());
return convertView;
}
}
}