如何在自定义列表视图中更新,删除记录

时间:2013-10-10 15:13:32

标签: android

我想在下一个Activity中更改自定义数据列表。请帮我。我试过了notifysetdatachanged(),但我的名单仍然相同。我想更新我的列表数据。

  1. 首先我保存数据并将其显示在列表中
  2. 然后在listActivity中我使用意图将数据传递到下一个Activity
  3. 然后在下一个活动中使用更新方法
  4. 现在点击按钮我想根据下一个活动的更改
  5. 更新列表

    自定义适配器的代码在这里:

    public class CustomAdapter extends ArrayAdapter<Newd> {
        Context context; 
        int layoutResourceId;    
        static Newd data[];
        public BookHolder holder = null;
        public CustomAdapter(Context context, int textViewResourceId,Newd[] objects) {
            super(context, textViewResourceId, objects);
            this.layoutResourceId = textViewResourceId;
            this.context = context;
            CustomAdapter.data = objects;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            //BookHolder holder = null;
    
            if(row == null) {
                LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);
    
                holder = new BookHolder();
                holder.name2= (TextView)row.findViewById(R.id.cname);
                //holder.eml2 = (TextView)row.findViewById(R.id.eml1);
                holder.phn2 = (TextView)row.findViewById(R.id.cphno);
    
                row.setTag(holder);
            }
            else {
                holder = (BookHolder)row.getTag();
            }
    
            Newd book_detail = data[position];
    
            holder.name2.setText(book_detail.name);
            //holder.eml2.setText(book_detail.email);
            holder.phn2.setText(String.valueOf(book_detail.phno));
            System.out.println("name " + book_detail.name + "......." + book_detail.getName() + "name...." + "email " + "phoo" + book_detail.phno );
    
            return row;
        }
    
        static class BookHolder {
    
            TextView name2;
            //TextView eml2;
            TextView phn2;
        }
    }
    

    列表活动:

    public class Showdata extends Activity {
        ListView lv;
        String nms,emlss;
        int phns,id11,id1;
        Newd data1[]=null;
        DataHelper dh;
        SharedPreferences pref;
        ArrayList<Newd>data=new ArrayList<Newd>();
        public ArrayList<Integer>phone = new ArrayList<Integer>();
        public ArrayList<String> name = new ArrayList<String>();
        public ArrayList<String> email = new ArrayList<String>();
        public ArrayList<Integer> userid = new ArrayList<Integer>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.showdata);
    
            dh=new DataHelper(getApplicationContext());
            pref = getSharedPreferences("UID",
                                        Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putInt("ID", id1);
            editor.commit();
            System.out.println("id in intent  "  +id1);
    
            //System.out.println("idssss"   +id1);
            dh.open();
            data=dh.getdata();
            dh.close();
    
            System.out.println("data .." +data);
    
            if(data.isEmpty()) {
    
            }
    
            else {
                for (int i = 0; i < data.size(); i++) {
                    Newd r = data.get(i);
                    id11=r.getId();
                    nms=r.getName();
                    //emlss=r.getEmail();
                    phns= r.getPhno();
                    //total=r.getSeats();
                    System.out.println("list of booking dates  ........" + nms+ "..........u iddd" +phns  +"srno" +emlss);
    
                    userid.add(id11);
                    name.add(nms);
                    email.add(emlss);
                    phone.add(phns);
                    System.out.println("list of booking dates1  ........" + nms + "..........u iddd" + phns + "srno" + emlss);
                }
    
                data1=new Newd[name.size()];
    
                for (int i = 0; i < name.size(); i++) {
                    // srn=srno.get(i);
                    int id2=userid.get(i);
    
                    String nm1=name.get(i);
                    String eml1=email.get(i);
                    int phn1=phone.get(i);
    
                    data1[i]=new Newd(id2,nm1, phn1);
                    System.out.println("srno...???"  +id2);
                }
            }
    
            lv=(ListView) findViewById(R.id.slist);
    
            final CustomAdapter adapter=new CustomAdapter(Showdata.this, R.layout.custm, data1);
            //lv.add
            lv.setAdapter(adapter);
            System.gc();
            adapter.notifyDataSetChanged();
            lv.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> parent, View v, int position,
                        long id) {
                    // TODO Auto-generated method stub
                    //Cursor    cr= (Cursor) parent.getItemAtPosition(position);
                    // updateList();
    
                    TextView t1=(TextView) findViewById(R.id.cname);
                    TextView t2=(TextView) findViewById(R.id.cphno);
                    // v=(View)parent.getItemAtPosition(position);
                    String unam=t1.getText().toString();
    
                    int phnu=Integer.valueOf(t2.getText().toString());
    
                    Intent it=new Intent(Showdata.this,UpdateData.class);
                    it.putExtra("unam",unam);
                    it.putExtra("phnu", phnu);
                    System.out.println("namesssss"  +unam  + "ph"   +phnu);
                    startActivity(it);  
                    //    lv.getAdapter().noti  
                }
            }); 
        }
    }
    

    下一个活动:

    public class UpdateData extends Activity {
    
        Button update,delete;
        EditText uname,uphn;
        SharedPreferences pref;
        DataHelper dh;
        Cursor cr;
        int idd;
        CustomAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.updatedata);
            update=(Button) findViewById(R.id.usubmit);
            delete=(Button) findViewById(R.id.uBack);
            uname=(EditText) findViewById(R.id.uname);
            uphn=(EditText) findViewById(R.id.uphono);
            pref = getSharedPreferences("UID",
                                        Context.MODE_PRIVATE);
            idd=pref.getInt("ID", 0);
            dh=new DataHelper(getApplicationContext());
            Intent it=getIntent();
            uname.setText(it.getStringExtra("unam"));
            uphn.setText(String.valueOf(it.getIntExtra("phnu", 0)));
            //    System.out.println("getintent"  +name);
            update.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String unames=uname.getText().toString();
                    int uphns=Integer.valueOf(uphn.getText().toString());
                    dh.open();
                    dh.updateValues(idd,unames, uphns);
                    dh.close();
                    //updateList();
    
                    System.out.println("updateddd"  +unames  +";;;;;;;;;;;;" +uphns);
                    Intent up=new Intent(UpdateData.this,Showdata.class);
                    startActivity(up);
                    //adapter.notifyDataSetChanged();
                }
            });
        }
    }
    

0 个答案:

没有答案