2016-05-02 100 views
1

任何人都知道如何在每次點擊listview中的項目時顯示其他片段細節中的詳細信息項?以及如何設置它們?任何人都有這樣的例子嗎? enter image description here 列表視圖中僅顯示: - 那抹,gambar1,TIPE,mainmuscle,othermuscle,阿拉特,評級細節片段我想表明: -Nama,gambar1,TIPE,mainmuscle,othermuscle,阿拉特,等級,deskripsi,gambar2,gambar3,gambar4如何在listview中顯示片段中的項目詳細信息點擊

這裏我的代碼:

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

你在找onitemclick? – Pomagranite

+0

是的,itemonclick listview和打開詳細片段listview每個時間項目點擊 –

+0

當你在片段中使用listview,對不對?然後在itemclick上,您可以顯示一個自定義對話框,其中包含要顯示的所有詳細信息。 –

回答

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> 
+0

已經嘗試你的代碼,但沒有發生,當我單擊列表視圖上的項目 –

+0

listView.setOnItemClickListener(新的AdapterView。OnItemClickListener(本);在我的回答 –

+0

嗯,它的工作,但它的對話框,如何顯示在另一個片段? –

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.... 
     } 
}); 
+0

你能解釋一下處理他們的看法嗎?對不起,我真的「不好的代碼,但即時嘗試 –

0

你可以擴展Listfragment和overrride的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(); 
} 

} 
+0

嘿,謝謝你的回答,你能用我的代碼幫助實現thoose嗎? –

+0

嘿,謝謝你的回答,你能用我的代碼幫助實現thoose嗎? –

+0

是否要顯示itemclick的自定義對話框? –

相關問題