如何更新复选框项位置

时间:2013-12-18 12:57:57

标签: android checkbox

我正在使用ListActivity中的复选框。我不知道如何更新服务器中的复选框。

现在我想出了未选中已检查项目的结果,并且保存意味着它将保存(或更新)服务器。但是在未选中项目中选中意味着不保存(或不更新)服务器。

Rsvp_EngagementMeals.java:

public class Rsvp_EngagementMeals extends ListActivity
{
    @Override

             checkstatus=new Hashtable<Integer,String>();
             status=new ArrayList<String>();
                veg =Rsvp_Meals.veg;  

          String user_url="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/version21/mmealitems.aspx?uname="+LoginForm.str1+"&occasion="+occasionval;    

        httpclass obj = new httpclass();
        result = obj.server_conn(user_url);

        if (result != null) 
        {           
            token = new StringTokenizer2(result, "");
        }


            value = new ArrayList<String>();

            while (token.hasMoreTokens()) 
            {
                value.add(token.nextToken());
            }   

            value.add(Integer.toString(value.size()));

            Integer k=null; 
            table=new Hashtable<Integer,ArrayList<String>>();
            itemId = new ArrayList<String>();   
            str2 = new ArrayList<String>(); 
            str3 = new ArrayList<String>();
            stritem = new ArrayList<String>();
            vegitems = new ArrayList<String>();

            int id=0,c=0,n=value.size();

            for(int j=0; j<n; j++)
            {               
                 z = value.get(j);

            String[] mystring  = z.split("<br>");

                int arraysize = mystring.length;

                for(int a=0; a<arraysize-1;a++)
                {
                str2.add(mystring[0]);
                }

            }               

            for(int g=0; g<str2.size();g++)
            {               
                String name = str2.get(g);

                token2 = new StringTokenizer2(name, "|", false);

                while (token2.hasMoreTokens()) 
                {
                vegitems.add(token2.nextToken());
                }
                            }

            for(int x=1;x<vegitems.size();x++)
            {   

                itemId.add(vegitems.get(x));
                x=x+1;
                stritem.add(vegitems.get(x));
                x=x+1;
                status.add(vegitems.get(x));

            }

                setListAdapter(new IconicAdapter(this));

            save.setOnClickListener(new OnClickListener() 
            {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    tru = new StringBuffer();
                    fals = new StringBuffer();

                    for (int i = 0; i<status.size();i++) 
                    {
                        if (status.get(i).equals("True"))

                            tru.append(itemId.get(i)+",");

                    }

                    boolean netvalue = false;
                     ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);


                        NetworkInfo info = cm.getActiveNetworkInfo();
                        if (info != null && info.isAvailable()) {
                                       netvalue = true;

                        String user_url="http://mobileapps.iwedplanner.com/mobileapps/iwedplanner/mobile/version21/minsertmealchoiceNew.aspx?uname=abcdefg&occasion=Engagement
                                "&choice="+tru+"&ownchoice=&category=";

                        httpclass obj = new httpclass();
                        result = obj.server_conn(user_url);

                        StringTokenizer st = new StringTokenizer(result, "|");
                        result = st.nextToken();

                        if ((result.equals("Engagement 1&")) || (result.equals("Wedding 1&")) || (result.equals("Reception 1&"))) 
                        {
                            //  alertbox("Success", "saved successfully");

                            Dialog locationError = new AlertDialog.Builder(Rsvp_EngagementMeals.this)
                                    .setIcon(0)
                                    .setMessage("Meals has been saved sucessfully")
                                    .setTitle("Success")
                                    .setPositiveButton(R.string.yes,
                                            new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick( 
                                                        DialogInterface dialog,
                                                        int id) {

                                                    Intent ma = new Intent(
                                                            Rsvp_EngagementMeals.this, Rsvp_Meals.class);
                                                    startActivity(ma);
                                                    finish();

                                                }
                                            }).create();
                            locationError.show();

                        } else
                            alertbox("", result);

                    } else {
                        alertbox("Message", "No Internet Connection!");
                    }

                }

            });


             }// End of On create method 



    @Override
    protected void onListItemClick(ListView parent, View v, int position,long id) 
    {
        super.onListItemClick(parent, v, position, id);

        }
public void onClick(View v)
{
    System.out.println("On View Click");
}

    @SuppressWarnings("rawtypes")
    class IconicAdapter extends ArrayAdapter 
    {
        Activity context;

        @SuppressWarnings("unchecked")
        IconicAdapter(Activity context)                    
        {
            super(context, R.layout.rsvp_mealsse, stritem);

            this.context = context;
        }

    @Override

    public View getView(int position, View convertView, ViewGroup parent) 
    {
            LayoutInflater inflater = context.getLayoutInflater();
            View row = inflater.inflate(R.layout.rsvp_mealsse,null);//viewappointlist, null);

            TextView index = (TextView) row.findViewById(R.id.index);
            index.setText(String.valueOf(position+1)+".");


            TextView label = (TextView) row.findViewById(R.id.title);
            label.setText(stritem.get(position));


            CheckBox check=(CheckBox)row.findViewById(R.id.check);

            check.setId(Integer.parseInt(itemId.get(position)));


            if(status.get(position).equals("True"))
                 check.setChecked(true);
                 ;

                    check.setOnCheckedChangeListener(new OnCheckedChangeListener()
                    {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            // TODO Auto-generated method stub
                            int ind=itemId.indexOf(String.valueOf(buttonView.getId()));
                            status.set(ind, String.valueOf(isChecked));

                        }

                    });

            return (row);
                    }
}

这是我的完整代码。如何在复选框中更改状态。我想要更新复选框时保存服务器的结果,并在查看列表后更新项目查看我的复选框。

1 个答案:

答案 0 :(得分:3)

我唯一可以指出的错误是if语句中的false拼写:

更改此代码:

if(status.get(position).equals("False"))

致此代码:

if(status.get(position).equals("false"))
希望这有效!