如何将数据库中的数据显示为android中的复选框列表

时间:2014-04-07 07:26:32

标签: java android mysql xml

我在mysql中有一个数据库,我希望将数据库中的所有数据显示为chkbox列表,其中包含来自数据库的数据。选择几个复选框后,我可以向DB查询有关所选数据的某些数据。我尝试使用下面的listView是我的JAVA文件和xml

这里是JAVA文件

public class BdaySelect extends Activity {
     private String jsonResult;
     private String url = "http://10.0.2.2/markit/login.php";
     private ListView listView;
     private TextView textv1;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.bday_select);
      listView = (ListView) findViewById(R.id.listView1);
      textv1=(TextView)findViewById(R.id.textView1);
      accessWebService();
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
     }

     // Async Task to access the web
     private class JsonReadTask extends AsyncTask<String, Void, String> {
      @Override
      protected String doInBackground(String... params) {
       HttpClient httpclient = new DefaultHttpClient();
       HttpPost httppost = new HttpPost(params[0]);
       try {
        HttpResponse response = httpclient.execute(httppost);
        jsonResult = inputStreamToString(
          response.getEntity().getContent()).toString();
       }

       catch (ClientProtocolException e) {
        e.printStackTrace();
       } catch (IOException e) {
        e.printStackTrace();
       }
       return null;
      }

      private StringBuilder inputStreamToString(InputStream is) {
       String rLine = "";
       StringBuilder answer = new StringBuilder();
       BufferedReader rd = new BufferedReader(new InputStreamReader(is));

       try {
        while ((rLine = rd.readLine()) != null) {
         answer.append(rLine);
        }
       }

       catch (IOException e) {
        // e.printStackTrace();
        Toast.makeText(getApplicationContext(),
          "Error..." + e.toString(), Toast.LENGTH_LONG).show();
       }
       return answer;
      }

      @Override
      protected void onPostExecute(String result) {
       ListDrwaer();
      }
     }// end async task

     public void accessWebService() {
      JsonReadTask task = new JsonReadTask();
      // passes values for the urls string array
      task.execute(new String[] { url });
     }

     // build hash set for list view
     public void ListDrwaer() {
      List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();

      try {
       JSONObject jsonResponse = new JSONObject(jsonResult);
       JSONArray jsonMainNode = jsonResponse.optJSONArray("emp_info");
       for (int i = 0; i < jsonMainNode.length(); i++) {
        JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
        String name = jsonChildNode.getString("emp_name");
        String number = jsonChildNode.getString("emp_no");
        //String chkBoxNo = Integer.toString(i);
        //String chkBoxName = "chk"+chkBoxNo;
        //CheckBox chkBoxName = new CheckBox(this);

        String outPut = name + "-" + number;
        employeeList.add(createEmployee("employees", outPut));
       }
      } catch (JSONException e) {
       Toast.makeText(getApplicationContext(), "Error" + e.toString(),
         Toast.LENGTH_SHORT).show();
      }

      SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
        android.R.layout.simple_list_item_1,
        new String[] { "employees" }, new int[] { android.R.id.text1 });
      listView.setAdapter(simpleAdapter);
     }

     private HashMap<String, String> createEmployee(String name, String number) {
      HashMap<String, String> employeeNameNo = new HashMap<String, String>();
      employeeNameNo.put(name, number);
      return employeeNameNo;
     }
    }

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#29001F"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/bck" />
<Space
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_weight="2" />

        <ImageButton
            android:id="@+id/imageButton1"
            android:paddingRight="10dp"
            android:background="#29001F"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/setting2" />

    </TableRow>

    <TableRow
        android:background="#29001F"
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:gravity="center"
            android:textColor="#FFFFFF"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="SELECT"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="#94808F"
            android:text="DISCOUNT"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:textColor="#94808F"
            android:layout_weight="1"
            android:text="SEND"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </TableRow>
<Space
        android:layout_width="match_parent"
        android:layout_height="20dp" />



        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

        <TableRow
            android:id="@+id/tableRow6"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="102dp"
            android:gravity="right"
            android:paddingRight="30dp" >



            <Button
                android:id="@+id/butBdaySelectNext"
                android:layout_width="80dp"
                android:layout_height="35dp"
                android:layout_gravity="center"
                android:background="@drawable/arrownext"
                android:paddingRight="15dp"
                android:text="Next"
                android:textColor="#FFFFFF" />
        </TableRow>


</LinearLayout>

1 个答案:

答案 0 :(得分:0)

在android中创建自定义列表视图的基本概念以自定义视图.xml开始,该视图将对应于每个项目的外观布局。例如,如果您希望每个项目都有一个名称的文本视图和一个复选框,您的自定义布局将只包含一个带有textview和复选框的线性(水平)布局。在您的活动之后,您需要创建一个扩展Base Adapter的类。此类的主要内容是获取所需的数据(例如,从数组中获取)并将该信息膨胀到您创建的布局。我发现本教程可以帮助您:

http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92