Android开发-数组运用-List点击通过数组传递Key-AndroidStudio

解析Json,并添加到List,如何在onClick的时候,把Json的某个key传递出去呢,我利用了数组。如果有更好的办法,请告诉我,谢谢!

/**
 * 数组 用于存放taskId
 * 数组赋值 taskIdArray[i] 在解析Json并list.add时赋值
 * 数组取值 taskIdArray[position] 在onItemClick并data.setTaskId时取值
 */
String[] taskIdArray = new String[100];
/**
 * JSONArray
 * 解析Json中的标题 并设置到 list
 * 以下对应 http://125.208.12.227/appceshi/Api
 */
try {
   JSONArray jsonArray = new JSONArray(result.toString());
   System.out.println("jsonArray.toString() = " + jsonArray.toString());
   System.out.println("jsonArray.length() = " + jsonArray.length());
   for(int i=0;i<jsonArray.length();i++)
   {
      JSONObject jsonObject = jsonArray.getJSONObject(i);
      Log.e("TAG", jsonObject.getString("id"));
      String taskId = jsonObject.getString("id");
      Log.e("TAG", jsonObject.getString("renwu"));
      String renwu = jsonObject.getString("renwu");
      list.add("taskId =  " + taskId + " , renwu = " + renwu);
      taskIdArray[i]=taskId;
   }
   taskAdapter = new TaskAdapter();
   listViewTask.setAdapter(taskAdapter);
} catch (JSONException e) {
   e.printStackTrace();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

   MyData data = new MyData();
   data.setPosition(position);
   data.setTaskId(taskIdArray[position]);
   Intent intent = new Intent();
   intent.putExtra(key, data);
   //用于发送消息给Fragment two
   getTargetFragment().onActivityResult(requestCode, Activity.RESULT_OK, intent);

   mainActivity.showTwoFragment();
}




















这里是完整的OneFragment.java:

package com.iwanghang.fragmenttransactiondemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 实现OnItemClickListener(Item点击事件监听),OnClickListener(按钮点击事件监听)
 */

public class OneFragment extends Fragment implements OnItemClickListener {

   /**
    * item
    */
   private ListView listViewTask;
   private List<String> list;
   private LayoutInflater inflater;
   private TaskAdapter taskAdapter;

   /**
    * MainActivity
    */
   private MainActivity mainActivity;

   /**
    * Json
    */
   private String title;
   private Volley4Json volley4Json;

   /**
    * 传值
    */
   public static int requestCode = 0;
   public static String key = "OneFragment.key";

   /**
    * 数组 用于存放taskId
    * 数组赋值 taskIdArray[i] 在解析Json并list.add时赋值
    * 数组取值 taskIdArray[position] 在onItemClick并data.setTaskId时取值
    */
   String[] taskIdArray = new String[100];

   //onAttach(),当fragment被绑定到activity时被调用(Activity会被传入.).
   @Override
   public void onAttach(Context context) {
      super.onAttach(context);
      mainActivity = (MainActivity) context;
   }

   //onCreateView(),创建和fragment关联的view hierarchy时调用.
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      /**
       * 实例化volley4Json
       */
      volley4Json = new Volley4Json(mainActivity);
      /**
       * UI组件初始化
       */
      View view = inflater.inflate(R.layout.fragment_one,null);
      /**
       * item初始化
       */
      listViewTask = (ListView) view.findViewById(R.id.listViewTask);
      /**
       * 解析Json
       */
      list = new ArrayList<String>();
      volley4Json.getJsonResultTaskList(new Volley4Json.VolleyCallBack() {
         @Override
         /**
          * JSONObject
          */
         //public void onSuccess(JSONObject result) {
         /**
          * JSONArray
          */
         public void onSuccess(JSONArray result) {
            System.out.println(result.toString());
            /**
             * JSONObject
             * 解析Json中的标题 并设置到 list
             * 以下对应 http://api.zsreader.com/v2/pub/channel/list?&page=1&tp=1&size=20
             */
//          try {
//             JSONArray jsonArray = result.getJSONArray("data");
//             for (int i = 0; i < jsonArray.length(); i++) {
//                JSONObject obj2 = jsonArray.getJSONObject(i);
//                Log.e("TAG", obj2.getString("title"));
//                title = obj2.getString("title");
//                list.add(i + " . " + title);
//             }
//             taskAdapter = new TaskAdapter();
//             listViewTask.setAdapter(taskAdapter);
//          } catch (JSONException e) {
//             e.printStackTrace();
//          }
            /**
             * JSONArray
             * 解析Json中的标题 并设置到 list
             * 以下对应 http://125.208.12.227/appceshi/Api
             */
            try {
               JSONArray jsonArray = new JSONArray(result.toString());
               System.out.println("jsonArray.toString() = " + jsonArray.toString());
               System.out.println("jsonArray.length() = " + jsonArray.length());
               for(int i=0;i<jsonArray.length();i++)
               {
                  JSONObject jsonObject = jsonArray.getJSONObject(i);
                  Log.e("TAG", jsonObject.getString("id"));
                  String taskId = jsonObject.getString("id");
                  Log.e("TAG", jsonObject.getString("renwu"));
                  String renwu = jsonObject.getString("renwu");
                  list.add("taskId =  " + taskId + " , renwu = " + renwu);
                  taskIdArray[i]=taskId;
               }
               taskAdapter = new TaskAdapter();
               listViewTask.setAdapter(taskAdapter);
            } catch (JSONException e) {
               e.printStackTrace();
            }

         }
      });

      /**
       * Item点击事件监听
       */
      listViewTask.setOnItemClickListener(this);

      return view;
   }

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

      MyData data = new MyData();
      data.setPosition(position);
      data.setTaskId(taskIdArray[position]);
      Intent intent = new Intent();
      intent.putExtra(key, data);
      //用于发送消息给Fragment two
      getTargetFragment().onActivityResult(requestCode, Activity.RESULT_OK, intent);

      mainActivity.showTwoFragment();
   }

   public class TaskAdapter extends BaseAdapter {
      public TaskAdapter() {
         super();
         inflater = LayoutInflater.from(mainActivity);
      }

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

      @Override
      public Object getItem(int position) {
         return list.get(position);
      }

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

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
         View view = convertView;
         if (view == null) {
            view = inflater.inflate(R.layout.item_task_list, parent, false);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.content = (TextView) view.findViewById(R.id.textView_task);
            view.setTag(viewHolder);
         }
         ViewHolder viewHolder = (ViewHolder) view.getTag();
         viewHolder.content.setText(list.get(position));
         return view;
      }
   }

   private class ViewHolder {
      private TextView content;
   }

}

你可能感兴趣的:(java,json,list,key,androidstudio)