如何传递一个JSON的ID到下一个活动? [英] How to pass ID of one JSON to next Activity?

查看:168
本文介绍了如何传递一个JSON的ID到下一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是新的我的应用程序到Android我在这我完全获取数据在那里我有用户列表现在我想,点击任何用户后,我需要展示自己的Profile中有一个ListFragment活动

公共类HomeFragment扩展ListFragment {

  // CustomAdapter适配器;
//私有列表< RowItem> rowItems;私人ProgressDialog pDialog;
// JSON解析器类
JSONParser jsonParser =新JSONParser();JSONArray匹配= NULL;ArrayList的<&HashMap的LT;字符串,字符串>>一个列表;
私人静态字符串MATCH_URL = NULL;
私有静态最后弦乐TAG_MATCH =匹配;
私有静态最后弦乐TAG_MATCH_ID =match_detail_id;
私有静态最后弦乐TAG_NAME =名;
私有静态最后弦乐TAG_PROFILE =PROFILE_ID;
私有静态最后弦乐TAG_IMAGE =形象;
私有静态最后弦乐TAG_CAST =投;
私有静态最后弦乐TAG_AGE =年龄;
私有静态最后弦乐TAG_LOCATION =地利;私人的ListView列表视图;串user_match_id;公共HomeFragment(){}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){    。字符串strText的= getArguments()的getString(user_login_id);
     MATCH_URL =htt​​p://abcd.com/webservice/matching?version=apps&user_login_id=\"+strtext;
    查看rootView = inflater.inflate(R.layout.fragment_home,集装箱,FALSE);
    ALIST =新的ArrayList<&HashMap的LT;字符串,字符串>>();   // rowItems =新的ArrayList< RowItem>();    列表视图=(ListView控件)rootView.findViewById(android.R.id.list);    新LoadAlbums()执行();
    返回rootView;
}类LoadAlbums扩展的AsyncTask<字符串,字符串的ArrayList<&HashMap的LT;字符串,字符串>>> {    / **
     *启动后台线程显示进度对话框之前
     * * /
    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        pDialog =新ProgressDialog(getActivity());
        pDialog.setMessage(正在加载...);
        pDialog.setIndeterminate(假);
        pDialog.setCancelable(假);
        pDialog.show();
    }
    保护的ArrayList<&HashMap的LT;字符串,字符串>> doInBackground(字符串参数... args){
        SH的ServiceHandler =新的ServiceHandler();        //制作到URL的请求和响应得到
        字符串jsonStr = sh.makeServiceCall(MATCH_URL,ServiceHandler.GET);        Log.d(回应:,>中+ jsonStr);        ArrayList的<&HashMap的LT;字符串,字符串>>的ListData =新的ArrayList<&HashMap的LT;字符串,字符串>>();        如果(jsonStr!= NULL){
            尝试{
                JSONObject的jsonObj =新的JSONObject(jsonStr);                //获取JSON数组节点
                匹配= jsonObj.getJSONArray(TAG_MATCH);                //通过所有联系人循环
                的for(int i = 0; I< matching.length();我++){
                    JSONObject的C = matching.getJSONObject(I)                    //创建新的HashMap
                    HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();                    //将每个子节点的HashMap键=>值
                    map.put(TAG_MATCH_ID,c.getString(TAG_MATCH_ID));
                    map.put(TAG_NAME,c.getString(TAG_NAME));
                    map.put(TAG_PROFILE,c.getString(TAG_PROFILE));
                    map.put(TAG_IMAGE,c.getString(TAG_IMAGE));
                    map.put(TAG_CAST,c.getString(TAG_CAST));
                    map.put(TAG_AGE,c.getString(TAG_AGE)+年);
                    map.put(TAG_LOCATION,c.getString(TAG_LOCATION));                    //添加HashList到ArrayList的
                    listData.add(地图);
                }
            }赶上(JSONException E){
                e.printStackTrace();
            }
        }其他{
            Log.e(的ServiceHandler,无法从URL得到任何数据);
        }        返回的ListData;
    }
    保护无效onPostExecute(ArrayList的<&HashMap的LT;字符串,字符串>>的结果){        super.onPostExecute(结果);
        //让所有专辑后,关闭该对话框
        如果(pDialog.isShowing())
            pDialog.dismiss();
        //从后台线程更新界面        / **
         *更新解析JSON数据到ListView控件
         * * /        listview.setOnItemClickListener(新OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
                    长ARG3){
                // TODO自动生成方法存根
                意向意图=新意图(getActivity(),ProfileEdit.class);
                intent.putExtra(match_detail_id,ARG2);
                startActivity(意向);
            }
        });
        如果(ALIST == NULL){
            ALIST =新的ArrayList<&HashMap的LT;字符串,字符串>>();
        }
        aList.addAll(结果);
        CustomAdapter适配器=新CustomAdapter(getActivity(),ALIST);
        setListAdapter(适配器);    }}

公共类ProfilePage延伸活动{

 私人ProgressDialog pDialog;AQuery androidAQuery =新AQuery(本);//私有静态最后弦乐TAG_MATCH_ID =match_detail_id;私有静态最后弦乐USER_NAME =名;
私有静态最后弦乐USER_AGE =年龄;
私有静态最后弦乐USER_LOCATION =地利;
私有静态最后弦乐USER_MOTHER_TONGE =mother_tounge;
私有静态最后弦乐USER_OCCU =占领;
私有静态最后弦乐USER_INCOM =收入;
私有静态最后弦乐USER_HEIGHT =高度;
私有静态最后弦乐USER_MARRAGE =婚姻状况;
私有静态最后弦乐USER_RELIGION =宗教;
私有静态最后弦乐USER_GOTRA =gotra;
私有静态最后弦乐USER_MANGLIK =manglik;
私有静态最后弦乐USER_RASHI =拉什;
私有静态最后弦乐USER_EDUCATION =教育;
私有静态最后弦乐USER_EAT =吃;
私有静态最后弦乐USER_DRINK =喝;
私有静态最后弦乐USER_SMOKE =烟;
私有静态最后弦乐USER_ABOUT =about_me;
私有静态最后弦乐USER_PIC =profile_pic;私人静态字符串USER_URL =;串user_match_id;私人ImageView的覆盖;
私人ImageView的是;
私人ImageView的无;
私人ImageView的SENDMSG;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.profile_page);    串matchId = this.getIntent()getStringExtra(match_detail_id);
    如果(matchId.trim()长度()大于0){
        USER_URL=\"http://abcds.com/webservice/matchingdetails?version=apps&match_detail_id=\"+user_match_id;
    }其他{
        Toast.makeText(ProfilePage.this,匹配ID空白,Toast.LENGTH_LONG).show();    }    。StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建();
    StrictMode.setThreadPolicy(政策);
     SH的ServiceHandler =新的ServiceHandler();        //制作到URL的请求和响应得到
        字符串jsonStr = sh.makeServiceCall(USER_URL,ServiceHandler.GET);        Log.d(回应:,>中+ jsonStr);    尝试{
        JSONObject的jsonObj =新的JSONObject(jsonStr);            字符串USER_NAME = jsonObj.getString(USER_NAME);
            字符串user_age = jsonObj.getString(USER_AGE);
            字符串USER_LOCATION = jsonObj.getString(USER_LOCATION);
            字符串user_mothertong = jsonObj.getString(USER_MOTHER_TONGE);
            字符串= USER_OCCUPATION jsonObj.getString(USER_OCCU);
            字符串user_income = jsonObj.getString(USER_INCOM);
            字符串user_height = jsonObj.getString(USER_HEIGHT);
            字符串user_marg = jsonObj.getString(USER_MARRAGE);
            字符串user_religion = jsonObj.getString(USER_RELIGION);
            字符串user_gotra = jsonObj.getString(USER_GOTRA);
            字符串user_manglik = jsonObj.getString(USER_MANGLIK);
            字符串user_rashi = jsonObj.getString(USER_RASHI);
            字符串user_education = jsonObj.getString(USER_EDUCATION);
            字符串user_eat = jsonObj.getString(USER_EAT);
            字符串user_drink = jsonObj.getString(USER_DRINK);
            字符串user_smoke = jsonObj.getString(USER_SMOKE);
            字符串user_about = jsonObj.getString(USER_ABOUT);
           字符串user_pro = jsonObj.getString(USER_PIC);            最终的TextView的uname =(的TextView)findViewById(R.id.namedetail);
           最终的TextView fdetail =(的TextView)findViewById(R.id.firstdetail);
            最终的TextView sdetail =(的TextView)findViewById(R.id.seconddetail);
            最终的TextView tdetail =(的TextView)findViewById(R.id.thirddetail);
            最终的TextView ocdetail =(的TextView)findViewById(R.id.txtoccupationdetail);
            最终的TextView incomedetail =(的TextView)findViewById(R.id.incomedetaile);
            最终的TextView uheight =(的TextView)findViewById(R.id.txtheightprofile);
            最终的TextView umrg =(的TextView)findViewById(R.id.txtmrgprofile);
            最终的TextView ureligion =(的TextView)findViewById(R.id.prohindu);
            最终的TextView ugotra =(的TextView)findViewById(R.id.gothraa);
            最终的TextView umanglik =(的TextView)findViewById(R.id.usermanglik);
            最终的TextView urashi =(的TextView)findViewById(R.id.rashi);
            最终的TextView udegree =(的TextView)findViewById(R.id.userdegree);
            最终的TextView ueat =(的TextView)findViewById(R.id.txteatprofile);
            最终的TextView udrink =(的TextView)findViewById(R.id.txtdrinkprofile);
            最终的TextView usmoke =(的TextView)findViewById(R.id.txtsmokeprofile);
            最终的TextView uabout =(的TextView)findViewById(R.id.txtabouther);
           最后ImageView的ucover =(ImageView的)findViewById(R.id.coverimage);            uname.setText(USER_NAME);
            fdetail.setText(user_age +年);
            sdetail.setText(USER_LOCATION);
            tdetail.setText(user_mothertong);
            ocdetail.setText(USER_OCCUPATION);
            incomedetail.setText(user_income);
            uheight.setText(user_height);
            umrg.setText(user_marg);
            ureligion.setText(user_religion);
            ugotra.setText(user_gotra);
            umanglik.setText(user_manglik);
            urashi.setText(user_rashi);
            udegree.setText(user_education);
            ueat.setText(user_eat);
            udrink.setText(user_drink);
            usmoke.setText(user_smoke);
            uabout.setText(user_about);
            androidAQuery.id(ucover)在图像配(user_pro,真实,真实);
    }赶上(JSONException E){
        e.printStackTrace();
    }


解决方案

尝试在片段覆盖onListItemClick:

  @覆盖
公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
    意向意图=新意图(getActivity(),ProfilePage.class);
    intent.putExtra(match_detail_id,aList.get(位置)获得(TAG_MATCH_ID));
    startActivity(意向);
}

代替人工setOnItemClickListener到的ListView所以不需要这个code:

  =列表视图(ListView控件)rootView.findViewById(android.R.id.list);listview.setOnItemClickListener(新OnItemClickListener(){
    @覆盖
    公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
       意向意图=新意图(getActivity()getApplicationContext(),ProfileEdit.class。);
       intent.putExtra(位置,ARG2);
       startActivity(意向);
    }
});

注意:当你手动使用ListFragment无需找ListView和设置项目点击以及setAdapter,直接得到这样setAdapter()和onListItemClick准备好所有的方法

hello I am new to android in my app i have one ListFragment activity in which i am getting data perfectly where i have list of users now i want that after click on any user i need to show their profile

public class HomeFragment extends ListFragment {

//CustomAdapter adapter;
//private List<RowItem> rowItems;

private ProgressDialog pDialog;
//JSON parser class
JSONParser jsonParser = new JSONParser();

JSONArray matching=null;

ArrayList<HashMap<String,String>> aList;
private static String MATCH_URL = null;
private static final String TAG_MATCH="matching";
private static final String TAG_MATCH_ID="match_detail_id";
private static final String TAG_NAME="name";
private static final String TAG_PROFILE="profile_id";
private static final String TAG_IMAGE="image";
private static final String TAG_CAST="cast";
private static final String TAG_AGE="age";
private static final String TAG_LOCATION="location";

private ListView listview;

String user_match_id;

public HomeFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    String strtext = getArguments().getString("user_login_id");
     MATCH_URL = "http://abcd.com/webservice/matching?version=apps&user_login_id="+strtext;
    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    aList = new ArrayList<HashMap<String,String>>();

   // rowItems = new ArrayList<RowItem>();

    listview=(ListView)rootView.findViewById(android.R.id.list);

    new LoadAlbums().execute();




    return rootView;
}

class LoadAlbums extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
        ServiceHandler sh = new ServiceHandler();

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

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

        ArrayList<HashMap<String,String>> listData =  new ArrayList<HashMap<String, String>>();

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                matching = jsonObj.getJSONArray(TAG_MATCH);

                // looping through All Contacts
                for (int i = 0; i < matching.length(); i++) {
                    JSONObject c = matching.getJSONObject(i);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_MATCH_ID, c.getString(TAG_MATCH_ID));
                    map.put(TAG_NAME,c.getString(TAG_NAME));
                    map.put(TAG_PROFILE, c.getString(TAG_PROFILE));
                    map.put(TAG_IMAGE, c.getString(TAG_IMAGE));
                    map.put(TAG_CAST, c.getString(TAG_CAST));
                    map.put(TAG_AGE, c.getString(TAG_AGE)+" years");
                    map.put(TAG_LOCATION, c.getString(TAG_LOCATION));

                    // adding HashList to ArrayList
                    listData.add(map);


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

        return listData;
    }


    protected void onPostExecute( ArrayList<HashMap<String,String>> result) {

        super.onPostExecute(result);
        // dismiss the dialog after getting all albums
        if (pDialog.isShowing())
            pDialog.dismiss();
        // updating UI from Background Thread

        /**
         * Updating parsed JSON data into ListView
         * */

        listview.setOnItemClickListener(new OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(getActivity(),ProfileEdit.class);
                intent.putExtra("match_detail_id", arg2);
                startActivity(intent);
            }
        });
        if(aList==null){
            aList =  new ArrayList<HashMap<String, String>>();
        }
        aList.addAll(result);
        CustomAdapter adapter = new CustomAdapter(getActivity(),aList);
        setListAdapter(adapter);

    }

}

public class ProfilePage extends Activity{

private ProgressDialog pDialog;

AQuery androidAQuery=new AQuery(this);

//private static final String TAG_MATCH_ID="match_detail_id";



private static final String USER_NAME="name";
private static final String USER_AGE="age";
private static final String USER_LOCATION="location";
private static final String USER_MOTHER_TONGE="mother_tounge";
private static final String USER_OCCU="occupation";
private static final String USER_INCOM="income";
private static final String USER_HEIGHT="height";
private static final String USER_MARRAGE="marital_status";
private static final String USER_RELIGION="religion";
private static final String USER_GOTRA="gotra";
private static final String USER_MANGLIK="manglik";
private static final String USER_RASHI="rashi";
private static final String USER_EDUCATION="education";
private static final String USER_EAT="eating";
private static final String USER_DRINK="drink";
private static final String USER_SMOKE="smoke";
private static final String USER_ABOUT="about_me";
private static final String USER_PIC="profile_pic";

private static String USER_URL="";

String user_match_id;

private ImageView cover;
private ImageView yes;
private ImageView no;
private ImageView sendmsg;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile_page);

    String matchId=this.getIntent().getStringExtra("match_detail_id");
    if(matchId.trim().length()>0){
        USER_URL="http://abcds.com/webservice/matchingdetails?version=apps&match_detail_id="+user_match_id;
    }else{
        Toast.makeText(ProfilePage.this,"match id blank",Toast.LENGTH_LONG).show();

    }

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
     ServiceHandler sh = new ServiceHandler();

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

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

    try {
        JSONObject jsonObj = new JSONObject(jsonStr);

            String user_name = jsonObj.getString(USER_NAME);
            String user_age = jsonObj.getString(USER_AGE);
            String user_location = jsonObj.getString(USER_LOCATION);
            String user_mothertong = jsonObj.getString(USER_MOTHER_TONGE);
            String user_occupation = jsonObj.getString(USER_OCCU);
            String user_income = jsonObj.getString(USER_INCOM);
            String user_height = jsonObj.getString(USER_HEIGHT);
            String user_marg = jsonObj.getString(USER_MARRAGE);
            String user_religion = jsonObj.getString(USER_RELIGION);
            String user_gotra = jsonObj.getString(USER_GOTRA);
            String user_manglik = jsonObj.getString(USER_MANGLIK);
            String user_rashi = jsonObj.getString(USER_RASHI);
            String user_education = jsonObj.getString(USER_EDUCATION);
            String user_eat = jsonObj.getString(USER_EAT);
            String user_drink = jsonObj.getString(USER_DRINK);
            String user_smoke = jsonObj.getString(USER_SMOKE);
            String user_about = jsonObj.getString(USER_ABOUT);
           String user_pro = jsonObj.getString(USER_PIC);

            final TextView uname = (TextView)findViewById(R.id.namedetail);
           final TextView fdetail = (TextView)findViewById(R.id.firstdetail);
            final TextView sdetail = (TextView)findViewById(R.id.seconddetail);
            final TextView tdetail = (TextView)findViewById(R.id.thirddetail);
            final TextView ocdetail=(TextView)findViewById(R.id.txtoccupationdetail);
            final TextView incomedetail = (TextView)findViewById(R.id.incomedetaile);
            final TextView uheight = (TextView)findViewById(R.id.txtheightprofile);
            final TextView umrg = (TextView)findViewById(R.id.txtmrgprofile);
            final TextView ureligion = (TextView)findViewById(R.id.prohindu);
            final TextView ugotra = (TextView)findViewById(R.id.gothraa);
            final TextView umanglik = (TextView)findViewById(R.id.usermanglik);
            final TextView urashi = (TextView)findViewById(R.id.rashi);
            final TextView udegree = (TextView)findViewById(R.id.userdegree);
            final TextView ueat = (TextView)findViewById(R.id.txteatprofile);
            final TextView udrink = (TextView)findViewById(R.id.txtdrinkprofile);
            final TextView usmoke = (TextView)findViewById(R.id.txtsmokeprofile);
            final TextView uabout = (TextView)findViewById(R.id.txtabouther);
           final ImageView ucover = (ImageView)findViewById(R.id.coverimage);

            uname.setText(user_name);
            fdetail.setText(user_age+" years");
            sdetail.setText(user_location);
            tdetail.setText(user_mothertong);
            ocdetail.setText(user_occupation);
            incomedetail.setText(user_income);
            uheight.setText(user_height);
            umrg.setText(user_marg);
            ureligion.setText(user_religion);
            ugotra.setText(user_gotra);
            umanglik.setText(user_manglik);
            urashi.setText(user_rashi);
            udegree.setText(user_education);
            ueat.setText(user_eat);
            udrink.setText(user_drink);
            usmoke.setText(user_smoke);
            uabout.setText(user_about);


            androidAQuery.id(ucover).image(user_pro, true, true);




    } catch (JSONException e) {
        e.printStackTrace();
    }

解决方案

Try to override onListItemClick in fragment :

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Intent intent = new Intent(getActivity(), ProfilePage.class);
    intent.putExtra("match_detail_id", aList.get(position).get(TAG_MATCH_ID));
    startActivity(intent);
}

Instead of manual setOnItemClickListener to listview so no required this code :

listview=(ListView)rootView.findViewById(android.R.id.list);

listview.setOnItemClickListener(new OnItemClickListener() {   
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
       Intent intent=new Intent(getActivity().getApplicationContext(),ProfileEdit.class);
       intent.putExtra("position", arg2);
       startActivity(intent);
    }
});

Note : when you use ListFragment no need to find Listview and set item click as well as setAdapter manually,directly get all method ready like setAdapter() and onListItemClick.

这篇关于如何传递一个JSON的ID到下一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆