How i can create a custom adapter for this xml.Can someone guide me on how to create a custom adapter or give me a sample code on how to do it. Please help me i need to learn how to create custom adapter.Help me please. Thanks in advance.
This is my MainActivity.java
package com.example.work.listviewadapter;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private static final String PRODUCTLIST_URL ="http://192.168.56.1/productlist.php";
private ProgressDialog pDialog;
public static final JSONParser jParser = new JSONParser();
private static final String GET_PRODUCT = "message";
private static final String GET_ID = "ID";
private static final String GET_BRAND = "Brand";
private static final String GET_CATEGORY = "Category";
private static final String GET_DESCRIPTION = "Description";
private static final String GET_CODE = "Code";
private static final String GET_QUANTITY = "Quantity";
private static final String GET_UNIT = "Unit";
private static final String GET_UNITPRICE = "Unitprice";
private JSONArray order = null;
ListView list;
private ArrayList<HashMap<String, String>> orderlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productlist);
new LoadComments().execute();
}
public void updateJSONdata() {
orderlist = new ArrayList<HashMap<String, String>>();
JSONObject json = jParser.getJSONFromUrl(PRODUCTLIST_URL);
try {
order = json.getJSONArray(GET_PRODUCT);
for (int i = 0; i < order.length(); i++) {
JSONObject c = order.getJSONObject(i);
String id = c.getString(GET_ID);
String brand = c.getString(GET_BRAND);
String category = c.getString(GET_CATEGORY);
String description = c.getString(GET_DESCRIPTION);
String code = c.getString(GET_CODE);
String quantity = c.getString(GET_QUANTITY);
String unit = c.getString(GET_UNIT);
String unitprice = c.getString(GET_UNITPRICE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(GET_ID,id);
map.put(GET_BRAND, brand);
map.put(GET_CATEGORY, category);
map.put(GET_DESCRIPTION, description);
map.put(GET_CODE, code);
map.put(GET_QUANTITY, quantity);
map.put(GET_UNIT, unit);
map.put(GET_UNITPRICE, unitprice);
orderlist.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void updateList() {
//this is for my custom listview
}
final public class LoadComments extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Order...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Boolean doInBackground(Void... arg0) {
//we will develop this method in version 2
updateJSONdata();
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
//we will develop this method in version 2
updateList();
}
}
}
This is my Custom ListView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip"
android:background="@drawable/list_selector"
>
<TextView
android:id="@+id/Quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10000"
android:textSize="24dip"
android:textStyle="bold"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="24dp"
android:layout_marginRight="51dp"
>
</TextView>
<TextView
android:id="@+id/Unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gal."
android:layout_alignParentRight="true"
android:layout_marginTop="26dp"
android:textSize="20dp"
android:layout_marginRight="15dp"
/>
<TextView
android:id="@+id/Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="White"
android:textColor="#040404"
android:typeface="sans"
android:textSize="20dip"
android:textStyle="bold"/>
<TextView
android:id="@+id/Code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wpu-01"
android:layout_marginTop="2dp"
android:layout_below="@+id/Description"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/Brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Code"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:text="Weber"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Urethane"
android:layout_below="@+id/Code"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/Brand"
android:layout_marginLeft="5dp"
android:id="@+id/Category" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Category"
android:text="P"
android:layout_marginLeft="12dp"
android:paddingRight="5dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:textStyle="italic"
android:id="@+id/P"
/>
<TextView
android:id="@+id/Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10000.00"
android:layout_below="@+id/Brand"
android:layout_toRightOf="@+id/P"
android:layout_marginTop="2dp"
android:layout_marginLeft="2dp"
/>
<TextView
android:id="@+id/ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11"
android:layout_alignParentRight="true"
android:visibility="invisible"
/>
</RelativeLayout>
This for the listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Search"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:singleLine="true"/>
<ListView
android:layout_below="@+id/search"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:divider="#ff5873ff"
android:dividerHeight="3dp" />