如何从片段发送ID到活动? [英] How to send id from fragment to activity?

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

问题描述

你好,我是新来的机器人,在我的应用程序我得到的列表视图使用一个以上的用户列表 JSON 和它有一个唯一的ID,它看起来是这样的。

我完全得到这个数据,我的应用程序,现在在这个 JSON 我matching_id其独特的,现在我想获得的用户配置文件按照此ID在第二页其 JSON 看起来像这样让我们 ID = 636 ,所以我希望得到的信息。

Homefragment类

 公共类HomeFragment扩展ListFragment {

    // CustomAdapter适配器;
    //私有列表< RowItem> rowItems;

    私人ProgressDialog pDialog;
    // JSON解析器类
    JSONParser jsonParser =新JSONParser();

    JSONArray匹配= NULL;

    ArrayList的< HashMap的<字符串,字符串>>一个列表;
    私有静态字符串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列表视图;

    公共HomeFragment(){}

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){

        。串strText的= getArguments()的getString(user_login_id);
         MATCH_URL =htt​​p://abcsd.com/webservice/matching?version=apps&user_login_id="+strtext;
        查看rootView = inflater.inflate(R.layout.fragment_home,集装箱,假);
        ALIST =新的ArrayList< HashMap的<字符串,字符串>>();

       // rowItems =新的ArrayList< RowItem>();

        列表视图=(ListView控件)rootView.findViewById(android.R.id.list);

        新LoadAlbums()执行();



        返回rootView;
    }

    类LoadAlbums扩展的AsyncTask<字符串,字符串,字符串> {

        / **
         *在启动后台线程显示进度对话框
         * * /
        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
            pDialog =新ProgressDialog(HomeFragment.this.getActivity());
            pDialog.setMessage(载入中...);
            pDialog.setIndeterminate(假);
            pDialog.setCancelable(假);
            pDialog.show();
        }
        保护字符串doInBackground(字符串参数... args){
            ServiceHandler SH =新ServiceHandler();

            //制作一个请求URL并得到响应
            字符串jsonStr = sh.makeServiceCall(MATCH_URL,ServiceHandler.GET);

            Log.d(回应:,>中+ jsonStr);

            如果(jsonStr!= NULL){
                尝试 {
                    JSONObject的jsonObj =新的JSONObject(jsonStr);

                    //获取JSON数组节点
                    匹配= jsonObj.getJSONArray(TAG_MATCH);

                    //遍历所有联系人
                    的for(int i = 0; I< matching.length();我++){
                        JSONObject的C = matching.getJSONObject(我);




                        //保存在变量中的每个JSON项目的值
                        字符串user_match_id = c.getString(TAG_MATCH_ID);
                        字符串用户名= c.getString(TAG_NAME);
                        字符串USER_PROFILE = c.getString(TAG_PROFILE);
                        字符串user_image = c.getString(TAG_IMAGE);
                        字符串user_cast = c.getString(TAG_CAST);
                        字符串user_age = c.getString(TAG_AGE);
                        字符串user_location = c.getString(TAG_LOCATION);



                        //创建新的HashMap
                        HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();

                        //添加每个子节点HashMap中的key =>值
                        map.put(TAG_MATCH_ID,user_match_id);
                        map.put(TAG_NAME,用户名);
                        map.put(TAG_PROFILE,USER_PROFILE);
                        map.put(TAG_IMAGE,user_image);
                        map.put(TAG_CAST,user_cast);
                        map.put(TAG_AGE,user_age +年);
                        map.put(TAG_LOCATION,user_location);



                        //添加HashList到ArrayList中
                        aList.add(图)


                    }
                }赶上(JSONException E){
                    e.printStackTrace();
                }
            } 其他 {
                Log.e(ServiceHandler,无法从URL中得到任何数据);
            }

            返回null;
        }


        保护无效onPostExecute(字符串file_url){

            super.onPostExecute(file_url);
            让所有专辑后//关闭该对话框
            如果(pDialog.isShowing())
            pDialog.dismiss();
            //从后台线程更新UI

                    / **
                     *更新解析JSON数据到ListView控件
                     * * /
            CustomAdapter适配器=新CustomAdapter(getActivity(),ALIST);
            setListAdapter(适配器);

                }

        }




}
 

这是现在下一个活动做工精细我想要得到的全部细节每秒 JSON

在下面的活动我想获得用户的细节我应该怎么办?

 公共类ProfilePage延伸活动{

私人ImageView的覆盖;
私人ImageView的是;
私人ImageView的无;
私人ImageView的SENDMSG;

@覆盖
保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.profile_page);

    盖=(ImageView的)findViewById(R.id.coverimage);
    是=(ImageView的)findViewById(R.id.yesbutton);
    无=(ImageView的)findViewById(R.id.nobutton);
    SENDMSG =(ImageView的)findViewById(R.id.imgsendmsgg);
    sendmsg.setOnClickListener(新OnClickListener()
    {

        @覆盖
        公共无效的onClick(查看arg0中)
        {
            AlertDialog.Builder建设者=新AlertDialog.Builder(ProfilePage.this);
            builder.setTitle(R.string.title_alertbox_upgrade)
                    .setIcon(R.drawable.ic_launcher)
                   .setMessage(R.string.chek_upgrade)
                   .setCancelable(真)
                   .setNegativeButton(R.string.okalert_upgrade,新DialogInterface.OnClickListener(){
                       公共无效的onClick(DialogInterface对话框,INT ID){
                            dialog.dismiss();
                       }
                   });

            AlertDialog welcomeAlert = builder.create();
            welcomeAlert.show();
            //使TextView的点击。演出后,必须调用()
            ((TextView)welcomeAlert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
            / * //创建警报对话框有一个按钮

            AlertDialog alertDialog =新AlertDialog.Builder(
                    ProfileEdit.this).create();

            //设置对话框标题
            alertDialog.setTitle(编辑个人资料);

            //设置对话框消息
            alertDialog.setMessage(关于请登录gujjumatch.com桌面站点编辑您的资料+
                    并设置其他细节或致电91 281 3054120);

            //设置图标,以对话框
            alertDialog.setIcon(R.drawable.ic_launcher);

            //设置OK按钮
            alertDialog.setButton(OK,
                    新DialogInterface.OnClickListener(){

                        公共无效的onClick(DialogInterface对话框,
                                其中INT){
                            //写您的code此对话框后执行
                            // 关闭
                            Toast.makeText(getApplicationContext(),
                                    谢谢你,Toast.LENGTH_SHORT)
                                    。显示();
                        }
                    });

            //显示警报信息
            alertDialog.show(); * /


        }
    });
    yes.setOnClickListener(新OnClickListener()

    {

        @覆盖
        公共无效的onClick(查看为arg0){

            LayoutInflater充气= getLayoutInflater();


            查看布局= inflater.inflate(R.layout.custom_toast,
                      (ViewGroup中)findViewById(R.id.custom_toast_layout_id));

            ImageView的形象=(ImageView的)layout.findViewById(R.id.image);
            image.setImageResource(R.drawable.yes);

            //设置消息
            TextView的文字=(TextView的)layout.findViewById(R.id.text);
            text.setText(利息已发送);



            //吐司...
            吐司面包=新吐司(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(布局);
            toast.show();

        }
    });

    no.setOnClickListener(新OnClickListener(){

        @覆盖
        公共无效的onClick(查看为arg0){

            LayoutInflater充气= getLayoutInflater();


            查看布局= inflater.inflate(R.layout.custom_toast,
                      (ViewGroup中)findViewById(R.id.custom_toast_layout_id));

            ImageView的形象=(ImageView的)layout.findViewById(R.id.image1);
            image.setImageResource(R.drawable.no);

            //设置消息
            TextView的文字=(TextView的)layout.findViewById(R.id.text1);
            text.setText(利息拒绝);



            //吐司...
            吐司面包=新吐司(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(布局);
            toast.show();

        }
    });
    cover.setOnClickListener(新OnClickListener(){
                    @覆盖
        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根
            //发送图片ID为FullScreenActivity
               意图I =新的意图(getApplicationContext(),Fullimage.class);
               //通过数组索引


               startActivity(ⅰ);

        }
    });
}

 }
 

解决方案

这是你的的ListView 中的片段类。

我认为你可以创建ListView的轻松......

现在的点击的ListView,你需要通过这样的数据。

  lv.setOnItemClickListener(新OnItemClickListener(){
    @覆盖
    公共无效onItemClick(适配器视图<>为arg0,查看ARG1,
                    INT ARG2,长ARG3){
        意向意图=新的意图(getActivity(),UserBrandsFragmentDetail.class);
        intent.putExtra(brand_logo,m_ArrayList.get(ARG2).brand_logo);
        startActivity(意向);
    }
}
 

更新

您需要有ListView控件。然后在点击的ListView,你需要用我上面提到的code的...

在我上面提到的codeLV是ListView控件。 :)

在你的下一个活动,你需要使用包......这样来获取数据。

 捆绑额外= getIntent()getExtras();
串your_data = extra.getString(your_key_while_parsing_data_through_Intent);
 

这就是它..你是好去!

有关创建列表视图中,你可以Google一下。有很多教程适用于这一点。

让我知道,如果你还有任何形式的查询。

Hello i am new to android,in my app i am getting more than one users list in Listview using json and it has one unique id it looks like this.

I am getting this data perfectly in my apps,now in this json i have matching_id which unique now i want to get user profile as per this id in second page and its json looks like this lets take id=636 so i want to get details..

Homefragment class

    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;

    public HomeFragment(){}

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

        String strtext = getArguments().getString("user_login_id");
         MATCH_URL = "http://abcsd.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, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(HomeFragment.this.getActivity());
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        protected 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);

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




                        // Storing each json item values in variable
                        String user_match_id=c.getString(TAG_MATCH_ID);
                        String user_name = c.getString(TAG_NAME);
                        String user_profile=c.getString(TAG_PROFILE);
                        String user_image=c.getString(TAG_IMAGE);
                        String user_cast=c.getString(TAG_CAST);
                        String user_age=c.getString(TAG_AGE);
                        String user_location=c.getString(TAG_LOCATION);



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

                        // adding each child node to HashMap key => value
                        map.put(TAG_MATCH_ID, user_match_id);
                        map.put(TAG_NAME,user_name);
                        map.put(TAG_PROFILE, user_profile);
                        map.put(TAG_IMAGE, user_image);
                        map.put(TAG_CAST, user_cast);
                        map.put(TAG_AGE, user_age+" years");
                        map.put(TAG_LOCATION, user_location);



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


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

            return null;
        }


        protected void onPostExecute(String file_url) {

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

                    /**
                     * Updating parsed JSON data into ListView
                     * */
            CustomAdapter adapter = new CustomAdapter(getActivity(),aList);
            setListAdapter(adapter);

                }

        }




}

this is working fine now in next activity i want to get full details as per second json.

in below activity i want to get users detail what should i do?

public class ProfilePage extends Activity{

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

    cover=(ImageView)findViewById(R.id.coverimage);
    yes=(ImageView)findViewById(R.id.yesbutton);
    no=(ImageView)findViewById(R.id.nobutton);
    sendmsg=(ImageView)findViewById(R.id.imgsendmsgg);
    sendmsg.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View arg0) 
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(ProfilePage.this);
            builder.setTitle(R.string.title_alertbox_upgrade)
                    .setIcon(R.drawable.ic_launcher)
                   .setMessage(R.string.chek_upgrade)
                   .setCancelable(true)
                   .setNegativeButton(R.string.okalert_upgrade, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                       }
                   });

            AlertDialog welcomeAlert = builder.create();
            welcomeAlert.show();
            // Make the textview clickable. Must be called after show()
            ((TextView)welcomeAlert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
            /*// Creating alert Dialog with one Button

            AlertDialog alertDialog = new AlertDialog.Builder(
                    ProfileEdit.this).create();

            // Setting Dialog Title
            alertDialog.setTitle("Edit Profile");

            // Setting Dialog Message
            alertDialog.setMessage("Please log on to gujjumatch.com desktop site to edit your profile " +
                    "and also set other details or call on 91 281 3054120");

            // Setting Icon to Dialog
            alertDialog.setIcon(R.drawable.ic_launcher);

            // Setting OK Button
            alertDialog.setButton("OK",
                    new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,
                                int which) {
                            // Write your code here to execute after dialog
                            // closed
                            Toast.makeText(getApplicationContext(),
                                    "Thank You", Toast.LENGTH_SHORT)
                                    .show();
                        }
                    });

            // Showing Alert Message
            alertDialog.show();*/


        }
    });
    yes.setOnClickListener(new OnClickListener() 

    {

        @Override
        public void onClick(View arg0) {

            LayoutInflater inflater = getLayoutInflater();


            View layout = inflater.inflate(R.layout.custom_toast,
                      (ViewGroup) findViewById(R.id.custom_toast_layout_id));

            ImageView image = (ImageView) layout.findViewById(R.id.image);
            image.setImageResource(R.drawable.yes);

            // set a message
            TextView text = (TextView) layout.findViewById(R.id.text);
            text.setText("Interest Sent");



            // Toast...
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();

        }
    });

    no.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            LayoutInflater inflater = getLayoutInflater();


            View layout = inflater.inflate(R.layout.custom_toast,
                      (ViewGroup) findViewById(R.id.custom_toast_layout_id));

            ImageView image = (ImageView) layout.findViewById(R.id.image1);
            image.setImageResource(R.drawable.no);

            // set a message
            TextView text = (TextView) layout.findViewById(R.id.text1);
            text.setText("Interest Declined");



            // Toast...
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();

        }
    });
    cover.setOnClickListener(new OnClickListener() {
                    @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            // Sending image id to FullScreenActivity
               Intent i = new Intent(getApplicationContext(), Fullimage.class);
               // passing array index


               startActivity(i);

        }
    });
}

 }

解决方案

This is your ListView within fragment class.

I assume that you can create ListView with ease ...

Now on click of ListView you need to pass data like this ..

lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
        Intent intent = new Intent(getActivity(), UserBrandsFragmentDetail.class);
        intent.putExtra("brand_logo", m_ArrayList.get(arg2).brand_logo); 
        startActivity(intent);
    }
}

UPDATE

You require to have ListView. Then on click of ListView you need to use my above mentioned code ...

In my above mentioned code "lv" is ListView. :)

On your next activity you need to get that data using bundle ...like this.

Bundle extra = getIntent().getExtras();
String your_data = extra.getString("your_key_while_parsing_data_through_Intent");

That's it .. you are good to go!!

For creating list view you can Google it. There's lot of tutorial available for that.

Let me know if you still have any sort of query ..

这篇关于如何从片段发送ID到活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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