Android CheckBox setCheck(True)

时间:2011-11-24 10:19:26

标签: android

我想从Android sqlite上传到sql sever。其工作正常上传。问题是:每个表上传完成,服务返回true / false。如果是,那么需要setChecked(true);。这里我在一个方法中动态创建了复选框,我想从另一个方法设置状态....

   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View viewInflate = LayoutInflater.from(UploadActivity.this).inflate(R.layout.upload_screen, null);
    setContentView(viewInflate);
    if (uploadTable.size() > 0) {
        for (int i = 0; i < uploadTable.size(); i++) {
            TableRow tr = new TableRow(this);
            tr.setId(i);
            ch = new CheckBox(this);
            ch.setId(i);
            ch.setChecked(false);
            tr.addView(ch);
            TextView tv2 = new TextView(this);
            createView(tr, tv2, uploadTable.get(i));
            uploadingTable.addView(tr);
        }
    }
  }

这是调用服务的方法..

   public boolean soapPrimitiveData(String tablename,String strBusinessUnit, String strExecutive,String jsonString) throws IOException,XmlPullParserException {
    SoapPrimitive responsesData = null;
    boolean status =false;
    SoapObject requestData = new SoapObject(NAMESPACE, METHOD_NAME); // set
    requestData.addProperty("strBusinessUnit", strBusinessUnit);
    requestData.addProperty("strExecutiveCode", strExecutive);
    requestData.addProperty("strTableName", tablename);
    requestData.addProperty("jsonContent", jsonString);
    SoapSerializationEnvelope envelopes = new SoapSerializationEnvelope(SoapEnvelope.VER11); // put all required data into a soap//// envelope
    envelopes.dotNet = true;
    envelopes.setOutputSoapObject(requestData);
    AndroidHttpTransport httpTransport = new AndroidHttpTransport(APPURL);
    httpTransport.debug = true;
    try {
        httpTransport.call(SOAP_ACTION, envelopes);
        responsesData = (SoapPrimitive) envelopes.getResponse();
        if((responsesData.toString()).equals("true")){
            DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(UploadActivity.this);

            status = dbAdapter.updateUploadedTable(tablename, strExecutive, strBusinessUnit);

                 if(status){
                   ch.setChecked(true);
                  }
        }


    } catch (SocketException ex) {
        Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
        ex.printStackTrace();
    } catch (Exception e) {
        Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
       e.printStackTrace();
    }
    return status;
}

未选中此复选框值。我怎么能拿特定的复选框。例如,findById()也不能使用它。

这样的布局设计:

    CheckBox  WMInvoice
    CheckBox  WMInvoiceLine

就像那样。但它会动态创建。

请帮我解决这个问题

3 个答案:

答案 0 :(得分:2)

确保在UI线程中调用ch.setChecked(true);,尝试:

runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
             ch.setChecked(true);
        }
    });

答案 1 :(得分:0)

创建可以容纳CheckBoxes的列表:

ArrayList<CheckBox> myCheckBoxes;

onCreate()

之前在for-loop内实例化
myCheckBoxes = new ArrayList<CheckBox>();

当您将CheckBox添加到TableRow时(在for-loop中),同时将其添加到您的列表中:

myCheckBoxes.add( ch );

然后,您需要某种方法来识别CheckBox期间要检查的soapPrimitiveData - 来电。我看到你设置了每个CheckBox的ID,所以如果你能以某种方式将int传递给soapPrimitiveData方法,你可以像这样检查右CheckBox

if(status){
   for( CheckBox box : myCheckBoxes )
      if( box.getId() == idPassedToMethod ) {
         ch.setChecked(true);
         break;
      }
}

答案 2 :(得分:0)

您设置ID的方式与使用ID

选择容器的方式相同
ch.getId(i);

这将为您提供布局中的第i个复选框