1

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>
4
  • use a custom listview with a custom adapter. example shows image and text. instead of image you can show text androidhive.info/2012/02/… Commented May 23, 2013 at 11:54
  • Google Android custom BaseAdapter, that's your solution. Commented May 23, 2013 at 11:55
  • @Raghunandan if possible so please show me that few lines of code require to use in my program to implement ListView, big tutorial will not help me Commented May 23, 2013 at 12:01
  • @ChulbulPandey you check this stackoverflow.com/questions/10816243/…. if it helps Commented May 23, 2013 at 12:04

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.