简体   繁体   中英

Want to Refresh List View after change on item click

I am using ListView that get data from Database. when i call update and delete i refresh the activity by call Intent . it shows updated list but when i click on it it not shows changes in text view on which i am showing list details. i am using Custom Adapter.Below is my code,

public class Mainmenu extends Activity{
    ListView list;
    DBHandler db= new DBHandler(this);
    int ID;
    String strname,strlastname,strdob,strcell,strcnic,straddress;
    String pppath,cfpath,cbpath,fppath;
    TextView tvname,tvlastname,tvdob,tvcnic,tvcell,tvaddress;
    ImageView pp,cf,cb;
    public static ArrayList<String> customers = new ArrayList<String>();
    SearchResults sr1;
      ArrayList<SearchResults> searchResults= new ArrayList<SearchResults>();
      MyCustomBaseAdapter adaptor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenulist);
        list= (ListView) findViewById(R.id.listMM);
        tvname=(TextView)findViewById(R.id.textView3);
        tvlastname=(TextView)findViewById(R.id.textView4);
        tvdob=(TextView)findViewById(R.id.textView5);
        tvcnic=(TextView)findViewById(R.id.textView10);
        tvcell=(TextView)findViewById(R.id.textView7);
        tvaddress=(TextView)findViewById(R.id.textView8);
        pp=(ImageView)findViewById(R.id.ImageView02);
        cf=(ImageView)findViewById(R.id.imageView2);
        cb=(ImageView)findViewById(R.id.ImageView03);

        Context c =getApplicationContext();


        if(db.getAllcustomers2().toString()==null){
            db.getWritableDatabase();
            db.init();
        }else{
            List<Customers_GS> gtEdu = db.getAllcustomers2();
            customers.clear();

              for (Customers_GS ed : gtEdu) {

                 sr1 = new SearchResults();
                 ID=ed.getCID();
                 sr1.setId(ID);  
                  customers.add(ed.getCustomer_Name());
                  strname=ed.getCustomer_Name().toString();
                  sr1.setName(strname);
                  customers.add(ed.getCustomer_DOB());
                  strdob=ed.getCustomer_DOB().toString();
                  sr1.setDOB(strdob);
                  customers.add(ed.getCustomer_CNIC());
                  strcnic=ed.getCustomer_CNIC().toString();
                  sr1.setCNIC(strcnic);
                  customers.add(ed.getCustomer_Cell());
                  strcell=ed.getCustomer_Cell().toString();
                  sr1.setPhone(strcell);
                  customers.add(straddress);
                  straddress=ed.getCustomer_Address().toString();
                  sr1.setAddress(straddress);
                  customers.add(ed.getCustomer_PP());
                  pppath=ed.getCustomer_PP().toString();
                  customers.add(ed.getCustomer_CNICf());
                  cfpath=ed.getCustomer_CNICf().toString();
                  customers.add(ed.getCustomer_CNICb());
                  cbpath=ed.getCustomer_CNICb().toString();
                  customers.add(ed.getCustomer_Thumb());
                  sr1.setImageNumber(1);
                  searchResults.add(sr1);
             }
        }


        adaptor= new MyCustomBaseAdapter(c, searchResults);
        list.setAdapter(adaptor);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                int height= 146;
                int width=146;
            //parent.getItemAtPosition(position);
//
                Log.d("PP", ""+pppath);
                Log.d("cP", ""+cbpath);
                Log.d("fP", ""+cfpath);

//              Bitmap ppbitmap = BitmapFactory.decodeFile(pppath );
//              Bitmap cbbitmap = BitmapFactory.decodeFile(cbpath );
//              Bitmap cfbitmap = BitmapFactory.decodeFile(cfpath );
//              ppbitmap=Bitmap.createScaledBitmap(ppbitmap, width,height, true);
//              cbbitmap=Bitmap.createScaledBitmap(cbbitmap, width,height, true);
//              cfbitmap=Bitmap.createScaledBitmap(cfbitmap, width,height, true);
//              
//              pp.setImageBitmap(ppbitmap);
//              cb.setImageBitmap(cbbitmap);
//              cf.setImageBitmap(cfbitmap);

                String[] result =strname.split(" ");
                strname= result[0];
                strlastname=result[1];

                tvname.setText(strname);
                tvlastname.setText(strlastname);
                tvcell.setText(strcell);
                tvaddress.setText(straddress);
                tvcnic.setText(strcnic);
                tvdob.setText(strdob);
                adaptor.notifyDataSetChanged();

            }

        }); 


        list.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int pos, long id) {
//                // TODO Auto-generated method stub
//              
//
                db.getWritableDatabase();
//              db.delete(ID);
//              db.update(17);
//              // TODO Auto-generated method stub
//
//              Intent gonext= new Intent("com.example.ibex.MAINMENU");
//              startActivity(gonext);
//
                  AlertDialog.Builder builder = new AlertDialog.Builder(Mainmenu.this);
                  builder.setMessage("Chose any one!");
                  builder.setCancelable(false);
                  builder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {

                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         db.update(ID);
                         Intent reload = new Intent(getApplicationContext(), Mainmenu.class);
                         startActivity(reload);

                     }
                 });

                 builder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {

                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                             db.delete(ID);
                             Intent reload = new Intent(getApplicationContext(), Mainmenu.class);
                             startActivity(reload);


                     }
                 });

                 AlertDialog alert = builder.create();
                 alert.show();

                return true;
            }
        }); 


    }


    ////### ##  ##### ### ### ### ### #### ### ### ### ### ### ## ////
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.optionmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==R.id.Addmenu){
            Intent openstngs=new Intent("com.example.ibex.REGISTRATION");
            startActivity(openstngs);
        }else if(item.getItemId()==R.id.Edithmenu){
            Bundle contan= new Bundle();
            contan.putInt("key", ID );
            Intent web =new Intent(Mainmenu.this, Edit.class);
            web.putExtras(contan);
            startActivity(web);

        }else if(item.getItemId()==R.id.Dltmenu){
            db.delete(ID);
            Intent reload = new Intent(getApplicationContext(), Mainmenu.class);
            startActivity(reload);

        }else{

            AlertDialog.Builder builder = new AlertDialog.Builder(Mainmenu.this);
            builder.setMessage("Are u sure to want to Logout ?");
            builder.setCancelable(false);
            builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {

               @Override
               public void onClick(DialogInterface dialog, int which) {
                   Intent reload = new Intent(getApplicationContext(), Login.class);
                   startActivity(reload);

               }
           });

           builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

               @Override
               public void onClick(DialogInterface dialog, int which) {
                       dialog.cancel();


               }
           });

           AlertDialog alert = builder.create();
           alert.show();



        }
        return super.onOptionsItemSelected(item);
    };


}

I'm not clear about your question since you haven't provided the code of your adapter. I think You haven't called notifyDataSetChanged() method on your Adapter instance to get your changes reflected on ListView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM