2017-02-24 4 views
0

これは私のフラグメントクラスです - ここで私はJSON array objectを取得し、それを設定するためのアダプタクラスを呼び出します。対応するXML fileでは、私はカードビューとlsitビューを使ってデータを表示しています。私はonItemClickListener{}関数を実装する方法を知りたいので、別のアクティビティに移動してより詳細な方法でリストを表示することができます。私の質問は、JSONから取得したリストデータをフラグメント内でアクティビティに渡すにはどうすればいいですか?

{ 
    String url = "http://www.appplay.in/student_project/public/staff_details"; 

    ProgressDialog mProgressDialog; 
    ListView lvDetail; 
    Context context; 
    ArrayList<staff_model> myList = new ArrayList<>(); 
    ListView lv; 
    public static ArrayList<staff_model> staff_array; 

    JSONObject jsonObject; 
    JSONObject jsonKey; 
    Button submit; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) { 
     context=container.getContext(); 
     View v = inflater.inflate(R.layout.tab_fragment_2, container, false); 

     lvDetail = (ListView) v.findViewById(R.id.CustomList); 
     // submit=(Button)v.findViewById(R.id.button) ; 
     new GetGalleryimage().execute(); 
      v.setOnClickListener(new View.OnClickListener() { 
       @Override public void onClick(View v) { 
        Intent in = new Intent(getActivity(), Staff_Main.class); 
        startActivity(in); 
       } 
      }); 
     return v; 
    } 

    public void positionAction(View view) { 
     int position = (int) view.getTag(); 
     Toast.makeText(view.getContext(),Integer.toString(position),Toast.LENGTH_SHORT).show(); 
    } 

    private class GetGalleryimage extends AsyncTask<Void, Void, Void> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Showing progress dialog 
      mProgressDialog = new ProgressDialog(getActivity()); 
      mProgressDialog.setMessage("Please wait..."); 
      mProgressDialog.setCancelable(false); 
      mProgressDialog.show(); 
     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      // Creating service handler class instance 
      ServiceHandler sh = new ServiceHandler(); 

      // Making a request to url and getting response 
      String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

      Log.d("Response: ", "> " + jsonStr); 

      if (jsonStr != null) { 
       try { 
        JSONObject jsonObj = new JSONObject(jsonStr); 
        JSONArray jsonarr = jsonObj.getJSONArray("User_Details"); 
        for(int i=0; i<jsonarr.length(); i++) 
        { 
         JSONObject jsonObject = jsonarr.getJSONObject(i); 

         myList.add(new staff_model(jsonObject.getString("name"),jsonObject.getString("dob"),jsonObject.getString("occupation"),jsonObject.getString("emailid"),jsonObject.getString("contact"),jsonObject.getString("timetable"),jsonObject.getString("image"))); 

        } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } else { 
       Log.e("ServiceHandler", "Couldn't get any data from the url"); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      // Dismiss the progress dialog 
      Log.v("result",""+result); 
      if (mProgressDialog.isShowing()) 
       mProgressDialog.dismiss(); 
      staff_BaseAdapter rcAdapter = new staff_BaseAdapter(getActivity(),0, myList); 
      lvDetail.setAdapter(rcAdapter); 
     } 
    } 

    public class MAP_Detailservice extends 
      AsyncTask<String, String, String> { 

     Context mContext; 
     JSONObject mJsnObj; 
     ProgressDialog mProgressDialog; 

     @Override 
     protected void onPreExecute() { 
      showProgress(); 
      super.onPreExecute(); 
     } 

     @Override 
     protected String doInBackground(String... strings) { 
      JSONObject json = new JSONObject(); 
      String cricketAllMatchesLink = "http://www.appplay.in/student_project/public/staff_details"; 

      try { 
       return String.valueOf(HttpUtils.getJson(cricketAllMatchesLink)); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      Log.v("POST EXECUTE", "RESULT :" + result); 
      super.onPostExecute(result); 
      hideProgress(); 
      // mProgressDialog.dismiss(); 
      // if (HttpUtils.status == 408) { 
      // Toast.makeText(getActivity(), "Conection Lost ", 
      // Toast.LENGTH_SHORT).show(); 
      // } 
      try { 

       JSONArray array = new JSONArray(result); 

       JSONObject mJsonObject = array.getJSONObject(0); 

       // ArrayList<user_agent> GetSearchResults(); 
       ArrayList<staff_model> staff_result = new ArrayList<staff_model>(); 

       /** 
       * Question Array Parsing 
       */ 

       JSONArray jsonQuesstionsArray = mJsonObject 
         .getJSONArray("User_Details"); 

       for (int i = 0; i < jsonQuesstionsArray.length(); i++) { 
        // Reading questions 
        JSONObject jsonQuestionObject = jsonQuesstionsArray 
          .getJSONObject(i); 
        String name = jsonQuestionObject.getString("name"); 
        String dob = jsonQuestionObject.getString("dob"); 
        String occupation = jsonQuestionObject.getString("occupation"); 
        String emailid = jsonQuestionObject.getString("emailid"); 
        String contact = jsonQuestionObject.getString("contact"); 
        String timetable = jsonQuestionObject.getString("timetable"); 
        String image = jsonQuestionObject.getString("image"); 
       } 
       lvDetail.setAdapter(new staff_BaseAdapter(
         getActivity(), 0,staff_result)); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

    public void showProgress() { 
     mProgressDialog = new ProgressDialog(getActivity()); 
     mProgressDialog.setMessage("Loading please wait"); 
     mProgressDialog.setCancelable(false); 
     mProgressDialog.show(); 
    } 

    public void hideProgress() { 
     if (mProgressDialog != null && mProgressDialog.isShowing()) { 
      mProgressDialog.dismiss(); 
     } 
    } 

    @Override 
    public void onDestroy(){ 
     super.onDestroy(); 
     if (mProgressDialog!=null && mProgressDialog.isShowing()){ 
      mProgressDialog.cancel(); 
     } 
    } 
} 

これは私のアダプタクラスである - これは私のモデルクラスである

String title; 
int imgResId; 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public int getImgResId() { 
    return imgResId; 
} 

public void setImgResId(int imgResId) { 
    this.imgResId = imgResId; 
} 

} 

答えて

0

最初のモデルクラス「staff_model」を実装、私はデータを得ているし、コード

ArrayList<StaffList> myList = new ArrayList<StaffList>(); 
     LayoutInflater inflater; 
     Context context; 

     public StaffBaseAdapter(Context context, ArrayList<StaffList> myList) { 
      this.myList = myList; 
      this.context = context; 
      inflater = LayoutInflater.from(this.context); 
     } 

     @Override 
     public int getCount() { 
      return myList.size(); 
     } 

     @Override 
     public StaffList getItem(int position) { 
      return myList.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      MyViewHolder mViewHolder; 

      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.staff_list_view, parent, false); 
       mViewHolder = new MyViewHolder(convertView); 
       convertView.setTag(mViewHolder); 
      } else { 
       mViewHolder = (MyViewHolder) convertView.getTag(); 
      } 

      StaffList currentListData = getItem(position); 

      mViewHolder.tvTitle.setText(currentListData.getTitle()); 

      return convertView; 
     } 

     private class MyViewHolder { 
      TextView tvTitle, tvDesc; 
      ImageView ivIcon; 

      public MyViewHolder(View item) { 
       tvTitle = (TextView) item.findViewById(R.id.tvTitle); 
      } 
     } 
    } 

を設定それはあなたの単一のモデルアイテムのパスを他のアクティビティに移すのに役立ちます。 はその後TypeToken。あなたは、正確なJSONを持つことになります指定ここでは、あなたのgetView機能

MyViewHolder rowHolder=new MyViewHolder(convertView); 
rowHolder.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 

     //do whatever u want 


       } 
      }); 
0

をItemClickListenerを追加しますあなたはその意図に合格しました。

0
listView.setOnClickListener(new View.OnClickListener() { 
         public void onClick(View v) { 
     //Here you can convert your json to string 
     startActivity(new Intent(this, MainActivity.class).putExtra("myList",new Gson().toJson(list))); 
    } 
}); 

とあなたの受信アクティビティで

Type type = new TypeToken<List<String>>() { 
     }.getType(); 
     List<String> list1=new Gson().fromJson(getIntent().getStringExtra("myList"),type); 

にこれを追加するには、リストビューで

+0

ありがとうございます。私は間違いなくそれらを試して、あなたにバークを得るでしょう。 – Aakaash

関連する問題