这里是我的代码:
Fragment.java
public class AbdominalFragment extends Fragment {
// Log tag
private static final String TAG = AbdominalFragment.class.getSimpleName();
// Movies json url
private static final String url = "http.......";
private ProgressDialog pDialog;
private List<Exercise> exerciseList = new ArrayList<Exercise>();
private ListView listView;
private CustomListAdapter adapter;
public AbdominalFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_list, container, false);
// Inflate the layout for this fragment
final ListView listView = (ListView) rootView.findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), exerciseList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
if(exerciseList.isEmpty()) {
// Creating volley request obj
JsonArrayRequest exerciseReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
exerciseList.clear();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Exercise exercise = new Exercise();
if (obj.getString("tipe").equals("abdominal")) {
exercise.setNama(obj.getString("nama"));
exercise.setGambar1(obj.getString("gambar1"));
exercise.setTipe(obj.getString("tipe"));
exercise.setMainmuscle(obj.getString("mainmuscle"));
exercise.setAlat(obj.getString("alat"));
exercise.setTipe(obj.getString("othermuscle"));
exercise.setRating(obj.getDouble("rating"));
// adding exercise to exercise array
exerciseList.add(exercise);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(exerciseReq);
}else{
hidePDialog();
}
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
}
答案 0 :(得分:2)
listView.setOnItemClickListener(this);
制作片段implements AdapterView.OnItemClickListener
public class AbdominalFragment extends Fragment implements AdapterView.OnItemClickListener
现在 onClick
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Create custom dialog object
final Dialog dialog = new Dialog(context);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog); // layout of your dialog
// Set dialog title
dialog.setTitle("Detail");
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText(exerciseList.get(position).getNama());
// similar add statements for other details
dialog.show();
}
对话框 dialog.xml
的布局<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="@+id/imageDialog"/>
</RelativeLayout>
答案 1 :(得分:1)
在onCreate()
:
ListView list = (ListView) rootView.findViewById(R.id.listView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,int position, long id) {
ArrayList clickedItem = exerciseList.get(position);
//Here you can hadle your Views....
}
});
答案 2 :(得分:0)
我的建议是使用interface从ListView的OnItemClickListener发送回调到包含AbdominalFragment的活动的回调。
并且根据您的活动可以用细节片段替换AbdominalFragment。
有关与活动的沟通,请参阅以下链接
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
答案 3 :(得分:0)
您可以扩展Listfragment并覆盖onListItemClicked方法。
像这样,public class AbdominalFragment extends ListFragment {
String[] MONTHS = { "jan", "feb", "march", "april", "may", "june", "july",
"august", "sep", "octo", "nov", "dec" };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
inflater.getContext(), android.R.layout.simple_list_item_1,
MONTHS);
setListAdapter(adapter);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(getActivity(), "selected month :" + MONTHS[position],
Toast.LENGTH_LONG).show();
}
}