繁体   English   中英

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

[英]How to show data from DB as a checkbox list in android

我在mysql中有一个数据库,我想将数据库中的所有数据显示为chkbox列表以及来自数据库的数据。 在选择了几个复选框后,我可以向数据库查询有关所选数据的某些数据。 我尝试使用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>

在android中创建自定义列表视图的基本概念始于自定义视图.xml,该视图将与每个单独项目的外观布局相对应。 例如,如果您希望每个项目都有一个名称的文本视图和一个复选框,则您的自定义布局将仅包含带有文本视图和复选框的线性(水平)布局。此后,您需要创建一个扩展基本适配器的类。 该类的主要用途是获取所需的数据(例如,从数组中获取)并将该信息添加到您创建的布局中。 我发现本教程可以为您提供帮助:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM