I have a DB in mysql, I want to show all the data in the DB as a list of chkbox with the data from the DB . and after selection of few checkbox i can fire query to DB for certain data about the selected ones. I have tried using listView below is my JAVA file and xml
here is JAVA file
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 file
<?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>
The basic concept to create a custom list view in android starts with a custom view .xml that will correspond to the layout of how each individual item will look like. For example if you want each item to have a textview for the name and a checkbox your custom layout will only contain a linear (horizontal) layout with a textview and a check box in. After that on your activity you'll need to create a class that extends Base Adapter. The main roll of this class is to get the data you want (from an array for example) and inflate that info to the layout that you created. I found this tutorial that may help you:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.