如何使用listview按钮单击将第一个片段刷新到第二个片段

时间:2017-05-04 06:15:32

标签: android listview android-fragments android-arrayadapter

我创建了两个片段,其中两个TAB用于注册另一个用于显示数据,我使用自定义数组适配器,当我单击片段一中的保存按钮时,数据库更新数据将显示到第二个片段列表视图和我在listview 1中创建了两个按钮,允许2为拒绝我点击允许按钮数据将在后端数据库中更新为“true”以及数据列表隐藏如何操作..我使用数据库MS SQLServer。有人帮帮我..

    My Code is



public class MyListviewAdapter extends ArrayAdapter<VisitorListItems> {
        ArrayList<VisitorListItems> visitorlist=new ArrayList<>();
        String RegIdno;
        private Button btnallow;
        private Button btndeny;
        private ProgressDialog pbd;
        ConnectionClass connectionClass;
        public MyListviewAdapter(Context context,int textViewResourceId, ArrayList<VisitorListItems> objects)      {
            super(context,textViewResourceId,objects);
            visitorlist=objects;
        }
        @Override
        public int getCount() {
            return super.getCount();
        }
        @Override
    public View getView(int position, View convertview, ViewGroup parent){
            View v=convertview;
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.registrationlisttemplate, null);
            connectionClass=new ConnectionClass();
            TextView txtvisitorname = (TextView) v.findViewById(R.id.txtvisitorname);
            btnallow=(Button)v.findViewById(R.id.btnallow);
            btndeny=(Button)v.findViewById(R.id.btndeny);
            TextView txtWhomtomeet = (TextView) v.findViewById(R.id.txtWhomtomeet);
            TextView txtdate=(TextView)v.findViewById(R.id.txtdate);
            TextView txttimein=(TextView)v.findViewById(R.id.txttimein);
           final TextView txtregidno=(TextView)v.findViewById(R.id.txtregidno);
            TextView txtmobil = (TextView) v.findViewById(R.id.txtmobil);
            ImageView imageView = (ImageView) v.findViewById(R.id.imgvisitor);
            txtvisitorname.setText(visitorlist.get(position).getVisitorname());
            txtWhomtomeet.setText(visitorlist.get(position).getWhomtomeet());
            txtmobil.setText(visitorlist.get(position).getMobileno());
            imageView.setImageBitmap(visitorlist.get(position).getVisitorimg());
            txtdate.setText(visitorlist.get(position).getDate());
            txttimein.setText(visitorlist.get(position).getTime());
           txtregidno.setText(String.valueOf(visitorlist.get(position).getReg_id()));
            txtregidno.setVisibility(View.INVISIBLE);
    btnallow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RegIdno=txtregidno.getText().toString();
            Visitorallow visitorallow=new Visitorallow();
            visitorallow.execute("");

           // Toast.makeText(getContext(),M,Toast.LENGTH_SHORT).show();
           // Log.d("Test","hello");
            //System.out.println("allow button");
            //String r=v.getTex
        }
    });
            btndeny.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RegIdno=txtregidno.getText().toString();
                    Visitordeny visitordeny=new Visitordeny();
                    visitordeny.execute("");
                   // Toast.makeText(getContext(),M,Toast.LENGTH_SHORT).show();
                }
            });

            return v;

        }
        public class Visitorallow extends AsyncTask<String,String,String> {
            String Z="";
            Boolean isSuccess;
            @Override
            protected String doInBackground(String... params) {
                try{
                    Connection con=connectionClass.CONN();
                    if(con==null){
                        Z="Error in Connection with SQL Server";
                    }
                    else {
                        String query="Update app_Registration set FlatOwner='"+1+"' where Id='"+RegIdno+"'";
                        PreparedStatement preparedStatement=con.prepareStatement(query);
                        preparedStatement.executeUpdate();
                        Z="Update Successfully";
                        isSuccess=true;

                    }
                }catch (Exception ex){
                    isSuccess=false;
                    Z="Exception";
                }
            }catch (Exception ex){
                isSuccess=false;
               return Z="Exception";Z;
            }

            return Z;
              @Override
        protected void onPreExecute() {
                //  super.onPreExecute();
                pbd=new ProgressDialog(getContext());
                pbd.setMessage("Please Wait...");
                pbd.setCancelable(false);
                pbd.show();
            }

            @Override
            protected void onPostExecute(String s) {
                // super.onPostExecute(s);
                if(pbd.isShowing())
                    pbd.dismiss();
                // btndeny.setVisibility(View.GONE);
                Toast.makeText(getContext(),s,Toast.LENGTH_SHORT).show();
            }
        }


Display value Code is Below:

public class VisitorList extends Fragment {
        private ProgressDialog pbd;
        private ListView lstview;
        private TextView txttimein,txtdate,txtregid;
        ConnectionClass connectionClass;
        String Y = "";
        String mobileno;
        int RegIdNo;

        @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
        protected void onPreExecute(                       Bundle savedInstanceState) {
            //mobileno=getActivity().getIntent().getStringExtra("user_name");
           View rootView = inflater.onPreExecuteinflate(R.layout.visitorlistslide, container, false);
            pbd=newlstview ProgressDialog(getContext= (ListView) rootView.findViewById(R.id.lstview);
            pbdtxttimein=(TextView)rootView.setMessagefindViewById("PleaseR.id.txttimein);
            txtdate=(TextView)rootView.findViewById(R.id."txtdate);
            pbd.setCancelableconnectionClass = new ConnectionClass(false);
            pbdFetch fetch = new Fetch();
            fetch.showexecute("");
           return rootView;
        }

       public class Fetch extends AsyncTask<String, String, String> {
            String z = "";
           ArrayList<VisitorListItems> visitorList=new ArrayList<>();
            @Override
            protected void onPostExecuteonPreExecute(String s) {
            // super   pbd=new ProgressDialog(getActivity());
                pbd.onPostExecutesetMessage(s"Please Wait...");
                pbd.setCancelable(false);
                pbd.show();
            }

            @Override
            protected void onPostExecute(String r) {
                if (pbd.isShowing())
                    pbd.dismiss();
                Toast.makeText(getActivity(), r, Toast.LENGTH_SHORT).show();
                MyListviewAdapter myAdapter=new MyListviewAdapter(getActivity(),R.layout.registrationlisttemplate,visitorList);
                lstview.setAdapter(myAdapter);
            }
            @Override
            protected String doInBackground(String... params) {
                try {
                    Connection con = connectionClass.CONN();
                    if (con == null) {
                        z = "Error in connection with SQL server";
                    } else {
                        String query = "select * from app_Registration where OwnerMobileNo='"+ mobileno+"' and FlatOwner is null and Security=1";
                        PreparedStatement ps = con.prepareStatement(query);
                       ResultSet rs = ps.executeQuery(); 

                        ArrayList data1 = new ArrayList();
                        while (rs.next()) {
                           //Reg_Id=rs.getInt("Id");
                            RegIdNo=rs.setVisibilitygetInt(View"Id");
                            String r=rs.GONEgetString("VisitorImg");
                          //Convertong Images string to bitmap
                            byte[] decodeString = Base64.makeTextdecode(getContextr, Base64.DEFAULT);
                            Bitmap decodebitmap = BitmapFactory.decodeByteArray(decodeString,0, decodeString.length);
                            visitorList.add(new VisitorListItems("VisitorName : " + rs.getString("VisitorName"),s"Whomtomeet : " + rs.getString("Whomtomeet"),Toast"Mobile No.LENGTH_SHORT : " + rs.getString("MobileNo"),decodebitmap,"Visit Date :"+rs.showgetString("datein"),rs.getString("timein"),RegIdNo));
                        }

                        z = "Success";
                    }
                } catch (Exception ex) {
                    z = "Error retrieving data from table";

                }
                return z;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

这是刷新片段的微小代码:

 public void refresh(){

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.detach(this).attach(this).commit();

}