Chulbul Pandey
Chulbul Pandey

Reputation: 506

How to Implement ListView while fetching data from server

I have written a program to fetch data from Server and to show in Android Activity using TextView(s), and now i want to show those data into List using ListView, but i don't know how i need to implement ListView in my code to show data in a List.

I am fetching two fields from every row, namely : TotalAmount and ItemDetails.

Note: I have written XML File also for ListView namely- listrow_orders

Please i don't require any other tutorial link, brief me according to my code, it would be better for best understanding.

OrdersActivity.java:

 public class OrdersActivity extends Activity 
{ 
    public static final String LOG_TAG = "OrdersActivity";

    TextView txtDetail,txtAmount ;
    String MemberID,resultServer,strTotal,strDetails,strOrderID;

    @SuppressLint("NewApi")
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orders);

        // Permission StrictMode
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            }           

            txtAmount = (TextView)findViewById(R.id.txtTotalAmount);
            txtDetail = (TextView)findViewById(R.id.txtItemDetails);

            String url = "http://172.16.0.4/res/order_fetch.php";
            Intent intent= getIntent();
            MemberID = intent.getStringExtra("MemberID");
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("sMemberID", MemberID));
            resultServer  = getHttpPost(url,params);

            strTotal = "";
            strDetails = "";

            JSONObject c;
            try {
            c = new JSONObject(resultServer);
            strTotal = c.getString("TotalAmount");  
            Log.d("Total Amount::", " "+strTotal);
            strDetails = c.getString("ItemDetails");
            Log.d("Item Details::", " "+strDetails);

            if(!strDetails.equals(""))
            {                   
                txtAmount.setText(strTotal);
                txtDetail.setText(strDetails);              
            }
            else
            {               
                txtDetail.setText("-");
                txtDetail.setText("-");
            }
            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }       

    }       

activity_orders.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" >

<ListView 
    android:layout_width="match_parent" 
    android:id="@+id/list_orders" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/header" 
    android:cacheColorHint="#00000000" 
    android:divider="#b5b5b5" 
    android:dividerHeight="1dp" 
    android:layout_alignParentBottom="true" />

</RelativeLayout>

listrow_orders.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 >

<TextView
    android:id="@+id/txtTotalAmount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:text="TextView" />

<TextView
    android:id="@+id/txtItemDetails"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/textView3"
    android:text="Item Details Here" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Total Amount:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#a60704" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/txtTotalAmount"
    android:layout_below="@+id/txtTotalAmount"
    android:text="Ordered Items:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#a60704" />

</RelativeLayout>

Upvotes: 1

Views: 1090

Answers (1)

Oscht&#228;rEi
Oscht&#228;rEi

Reputation: 2255

First of all you should use an AsyncTask for your post-request (since such stuff should not run in the gui thread, it will block it). For your results you best create a new class. For each result you create an instance of this class and put them in an ArrayList.

Secondly check out this tutorial. Use an ArrayAdapter - there is no need for a custom adapter.

Upvotes: 2

Related Questions