【问题标题】:Store Checkbox items into database将复选框项目存储到数据库中
【发布时间】:2011-10-25 12:45:34
【问题描述】:

我有一个使用复选框显示名称列表的 Android 应用程序。我想将选中的项目和未选中的项目存储在我的数据库中。我不想在手机上永久存储任何数据项。我需要一种机制,通过该机制我可以将数据(选中和未选中)临时存储在手机上,然后通过 Web 服务将其传输到数据库。

谁能告诉我该怎么做?我应该对我当前的代码做哪些修改?

这是我的显示列表的 Android 代码:

      package com.demo;

      import java.io.IOException;
      import java.io.UnsupportedEncodingException;
      import java.net.SocketException;
      import java.util.ArrayList;
      import java.util.List;
      import org.apache.http.NameValuePair;
      import android.app.Activity;
      import android.app.AlertDialog;
      import an droid.content.DialogInterface;
      import android.os.Bundle;
      import android.util.Log;
      import android.widget.TextView;
      import org.apache.http.client.entity.UrlEncodedFormEntity; 
      import org.apache.http.HttpEntity;
      import org.apache.http.HttpResponse;
      import org.apache.http.client.ClientProtocolException;
      import org.apache.http.client.HttpClient;
      import org.apache.http.client.methods.HttpPost;
      import org.apache.http.impl.client.DefaultHttpClient;
      import org.apache.http.message.BasicNameValuePair;
      import org.apache.http.protocol.HTTP;
      import org.json.JSONArray;
      import org.json.JSONException;
      import org.json.JSONObject;
      import org.ksoap2.SoapEnvelope;
      import org.ksoap2.serialization.SoapObject;
      import org.ksoap2.serialization.SoapPrimitive;
      import org.ksoap2.serialization.SoapSerializationEnvelope;
      import org.ksoap2.transport.HttpTransportSE;



 public class TestApp extends Activity {

private static final String SOAP_ACTION = "http://tempuri.org/getData";

    private static final String METHOD_NAME = "getData";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://10.0.2.2/getdata2/Service1.asmx";
    TextView tv;

boolean[] bln1=null; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv=(TextView)findViewById(R.id.text1);

           String[] arr2= call();


           boolean[] bln=new boolean[arr2.length];
    for(int i=0;i<arr2.length;i++)
   {  
        bln[i]=false;

    }  

   bln1 = new boolean[arr2.length];

    new AlertDialog.Builder(TestApp.this)
    .setIcon(R.drawable.alert_dialog_icon)
    .setTitle("Title")
    .setMultiChoiceItems(arr2,
            bln,
            new DialogInterface.OnMultiChoiceClickListener() {
                public void onClick(DialogInterface dialog, int whichButton,
                        boolean isChecked) {

                                        if(isChecked){
                                    bln1[whichButton] = true;
                                           }
                    else{
                        bln1[whichButton] = false;
                    }
                }
            })
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            JSONObject jsonObject = new JSONObject();
            String[] arr2=call();
            for(int i=0;i<arr2.length;i++)
                try {
                    jsonObject.put("key"+i,arr2[i]);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            JSONArray jArrayParam = new JSONArray();
            jArrayParam.put(jsonObject);

            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
            nameValuePair.add(new BasicNameValuePair("bulkdata",
                    jArrayParam.toString()));

            Log.e("bulkdata", jArrayParam.toString());

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/login2/Service1.asmx");//web service to send data to to forward to database

        httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        try {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePair,  HTTP.UTF_8));
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        // Execute HTTP Post Request

        try {
            HttpResponse response=null;
            response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            Log.e("entity:",entity.toString());
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // get response entity

        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {


        }
    })
   .show();


}


public String[] call()
{
    SoapPrimitive responsesData = null; 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( 
    SoapEnvelope.VER11); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request);

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    androidHttpTransport.debug = true; 

    try {

    androidHttpTransport.call(SOAP_ACTION, envelope);

    responsesData = (SoapPrimitive) envelope.getResponse(); 
    System.out.println(" --- response ---- " + responsesData); 

    } catch (SocketException ex) { 
    ex.printStackTrace(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    System.out.println( " ----" + responsesData );

    String serviceResponse= responsesData .toString(); 


    String[] temp; 
    String delimiter = "#"; 
    temp= serviceResponse.split(delimiter);
    System.out.println( " ---- length ---- " + temp.length); 

    return temp; 

                }

         } 

【问题讨论】:

    标签: android web-services android-listview checkbox ksoap


    【解决方案1】:

    我想错误出在 .setPositiveButton() 方法中,在 String[] arr2=call() 行中。

    您为什么要再次从您的网络服务中读取数据,就像您在 onCreate 方法中所做的那样,并将其放入 json 结果中?

    你不应该输入json结果复选框状态,而不是原始数据吗?

    【讨论】:

    • 代码中没有错误..它工作正常..我只是想通过某种机制将选中和未选中的项目存储到数据库中来对其进行改进......忽略第二个HttpPost ...我还没有真正使用过它,是的,它应该被删除并且会这样做..非常感谢! :D
    • Ah ok ;) 使用 SharedPreferences 存储您的复选框结果并在您想要发送它们时检索它们怎么样?在应用程序生命周期结束时,重置首选项。 developer.android.com/reference/android/content/…
    • 当我的数据库完成后,我会说 70-80 个条目。那么 SharedPreferences 是否有任何大小限制?而且我必须在我的 .setPositive 和 .setNegative 方法中使用 SharedPreferences 对吗?
    • 基本上SharedPreferences是系统解析出来的xml文件,添加80个以上的字段也没问题。 .SetPositive 和 .SetNegative 指的是确定取消按钮事件,而不是按下对话框内的单个复选框。请参阅官方文档以了解如何使用列表管理对话框(快速问题,在 OnMultiChoiceClickListener 事件中):developer.android.com/guide/topics/ui/dialogs.html#AddingAList
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 2015-04-17
    • 2012-03-15
    • 1970-01-01
    • 2013-02-20
    • 2019-02-09
    相关资源
    最近更新 更多