想要在项目点击更改后刷新列表视图

时间:2014-01-29 13:02:53

标签: java android

我正在使用从数据库中获取数据的ListView。 当我呼叫更新和删除时,我通过呼叫Intent刷新活动。 它显示更新列表,但是当我点击它时,它不会显示文本视图中的更改,我将在其中显示列表详细信息。 我正在使用Custom Adapter.Below是我的代码,

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


}

1 个答案:

答案 0 :(得分:0)

由于您未提供适配器的代码,我不清楚您的问题。我认为你没有在Adapter实例上调用notifyDataSetChanged()方法来反映ListView上的更改。