在警告对话框中创建复选框并从数据库中获取选中的值

时间:2011-03-11 14:54:37

标签: android database checkbox alertdialog

如何在警告对话框中创建复选框并从数据库中获取检查值

请给我一些代码示例谢谢。

1 个答案:

答案 0 :(得分:0)

此代码对您有所帮助。评论是内联的

    public class HelloActivity extends Activity {
private InputStream is = null;
private StringBuilder sb=null;
private String resultString = null;
private JSONArray jArray;
private String categoryList = "";
private String[] categoryListArray ;
public HelloActivity() {
}

/**
 * Called with the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getCategoryList();//get the category list 
    createCategoryView();
}
/*
 * based on the data retrived from the database, the list of category names will be showed here
 */
private void createCategoryView() {
    // TODO Auto-generated method stub

    ScrollView sv = new ScrollView(this);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);

    TextView tv = new TextView(this);
    tv.setText("Foods :-)");
    ll.addView(tv);
    for(int i = 0; i < categoryListArray.length; i++) {
        CheckBox cb = new CheckBox(this);
        cb.setText(categoryListArray[i]);
        ll.addView(cb);
    }
    Button b = new Button(this);
    b.setText("Find Shops");
    b.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), categoryList ,Toast.LENGTH_LONG).show();
        }
    });
    ll.addView(b);
    this.setContentView(sv);

}
/*
 * For getting the category list from the database
 * I used php script here, which will return JSON object contains categoryId and CategoryName
 */
private void getCategoryList() {
    // TODO Auto-generated method stub
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    //http post
    try{
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://192.168.11.156/city.php");
        nameValuePairs.add(new BasicNameValuePair("requestText", "requestText"));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception ex){
          Log.e("log_tag", "Error in http connection"+ex.toString());
    }

    //convert response to string
    try{
          BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
           sb = new StringBuilder();
           sb.append(reader.readLine() + "\n");
           String line="0";
           while ((line = reader.readLine()) != null) {
                          sb.append(line + "\n");
            }
            is.close();
            resultString=sb.toString();
        }catch(Exception e){
                  Log.e("log_tag", "Error converting result "+e.toString());
        }
      //paring data
        int categoryId;

        String categoryName;
        try{
              jArray = new JSONArray(resultString);
              JSONObject json_data=null;
              categoryListArray = new String[jArray.length()];
              for(int i=0;i<jArray.length();i++){
                 json_data = jArray.getJSONObject(i);
                 categoryId=json_data.getInt("categoryId");
                 categoryName=json_data.getString("categoryName");
                 categoryListArray[i] = categoryName;
                 categoryList = categoryList + categoryName;
                 if(i != jArray.length()-1){
                     categoryList += ",";
                 }
              }
            }
              catch(JSONException e1){
                  Toast.makeText(getBaseContext(), "No Categories are found" ,Toast.LENGTH_LONG).show();

              } catch (ParseException e1) {
                    e1.printStackTrace();
            }
}
}

由于

相关问题