2011-11-24 58 views
0

我想要从Android sqlite上传到sql服务器。它的工作正常上传。问题是:每个表上传完成,服务返回true/false。如果这是真的,那么需要setChecked(true);。在这里,我动态创建一个方法复选框,我想设置从另一种方法状态....Android复选框setCheck(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; 
} 

这是不检查检查框值。我怎么能特别checkbox.for例如findById()也不能使用它。

布局设计是这样的:

CheckBox WMInvoice 
    CheckBox WMInvoiceLine 

像that.but它动态地创建。

请帮我从这个问题

回答

0

创建的列表,可以牵你的CheckBoxes

ArrayList<CheckBox> myCheckBoxes; 

for-loop之前实例化它onCreate()

myCheckBoxes = new ArrayList<CheckBox>(); 

当您添加的每个CheckBox到你的TableRow(在你的for-loop)也加我吨至您的列表:

myCheckBoxes.add(ch); 

然后你需要确定哪些CheckBox期间soapPrimitiveData -call检查的一些方法。我看到你设置的每个CheckBox的ID,所以如果你能以某种方式传递一个intsoapPrimitiveData - 方法可以检查正确的CheckBox这样的:

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

感谢您的帮助。但它总是检查最后一个项目。它没有找到具体的复选框。例如,如果我上传第二个表,那么它应该检查第二个复选框。但它检查最后一个。请问有什么想法? – Piraba

0

要设置ID您选择相同的方式的方式容器使用ID

ch.getId(i); 

这会给你在布局中第i个复选框

1

确保您ch.setChecked(true);是一个UI THRE中调用广告,请尝试:

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