简体   繁体   English

如何将一个JSON的ID传递给下一个Activity?

[英]How to pass ID of one JSON to next Activity?

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 你好,我是我的应用程序中的android新手,我有一个ListFragment活动,其中我可以完美地获取数据,现在我有用户列表,我想要在单击任何用户后都想要显示其个人资料

public class HomeFragment extends ListFragment { 公共类HomeFragment扩展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{ 公共类ProfilePage扩展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();
    }
Intent intent = new Intent(getBaseContext(), NewActivity.class);
intent.putExtra("KEY", Value);
startActivity(intent)

Try to override onListItemClick in fragment : 尝试覆盖片段中的onListItemClick:

@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 : 无需手动将setOnItemClickListener设置为listview,因此不需要以下代码:

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. 注意:使用ListFragment时,无需手动查找Listview并设置项目单击和setAdapter,直接准备好所有方法,如setAdapter()和onListItemClick。

Try with below code: 尝试以下代码:

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);
            }
        });

//get this data as below in ProfileEdit class //如下所示在ProfileEdit类中获取此数据

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从一个活动传递到下一个活动 - How to pass the data from one Activity to the next activity 如何将下载的图像从一个活动传递到下一个活动? - How do I pass a downloaded image from one activity to the next? 如何将json结果从一个活动传递到另一个活动 - How to pass json result from one activity to another 如何在选定的列表视图项中获取ID以打开上下文菜单并通过下一个活动进行意图? - How to get the id in selected listview item to open contextmenu and intent thru pass the next activity.? 如何将值从列表视图传递到下一个活动 - How to pass value from listview to the next activity 如何将用户提供的数据传递到下一个活动 - How to pass the data provided by the user to the next activity 如何将一个活动中的参数传递给另一活动? - How to pass the arguments in one activity to another activity? ListView适配器从LinkedHashSet获取信息 <UserItem> 。 如何将其中一个UserItem传递给下一个活动 - ListView Adapter gets info from a LinkedHashSet<UserItem>. How to pass one of those UserItems to next activity 如何在多项选择活动中更改问题并将分数传递到下一个活动 - How to change questions in a multiple choice activity and pass the score to the next activity 将ImageView传递到下一个活动 - Pass ImageView to next activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM