onbackpress listview变为null

时间:2016-01-12 05:07:31

标签: android listview android-intent

我将WishFriendsFamily的意向发送给WishFamily会员,在WishFriendsFamily我有listview我正在获取数据,直到这里工作正常,然后我能够想要WishFamilyMember,并在我的WishFamily会员活动中有一个按钮可以回到WishFriendsFamily,每当我回到WishFriendsFamily的listview时就变为空

WishFriendsFAmily

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wish_friends_family);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


        backaro=(ImageView)findViewById(R.id.wishfrndsfamily_backarrow);

        backaro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(WishFriendsFamily.this,MainActivity.class);
                startActivity(intent);
            }
        });
        //hideKeyboard(getApplicationContext());
        aQuery = new AQuery(getApplicationContext());
        frndsname=(TextView)findViewById(R.id.wishfriendfamily_name);
        frndsimgs=(ImageView)findViewById(R.id.wishfriendfamily_propic);

        listviw=(ListView)findViewById(R.id.list_frndsfamily);

        selecteperson=(TextView)findViewById(R.id.selected_family_member);

        sendgift=(ImageView)findViewById(R.id.sendgift);
       // Bundle bundle = this.getArguments();
        if( getIntent().getExtras() != null) {
            Intent intent = getIntent();
            pname = intent.getStringExtra("friendname");
            pimg = intent.getStringExtra("friendimage");
            pid = intent.getStringExtra("frienid");
            useridss = intent.getStringExtra("userloginid");
        }
        else
        {


            System.out.println("asdfasf");
        }


            INTEREST_ACCEPT_URL = "http:///friend_details.php?user_id="+useridss+"&friend_id="+pid;




        System.out.println("asdfsa" + pname + pimg + pid + useridss);
        frndsname.setText(pname);
        aQuery.id(frndsimgs).image(pimg, true, true, 0, R.drawable.male);



        if(pname==null && pimg==null && pid==null && useridss==null)
        {
            System.out.println("Tari");
        }
        else
        {
            new LoadAllStates().execute();
        }






    }

    private class CustomAdapterGiftsharealertfamily extends BaseAdapter {
        //  String [] result;
        Context context;
        // int [] imageId;
        private ArrayList<String> listData;
        private LayoutInflater inflater=null;
        public CustomAdapterGiftsharealertfamily(Context mainActivity, ArrayList<String> listData) {
            // TODO Auto-generated constructor stub
            context=mainActivity;
            this.listData=listData;

            inflater = ( LayoutInflater )context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listData.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public class Holder
        {
            TextView tv;

            public TextView tvbday;

            public TextView tvrelation;
            public LinearLayout linr;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final Holder holder=new Holder();
            View rowView;
            rowView = inflater.inflate(R.layout.list_item_frndsfmly, null);

            holder.linr=(LinearLayout)rowView.findViewById(R.id.linear_listitem_frndsmly);
            holder.tv=(TextView) rowView.findViewById(R.id.frndsfamly_name);
            holder.tvbday=(TextView) rowView.findViewById(R.id.frndsfamly_bady);
            holder.tvrelation=(TextView) rowView.findViewById(R.id.frndsfamly_realtion);
            holder.tvbday=(TextView) rowView.findViewById(R.id.frndsfamly_bady);
            holder.tv.setText(listData.get(position));
            holder.tvbday.setText(bdatedata.get(position));


            //  holder.tvrelation.setText(testlist.get(position));

            if(testlist.get(position).equals("spoc")) {
                holder.tvrelation.setText(" | "+"Wife");
            }

            else if(testlist.get(position).equals("ch"))
            {
                holder.tvrelation.setText(" | "+"Child 1");
            }
            else if(testlist.get(position).equals("ch1"))
            {
                holder.tvrelation.setText(" | "+"Child 2");
            }
            else if(testlist.get(position).equals("mom"))
            {
                holder.tvrelation.setText(" | "+"Mother");
            }
            else if(testlist.get(position).equals("dad"))
            {
                holder.tvrelation.setText(" | "+"Father");
            }
            else if(testlist.get(position).equals("spl"))
            {
                holder.tvrelation.setText(" | "+"Mine");
            }

            return rowView;
        }

    }
    class LoadAllStates extends
            AsyncTask<String, String, ArrayList<String>> {
        private ProgressDialog pDialog;
        private String test;
        private String ids;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(WishFriendsFamily.this);
            pDialog.setMessage("Please wait..");
            pDialog.setIndeterminate(true);
            pDialog.setCancelable(true);
            pDialog.show();


        }

        protected ArrayList<String> doInBackground(
                String... args) {
            ServiceHandler sh = new ServiceHandler();
            // Making a request to url and getting response
            statedata = new ArrayList<String>();
            testlist=new ArrayList<String>();
            String jsonStr = sh.makeServiceCall(INTEREST_ACCEPT_URL, ServiceHandler.GET);

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

            Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null) {
                try {
                    jsonObj = new JSONArray(jsonStr);
                    for (int i = 0; i < jsonObj.length(); i++) {
                        c = jsonObj.getJSONObject(i);
                        wifebday= (c.has("user_spoc_dob")) ? c.getString("user_spoc_dob") : null;
                        wifename= (c.has("spouse_name")) ? c.getString("spouse_name") : null;

                        childonebday= (c.has("user_child_dob")) ? c.getString("user_child_dob") : null;
                        childonename= (c.has("child_name")) ? c.getString("child_name") : null;

                        childtwobday= (c.has("user_child_dob1")) ? c.getString("user_child_dob1") : null;
                        childtwoname= (c.has("child1_name")) ? c.getString("child1_name") : null;


                        mothersbday= (c.has("user_mother_dob")) ? c.getString("user_mother_dob") : null;
                        mothersname= (c.has("mother_name")) ? c.getString("mother_name") : null;


                        fatherbday= (c.has("user_father_dob")) ? c.getString("user_father_dob") : null;
                        fathername= (c.has("father_name")) ? c.getString("father_name") : null;
                        ownbbday= (c.has("user_dob")) ? c.getString("user_dob") : null;
                        ownname= (c.has("user_fname")) ? c.getString("user_fname") : null;
                        occs= (c.has("occ")) ? c.getString("occ") : null;
                        ids= (c.has("friend_id")) ? c.getString("friend_id") : null;

                        // if test, to avoid adding null values
                        if(wifename!=null) statedata.add(wifename);
                        if(childonename!=null) statedata.add(childonename);
                        if(childtwoname!=null) statedata.add(childtwoname);
                        if(mothersname!=null) statedata.add(mothersname);
                        if(fathername!=null) statedata.add(fathername);
                        if(ownname!=null) statedata.add(ownname);


                        if(wifebday!=null) bdatedata.add(wifebday);
                        if(childonename!=null) bdatedata.add(childonename);
                        if(childtwoname!=null) bdatedata.add(childtwoname);
                        if(mothersbday!=null) bdatedata.add(mothersbday);
                        if(fatherbday!=null) bdatedata.add(fatherbday);
                        if(ownbbday!=null) bdatedata.add(ownbbday);
                        if(occs!=null) testlist.add(occs);
                    }
                    if(c.getString("occ").toString().equals("spoc"))
                    {
                        userwife=" | "+"Wife";
                    }
                    else if(c.getString("occ").toString().equals("ch"))
                    {
                        userchone=" | "+"Child 1";
                    }
                    else if(c.getString("occ").toString().equals("ch1"))
                    {
                        userchtwo=" | "+"Child 2";
                    }
                    else if(c.getString("occ").toString().equals("mom"))
                    {
                        usermom=" | "+"Mother";
                    }
                    else if(c.getString("occ").toString().equals("dad"))
                    {
                        userdaddy=" | "+"Father";;
                    }
                    else if(c.getString("occ").toString().equals("spl"))
                    {
                        userown=" | "+"Mine";
                    }
                    System.out.println("Bday" + userwife + usermom + userdaddy + userown+userchone+userchtwo);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
            return statedata;
        }
        protected void onPostExecute(ArrayList<String> result) {
            super.onPostExecute(result);

            if (pDialog.isShowing())
                pDialog.dismiss();
            aList = new ArrayList<String>();
            aList.addAll(result);
            adapter = new CustomAdapterGiftsharealertfamily(WishFriendsFamily.this, result);
            listviw.setAdapter(adapter);
            adapter.notifyDataSetChanged();


            listviw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    userid=ids.toString();
                    userocc=testlist.get(i).toString();
                    System.out.println("USER ID"+userid);
                    System.out.println("USER OCC" + userocc);


                    try {
                        if(c.getString("occ").toString().equals("spoc"))
                        {

                            selecteperson.setText("You selected "+statedata.get(i));
                            selperson=statedata.get(i);

                        }
                        else if(c.getString("occ").toString().equals("mom"))
                        {
                            selecteperson.setText("You selected "+statedata.get(i));
                            selperson=statedata.get(i);

                        }
                        else if(c.getString("occ").toString().equals("dad"))
                        {
                            selecteperson.setText("You selected "+statedata.get(i));
                            selperson=statedata.get(i);
                        }
                        else if(c.getString("occ").toString().equals("spl"))
                        {
                            selecteperson.setText("You selected "+statedata.get(i));
                            selperson=statedata.get(i);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });

            sendgift.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


                   // Toast.makeText(getApplicationContext(),selperson,Toast.LENGTH_SHORT).show();

                    Intent intent=new Intent(WishFriendsFamily.this,WishFamilyMember.class);

                    intent.putExtra("selname",selperson);
                    intent.putExtra("userloginids", useridss);
                    intent.putExtra("frndsid", userid);
                    intent.putExtra("names", pname);
                    intent.putExtra("images", pimg);
                    intent.putExtra("occasion", userocc);

                    intent.putExtra("pids", pid);



                    startActivity(intent);

                }
            });

        }

    }



}

WishFamilyMember

backaro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WishFamilyMember.this, WishFriendsFamily.class);
                startActivity(intent);
            }
        });

1 个答案:

答案 0 :(得分:1)

backaro.setOnClickListener中的WishFamilyMember应如下:

backaro.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {   
 Intent intent = new Intent(WishFamilyMember.this, WishFriendsFamily.class);
 intent.putExtra("friendname",friendname );
 intent.putExtra("friendimage",friendimage );
 intent.putExtra("frienid",frienid );
 intent.putExtra("userloginid",userloginid );

            startActivity(intent);

  }
    });

或者你可以调用onBackPressed();而不是像这样发送Intent

backaro.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {   
        onBackPressed();
   }
    });

我希望我能正常工作。