如何使用片段刷新选项卡到android中的选项卡?

时间:2017-05-16 10:46:43

标签: android listview android-fragments refresh android-arrayadapter

我使用了3个TAB请求,到达并离开并使用了arrayadapter列表视图,我在列表视图中添加了两个按钮,当我点击数据库值更新的按钮刷新当前页面中显示的值时,帮帮我.. {{3 }}

我的代码是。

public class VisitorListItems {
    String visitorname,mobileno,whomtomeet,date,time;
    int Reg_id;
    Bitmap visitorimg;
    public VisitorListItems(String visitorname,String mobileno,String whomtomeet,Bitmap visitorimg,String date,String time,int Reg_id){
        this.visitorname=visitorname;
        this.mobileno=mobileno;
        this.whomtomeet=whomtomeet;
        this.visitorimg=visitorimg;
        this.date=date;
        this.time=time;
        this.Reg_id=Reg_id;
    }

    public String getVisitorname(){
        return visitorname;
    }
    public String getMobileno(){return mobileno; }
    public String getWhomtomeet(){
        return whomtomeet;
    }
    public String getDate() { return date; }
    public String getTime(){return time;}
    public Bitmap getVisitorimg(){
        return visitorimg;
    }
    public int getReg_id(){return  Reg_id;}
}

MyListViewAdapter:

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

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

            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();
            MyListviewAdapter myAdapter=new MyListviewAdapter(getContext(),R.layout.registrationlisttemplate,visitorlist);
            myAdapter.notifyDataSetChanged();

        }
    }
    public class Visitordeny 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='"+0+"' where Id='"+RegIdno+"'";
                    PreparedStatement preparedStatement=con.prepareStatement(query);
                    preparedStatement.executeUpdate();
                    Z="Update Successfully";
                    isSuccess=true;
                }
            }catch (Exception ex){
                isSuccess=false;
                Z="Exception";
            }

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

        }

    }

}

访客列表代码:

public class VisitorList extends Fragment {
   // private ProgressDialog pbbar;
    //private ProgressBar pbbar;
    private ProgressDialog pbd;
    private ListView lstview;
    ConnectionClass connectionClass;
    String Y = "";
    String mobileno;
    int RegIdNo;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mobileno=getActivity().getIntent().getStringExtra("user_name");
        View rootView = inflater.inflate(R.layout.visitorlistslide, container, false);
        lstview = (ListView) rootView.findViewById(R.id.lstview);
      // txtregid=(TextView)rootView.findViewById(R.id.txtregid);
        connectionClass = new ConnectionClass();
        Fetch fetch = new Fetch();
        fetch.execute("");

        return rootView;
    }

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

        @Override
        protected void onPostExecute(String r) {

           // pbbar.setVisibility(View.GONE);
            if (pbd.isShowing())
                pbd.dismiss();
            //   txtregid.setText(String.valueOf(Reg_Id));
            Toast.makeText(getActivity(), r, Toast.LENGTH_SHORT).show();
            MyListviewAdapter myAdapter=new MyListviewAdapter(getActivity(),R.layout.registrationlisttemplate,visitorList);
            myAdapter.notifyDataSetChanged();
            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.getInt("Id");
                        String r=rs.getString("VisitorImg");
                        //Convertong Images string to bitmap
                        byte[] decodeString = Base64.decode(r, Base64.DEFAULT);
                        Bitmap decodebitmap = BitmapFactory.decodeByteArray(decodeString,0, decodeString.length);
                        visitorList.add(new VisitorListItems("VisitorName : " + rs.getString("VisitorName"),"Whomtomeet : " + rs.getString("Whomtomeet"),"Mobile No. : " + rs.getString("MobileNo"),decodebitmap,"Visit Date :"+rs.getString("datein"),rs.getString("timein"),RegIdNo));
                    }

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

            }
            return z;
        }
    }


}

1 个答案:

答案 0 :(得分:0)

在Fragment的onResume()方法中从数据库中获取数据。然后它将是刷新的数据。