我在Activity中创建一个片段,我尝试使用AsyncTask解析从Internet获取的JSON字符串,并在列表视图中显示JSON解析数据。 项目正确显示在列表中。 但是,当我点击列表中的项目时,单击监听器不会触发。 我在这里错过了什么。
我的活动文件如下所示:
@SuppressWarnings("DefaultFileTemplate")
public class LaundryFragment extends BaseFragment {
private final String TAG = LaundryFragment.class.getSimpleName();
private ProgressDialog pDialog;
private ArrayList<HashMap<String, String>> laundryList;
private ListView lv ;
@Override
public void onStart() {
super.onStart();
sendView(LaundryTestActivity.class.getSimpleName(), null); //sending to Google Analytics
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.laundry_alerts, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
laundryList = new ArrayList<>();
new GetLaundry().execute();
lv = (ListView) getActivity().findViewById(R.id.laundry_list); //working
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("laundry",view.toString());
Toast.makeText(getActivity(),"clicked",Toast.LENGTH_LONG).show();
Log.d("laundry","log message");
}
});
}
/**
* Async task class to get json by making HTTP call
*/
private class GetLaundry extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>> > {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please wait...");
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String url = "https://www.laundryalert.com/cgi-bin/backoffice/MachineState.py?loc=redwolves&room=All&format=JSON";
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONObject location = jsonObj.getJSONObject("location");
//Array of rooms like STEM Den, Honors 3,...
JSONArray rooms = location.getJSONArray("rooms");
// looping through All rooms
for (int i = 0; i < rooms.length(); i++) {
JSONObject r = rooms.getJSONObject(i);
String roomName = r.getString("name");
int washerCount = 0;
int availableWasherCount = 0;
int dryerCount = 0;
int availableDryerCount = 0;
JSONObject c = rooms.getJSONObject(i);
JSONArray machines = c.getJSONArray("machines");
HashMap<String, String> laun = new HashMap<>();
for (int j = 0; j < machines.length() ; j++ ){
JSONObject machine = machines.getJSONObject(j);
if(machine.getString("description").contains("Washer")) {
washerCount++;
if(machine.getString("status").contains("Available")){
availableWasherCount++;
}
}
else {
dryerCount++;
if(machine.getString("status").contains("Available")){
availableDryerCount++;
}
}
}
laun.put("name",roomName);
laun.put("AvailWasher",Integer.toString(availableWasherCount));
laun.put("AvailDryer",Integer.toString(availableDryerCount));
laun.put("TotalWasher",Integer.toString(washerCount));
laun.put("TotalDryer", Integer.toString(dryerCount));
laundryList.add(laun);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity().getApplicationContext(),
"Json parsing error: " + e.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity().getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return laundryList;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
getActivity(), result,
R.layout.laundry_detail, new String[]{"name", "AvailWasher", "TotalWasher", "AvailDryer",
"TotalDryer"}, new int[]{R.id.tvResidenceName, R.id.availWasher, R.id.totalwasher,
R.id.availDryer, R.id.totalDryer});
lv.setAdapter(adapter);
Toast.makeText(getActivity(),"hello from post execute",Toast.LENGTH_SHORT).show();
}
}
}
laundry_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="@dimen/news_item_padding" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="@dimen/news_item_padding"
android:orientation="vertical">
<TextView
android:id="@+id/tvResidenceName"
android:text="Residence Name"
android:textSize="20dp"
android:textColor="@color/ASTATE_DARK_GRAY"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Available Washers"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/availWasher"
android:text="2"
android:layout_gravity="right"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Total Washers"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/totalwasher"
android:text="10"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#DCDCDC"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Available Dryers"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/availDryer"
android:text="0"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Total Dryers"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/totalDryer"
android:text="10"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>