How to display Android database data in a ListView?

Display a button to store data in database in an Android ListView

  • I have a simple Android ListView code that displays a list of names that is returned from the web service I have created. I need to add three buttons at the end of the List because: 1) On pressing of each of the 2 buttons the entries marked/checked should be stored in a separate column of my database 2) Submit button to send the data For storing the data, I am planning to call a webservice when the user clicks the submit button. My problem is how do I store the checked values for both my buttons? How do I send that data via my webservice? Also when I run the code below, I am not able to see the buttons Can anyone please suggest any ideas? My Android code: package com.example; import java.net.SocketException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; 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 display 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; private ListView lView; private Button markpresent; private Button markabsent; private Button submit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.displaydata); tv=(TextView)findViewById(R.id.text1); String[] arr2= call(); lView = (ListView) findViewById(R.id.ListView01); //Set option as Multiple Choice. So that user can able to select more the one option from list lView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, arr2)); lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); markpresent = (Button) findViewById(R.id.Button01); markpresent.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //put checked entries in database } }); markabsent = (Button) findViewById(R.id.Button02); markabsent.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //put checked entries in database } }); submit = (Button) findViewById(R.id.Button03); submit.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //call another web service for insertion of data } }); } //webservice call works fine 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; } } My displaydata.xml file <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <ListView android:id="@+id/ListView01" android:layout_height="wrap_content" android:layout_width="fill_parent"></ListView> <Button android:layout_height="wrap_content" android:id="@+id/Button01" android:text="Mark present" android:layout_width="fill_parent" /> <Button android:layout_height="wrap_content" android:id="@+id/Button02" android:text="Mark absent" android:layout_width="fill_parent" />

  • Answer:

    The reason your buttons do not show up is that your XML is incorrect. I believe this achieves the layout you want: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/ListView01" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" > <Button android:id="@+id/Button01" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Mark present" /> <Button android:id="@+id/Button02" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Mark absent" /> </LinearLayout> </RelativeLayout> If you want to get the checked items from the ListView when a button is pressed, do something like this: markpresent = (Button) findViewById(R.id.Button01); markpresent.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { SparseBooleanArray checked = lView.getCheckedItemPositions(); ArrayList<String> items = new ArrayList<String>(); for (int i = 0; i < arr2.length; i++) { if (checked.get(i)) { items.add(arr2[i]); } } Log.d("", "items:"); for (String string : items) { Log.d("", string); } // put checked entries in database } }); Finally, if you want to learn about how to store data in a database look here http://developer.android.com/guide/topics/data/data-storage.html and if you want to send the data to your web service, it really depends on the protocol the server is expecting, but if you are designing it from scratch here are a few options http://stackoverflow.com/questions/3027066/how-to-send-a-json-object-over-request-with-android http://hc.apache.org/httpclient-3.x/methods/post.html

Parth Doshi at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.