How to show listviewitem when i input a text into Edittext. I searched already from the net but i dont have an idea on how to do it.
This is the code
MainActivity.java
public class MainActivity extends ListActivity {
private ProgressDialog pDialog;
private EditText et,search;
private static final String URL = "http://192.168.254.101/productlist.php";
JSONParser jsonParser = new JSONParser();
private static final String Success ="success";
private static final String TAG_POSTS = "message";
private static final String TAG_BRAND = "Brand";
private static final String TAG_CATEGORY = "Category";
private static final String TAG_DESCRIPTION = "Description";
private static final String TAG_CODE = "Code";
private static final String TAG_QUANTITY = "Quantity";
private static final String TAG_UNIT = "Unit";
private static final String TAG_UNITPRICE = "Unitprice";
private JSONArray mComments = null;
//manages all of our comments in a list.
private ArrayList<HashMap<String, String>> mCommentList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
search = (EditText)findViewById(R.id.EditText01);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//loading the comments via AsyncTask
new LoadComments().execute();
}
public void updateJSONdata() {
int success;
String searchdesc = search.getText().toString();
mCommentList = new ArrayList<HashMap<String, String>>();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", searchdesc));
JSONObject json = jsonParser.makeHttpRequest(
URL, "POST", params);
success = json.getInt(Success);
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
//gets the content of each tag
String brand = c.getString(TAG_BRAND);
String category = c.getString(TAG_CATEGORY);
String description = c.getString(TAG_DESCRIPTION);
String code = c.getString(TAG_CODE);
String quantity = c.getString(TAG_QUANTITY);
String unit = c.getString(TAG_UNIT);
String unitprice = c.getString(TAG_UNITPRICE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_BRAND, brand);
map.put(TAG_CATEGORY, category);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_CODE, code);
map.put(TAG_QUANTITY, quantity);
map.put(TAG_UNIT, unit);
map.put(TAG_UNITPRICE, unitprice);
mCommentList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void updateList() {
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] { TAG_BRAND, TAG_CATEGORY,
TAG_DESCRIPTION, TAG_CODE, TAG_QUANTITY, TAG_UNIT, TAG_UNITPRICE}, new int[]{ R.id.Brand, R.id.Category,
R.id.Description, R.id.Code, R.id.Quantity, R.id.Unit, R.id.Price
});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
public class LoadComments extends AsyncTask<Void, Void, Boolean> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Products...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
updateList();
}
}
}
activity_main.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<LinearLayout
android:id="#+id/top_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/EditText01"
android:layout_gravity="center_horizontal"
android:hint="Search.." />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_layover"
android:layout_below="#+id/top_layover"
android:background="#fff"
android:divider="#android:color/transparent"
android:scrollbars="none" />
<LinearLayout
android:id="#+id/bottom_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
a
single_post.xml
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#f0f0f0"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dp"
android:background="#ffffff">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Brand: ">
</TextView>
<TextView
android:id="#+id/Brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Category: " >
</TextView>
<TextView
android:id="#+id/Category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Description: " >
</TextView>
<TextView
android:id="#+id/Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Code: " >
</TextView>
<TextView
android:id="#+id/Code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Quantity: ">
</TextView>
<TextView
android:id="#+id/Quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Unit: " >
</TextView>
<TextView
android:id="#+id/Unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Price: " >
</TextView>
<TextView
android:id="#+id/Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
this is the php code search.php
<?php
try{
$handler= new PDO('mysql:host=localhost;dbname=account','root','');
$handler->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(Exception $e){
echo $e->getMessage();
die();
}
$description = $_POST['search'];
$query =$handler->query( "Select * from tblproducts where Description like '%$description%' ");
$records= array();
$records =$query->fetchAll(PDO::FETCH_ASSOC);
$json["success"]=1;
$json["message"]=$records;
echo json_encode($json);
?>
ok i edit my answer with your case
please put this line in onCreate method:
setContentView(R.layout.activity_main);
on top of this line
search = (EditText)findViewById(R.id.EditText01);
after that call this method
search = (EditText) findViewById(R.id.EditText01);
txt.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
updateJSONdata(s.toString());
}
});
after that change updateJSONdata method first line to this
public void updateJSONdata(String searchdesc){
and comment this line
String searchdesc = search.getText().toString();
maybe this help
Related
I am new to recycler view widget , i want to get my data from my api url and list the data into my recycler view , i have these classes : recyclerAdapter , recyclerView , MainActivity , row .
the data is loaded from api succesfully but the does not set the json string received from the api in the recycler view , and the recycler view is shown white and empty . any body can find the problem in my code ?
MainActivity.java code is :
public class MainActivity extends AppCompatActivity {
final String member_id = "2";
final String get_cash_url = "http://famila1.ir/khabgah/get_khabgah_cash.php?member_id=" + member_id;
private RecyclerView rc;
private RecyclerView.Adapter adapter;
private List<recyclerView> cashList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rc = findViewById(R.id.rc);
rc.setHasFixedSize(true);
rc.setLayoutManager(new LinearLayoutManager(this));
cashList = new ArrayList<>();
loadUrlData();
}
private void loadUrlData() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
get_cash_url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
System.out.println(response);
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("bedehkariHa");
for (int i = 0; i < array.length(); i++) {
JSONObject jo = array.getJSONObject(i);
recyclerView temp = new recyclerView(jo.getString("hazine"), jo.getString("tarikh"), jo.getString("tozihat"), jo.getString("member_id"), "kir");
cashList.add(temp);
}
adapter = new recyclerAdapter(cashList, getApplicationContext());
rc.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
progressDialog.setMessage("somthing was wrong during runnig app...");
progressDialog.show();
System.out.println("error: "+error);
}
}
);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
recyclerView.java code is :
public class recyclerView {
private String hazine,tarikh,tozihat,madarkharj,harSahm;
public recyclerView(String hazine,String tarikh,String tozihat,String madarkharj,String harSahm ) {
this.hazine = hazine;
this.harSahm = harSahm;
this.madarkharj = madarkharj;
this.tarikh = tarikh;
this.tozihat = tozihat;
}
public String getHazine(){return hazine;}
public String getTarikh(){return tarikh;}
public String getTozihat(){return tozihat;}
public String getMadarkharj(){return madarkharj;}
public String getHarSahm(){return harSahm;}
}
public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.ViewHolder>{
public static final String KEY_NAME = "name";
public static final String KEY_LASTNAME = "lastname";
public static final String KEY_IMAGE = "image";
public static final String KEY_HAZINE = "hazine";
public static final String KEY_TARIKH = "date";
public static final String KEY_MADARKHARJ = "member_id";
private List<recyclerView> cashlist;
private Context context;
Recycler adapter class is this :
public recyclerAdapter(List<recyclerView> cashlist, Context context){
this.cashlist = cashlist;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.row,viewGroup,false);
return new recyclerAdapter.ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, final int position) {
final recyclerView recyclerviews = cashlist.get(position);
viewHolder.hazine.setText(recyclerviews.getHazine());
viewHolder.harSahm.setText(recyclerviews.getHarSahm());
viewHolder.tarikh.setText(recyclerviews.getTarikh());
viewHolder.tozihat.setText(recyclerviews.getTozihat());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "TEST is Toasted", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return cashlist.size();
}
// Inside this class, we’ll have another class for the ViewHolder thus
public class ViewHolder extends RecyclerView.ViewHolder{
TextView hazine,harSahm,tozihat,tarikh;
public ViewHolder(#NonNull View itemView) {
super(itemView);
hazine = (TextView) itemView.findViewById(R.id.mablaq_hazine);
harSahm = (TextView) itemView.findViewById(R.id.bedehkariha_dong);
tarikh = (TextView) itemView.findViewById(R.id.tarikh_hazine);
tozihat = (TextView) itemView.findViewById(R.id.babate_hazine);
}
}
}
and this is row.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#android:color/white" >
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:padding="5dp"
android:background="#android:color/holo_blue_bright" android:layout_marginBottom="5dp"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="3dp"
android:background="#android:color/holo_blue_dark">
<TextView
android:text="سهم شما:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/tarikh_hazine2"
android:textSize="18sp" android:textColor="#android:color/holo_red_dark"
android:textStyle="bold"
/>
<TextView
android:text="قالب پرداخت"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/bedehkariha_dong"
android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"/>
<TextView
android:text="مبلغ کل: "
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/mablaq_hazine2"
android:textSize="18sp" android:textColor="#android:color/holo_red_dark"
android:textStyle="bold"/>
<TextView
android:text="مبلغ کل"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/mablaq_hazine" android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="3dp"
android:background="#android:color/holo_blue_dark">
<TextView
android:text="تاریخ:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/tarikh_hazine22"
android:textSize="18sp" android:textColor="#android:color/holo_red_dark"
android:textStyle="bold"
/>
<TextView
android:text="تاریخ"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/tarikh_hazine" android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:inputType="textMultiLine"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="3dp"
android:background="#android:color/holo_blue_dark">
<TextView
android:text="توضیحات: "
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/babate_hazine2"
android:textSize="18sp" android:textColor="#android:color/holo_red_dark"
android:textStyle="bold"
/>
<TextView
android:text="هزینه بابت......"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/babate_hazine" android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:inputType="textMultiLine"
android:gravity="right"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_marginTop="2dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_weight="1" android:padding="2dp">
<TextView
android:text="پرداخت از: "
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/pardakhtaz" android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none"
android:background="#android:color/white" android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/pardakhtaz1" android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#FF75AD06" android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/pardakhtaz2" android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#android:color/holo_green_dark" android:padding="2dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/pardakhtaz3" android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#FF75AD06" android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/pardakhtaz4" android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#android:color/holo_green_dark" android:padding="2dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/pardakhtaz5" android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#75ad06" android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/pardakhtaz6" android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#android:color/holo_green_dark" android:padding="2dp"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_weight="1" android:padding="2dp">
<TextView
android:text="هزینه شریکی با:"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/moshtarak_ba"
android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none"
android:background="#android:color/white" android:gravity="center"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/moshtarak_ba1"
android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#FFF10D0D" android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/moshtarak_ba2"
android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#android:color/holo_red_dark" android:padding="2dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/moshtarak_ba3"
android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#FFF10D0D" android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/moshtarak_ba4"
android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#android:color/holo_red_dark" android:padding="2dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/moshtarak_ba5"
android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#FFF10D0D" android:padding="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="#+id/moshtarak_ba6"
android:layout_weight="1"
android:textSize="18sp" android:textColor="#android:color/black"
android:fadingEdge="none" android:gravity="right"
android:background="#android:color/holo_red_dark" android:padding="2dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Parsing Problem first I got.Change
JSONArray array = jsonObject.getJSONArray("bedehkariHa");
To:
JSONArray array = jsonObject.getJSONArray("bedehkari_ha");
Then check
According to the result from JSON, the key bedehkariHa does not exist and that may cause Parsing Error, a JSONException. So instead of this:
JSONArray array = jsonObject.getJSONArray("bedehkariHa");
use this to get the JSONArray associated with bedehkari_ha:
JSONArray array = jsonObject.getJSONArray("bedehkari_ha");
I'd firstly look at your general approach.
You seem to be instantiating a Volley request...I bet this isn't the only one in your app?
Make a "NetworkService" singleton that holds application Context and call that as a service layer...rather than calling a new Volley instance for each class you need it!
Here is one I wrote for my apps...I have omitted a lot of the methods and left you with a GetRequest method. You could add the URLEncoding of parameters in the service.
You can call this by simply in an activity/fragment:
HashMap<String,String> urlParams = new HashMap<>();
urlParams.put("param1","value1");
VolleyRequestService.getInstance(this).getJSONObject(urlParams, true, new GetJSONObjectRequestable() {
#Override
public void onSuccessfulAPIResponse(JSONObject response) {
}
#Override
public void onFailedAPIResponse(VolleyRequestService.ErrorType errorType, String description) {
}
},"https://myendpoint.com/endpoint.php");
And the service:
package devdroidjonno.com.tests.Networking;
import android.content.Context;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
public class VolleyRequestService {
private static final String DEBUG = "VolleyRequestService";
private static final String authCode = "87fe987vwefb987eb9w87wv";
private static VolleyRequestService ourInstance = null;
private static RequestQueue requestQueue;
private VolleyRequestService(Context applicationContext) {
this.requestQueue = Volley.newRequestQueue(applicationContext);
Log.d(DEBUG, "Init Volley Service " + this.hashCode());
}
public static VolleyRequestService getInstance(Context context) {
if (ourInstance == null) {
ourInstance = new VolleyRequestService(context.getApplicationContext());
}
return ourInstance;
}
// A GET request to an endpoint that returns a JSONObject.
public void getJSONObject(HashMap<String, String> urlParams, final boolean requiresAuthHeader, final GetJSONObjectRequestable callback, String endpointURL) {
endpointURL += "?";
for (Map.Entry mapEntry : urlParams.entrySet()) {
endpointURL += mapEntry.getKey() + "=" + mapEntry.getValue() + "&";
}
Log.d(DEBUG, endpointURL);
requestQueue.add(new JsonObjectRequest(Request.Method.GET, endpointURL, null, response -> {
Log.d(DEBUG, response.toString());
try {
if (response.has("error")) {
callback.onFailedAPIResponse(ErrorType.APIERROR, response.getString("error"));
} else {
callback.onSuccessfulAPIResponse(response);
}
} catch (Exception e) {
callback.onFailedAPIResponse(ErrorType.EXCEPTION, e.getLocalizedMessage());
e.printStackTrace();
}
}, error -> callback.onFailedAPIResponse(ErrorType.VOLLEYERROR, error.getMessage())) {
#Override
public Map<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<>();
if (requiresAuthHeader) headers.put("Authorization", authCode);
return headers;
}
});
}
public enum ErrorType {
NOERROR, APIERROR, VOLLEYERROR, AUTHERROR, EXCEPTION
}
}
And finally the callback...
package devdroidjonno.com.tests.Networking;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
public interface GetJSONObjectRequestable {
void onSuccessfulAPIResponse(JSONObject response) throws JSONException, IOException;
void onFailedAPIResponse(VolleyRequestService.ErrorType errorType, String description);
}
I am fetch data using json and set my spinner and I am also want to add radio button in my operator spinner but radio button not visible and not working I want visible button some Ids but its not visible.
MyXML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rount_corner1"
android:layout_margin="#dimen/activity_horizontal_margin"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_marginTop="25dp"
android:inputType="number"
android:maxLength="10"
android:textColor="#000000"
android:layout_marginRight="40dp"
android:hint="Enter Mobile number"
android:textColorHint="#000000"
android:layout_marginLeft="40dp"
android:clickable="true"
android:id="#+id/prenumber"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_marginTop="7dp"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:clickable="true"
android:hint="Enter Recharge Amount"
android:textColorHint="#000000"
android:textColor="#000000"
android:maxLength="4"
android:inputType="number"
android:id="#+id/rechergpre"
/>
<Spinner
android:id="#+id/operator_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:gravity="left"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="7dp"
android:prompt="#string/appbar_scrolling_view_behavior">
</Spinner>
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="38dp"
android:visibility="gone"
>
<RadioButton
android:id="#+id/_TopUp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TopUp "
/>
<RadioButton
android:id="#+id/Special"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Special(2G,3G,SMS,VOICE )"
/>
</RadioGroup>
<Button
android:layout_width="match_parent"
android:id="#+id/prepaid_submit"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:text="Submit"
android:textStyle="bold"
android:layout_marginTop="60dp"
/>
</LinearLayout>
</RelativeLayout>
Mycode in Fragment
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
MyApplication myOpt = (MyApplication)getActivity().getApplicationContext();
protected Void doInBackground(Void... params) {
json_data = new ArrayList<Json_Data>();
datalist = new ArrayList<String>();
oprList = new ArrayList<String>();
jsonobject = JSONfunctions
.getJSONfromURL("http://www.example.com");
Log.d("Response: ", "> " + jsonobject);
try {
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Json_Data opt_code = new Json_Data();
opt_code.setName(jsonobject.optString("name"));
opt_code.setId(jsonobject.optString("ID"));
json_data.add(opt_code);
datalist.add(jsonobject.optString("name"));
oprList.add(jsonobject.getString("ID"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void args) {
final Spinner mySpinner = (Spinner)getView().findViewById(R.id.operator_spinner);
mySpinner
.setAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_dropdown_item,
datalist));
mySpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// get Operaotor cod in opt_code field
String opt_code = oprList.get(position);
String selectedItem = arg0.getItemAtPosition(position).toString();
Log.d("Selected operator is==", "======>" + selectedItem);
Log.d("Selected Value is======", "========>" + position);
Log.d("Selected ID is======", "========>" + opt_code);
if(opt_code=="8" || opt_code=="14"|| opt_code=="35"||opt_code=="36"||opt_code=="41"||opt_code=="43")
{
_RadioGroup = (RadioGroup)getView().findViewById(R.id.radioGroup);
_RadioGroup.setVisibility(View.VISIBLE);
mySpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int selectedId = _RadioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
RadioButton _RadioSex = (RadioButton)getView().findViewById(selectedId);
Toast.makeText(getActivity(),
_RadioSex.getText(), Toast.LENGTH_SHORT).show();
}
});
}
String user1 = myOpt.setOperator(opt_code);
String opt_name = myOpt.setOpt_provider(selectedItem);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
Image like:
Please help me, thanks.
Chenge xml like this
<?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" >
<Spinner
android:id="#+id/operator_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="7dp"
android:gravity="left"
android:textAlignment="center" >
</Spinner>
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:paddingLeft="38dp"
android:visibility="gone" >
<RadioButton
android:id="#+id/_TopUp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TopUp " />
<RadioButton
android:id="#+id/Special"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Special(2G,3G,SMS,VOICE )" />
</RadioGroup>
</LinearLayout>
How i can filter data using textview in Listview , data are from PHP using JSON Parser. I searched already from the net but i don't get their point because they are using array of string while Im using parsed data from php. Thanks for those who will help.
This is the code
productlist.java
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class productlist extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
private EditText et;
int textlength = 0;
private static final String URl = "http://192.168.254.101/productlist.php";
private static final String TAG_POSTS = "message";
private static final String TAG_BRAND = "Brand";
private static final String TAG_CATEGORY = "Category";
private static final String TAG_DESCRIPTION = "Description";
private static final String TAG_CODE = "Code";
private static final String TAG_QUANTITY = "Quantity";
private static final String TAG_UNIT = "Unit";
private static final String TAG_UNITPRICE = "Unitprice";
private JSONArray mComments = null;
//manages all of our comments in a list.
private ArrayList<HashMap<String, String>> mCommentList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productlist);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//loading the comments via AsyncTask
new LoadComments().execute();
}
public void updateJSONdata() {
mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(URl);
try {
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
//gets the content of each tag
String brand = c.getString(TAG_BRAND);
String category = c.getString(TAG_CATEGORY);
String description = c.getString(TAG_DESCRIPTION);
String code = c.getString(TAG_CODE);
String quantity = c.getString(TAG_QUANTITY);
String unit = c.getString(TAG_UNIT);
String unitprice = c.getString(TAG_UNITPRICE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_BRAND, brand);
map.put(TAG_CATEGORY, category);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_CODE, code);
map.put(TAG_QUANTITY, quantity);
map.put(TAG_UNIT, unit);
map.put(TAG_UNITPRICE, unitprice);
mCommentList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void updateList() {
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] { TAG_BRAND, TAG_CATEGORY,
TAG_DESCRIPTION, TAG_CODE, TAG_QUANTITY, TAG_UNIT, TAG_UNITPRICE}, new int[]{ R.id.Brand, R.id.Category,
R.id.Description, R.id.Code, R.id.Quantity, R.id.Unit, R.id.Price });
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
public class LoadComments extends AsyncTask<Void, Void, Boolean> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(productlist.this);
pDialog.setMessage("Loading Products...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
updateList();
}
}
}
productlist.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<LinearLayout
android:id="#+id/top_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/EditText01"
android:layout_gravity="center_horizontal"
android:hint="Search.." />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_layover"
android:layout_below="#+id/top_layover"
android:background="#fff"
android:divider="#android:color/transparent"
android:scrollbars="none" />
<LinearLayout
android:id="#+id/bottom_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
singlepost.xml
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#f0f0f0"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dp"
android:background="#ffffff">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Brand: ">
</TextView>
<TextView
android:id="#+id/Brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Category: " >
</TextView>
<TextView
android:id="#+id/Category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Description: " >
</TextView>
<TextView
android:id="#+id/Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Code: " >
</TextView>
<TextView
android:id="#+id/Code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Quantity: ">
</TextView>
<TextView
android:id="#+id/Quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Unit: " >
</TextView>
<TextView
android:id="#+id/Unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Price: " >
</TextView>
<TextView
android:id="#+id/Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
produclist.php
<?php
try{
$handler= new PDO('mysql:host=localhost;dbname=account','root','');
$handler->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(Exception $e){
echo $e->getMessage();
die();
}
$query =$handler->query( "Select * from tblproducts");
$records= array();
$records =$query->fetchAll(PDO::FETCH_ASSOC);
$json["message"]=$records;
echo json_encode($json);
?>
Please use below code which you will get idea and customize as your need
class CustomAdpter extends BaseAdapter implements Filterable{
LayoutInflater inflater = null;
Activity activity;
ItemFilter itemfilter = new ItemFilter();
public CustomAdpter(Activity a) {
this.activity = a;
inflater = LayoutInflater.from(a);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return listBillingSearch.size();
//return 3;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listBillingSearch.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View view1, ViewGroup parent) {
ViewHolder holder;
if (view1 == null) {
view1 = inflater.inflate(R.layout.adpter_billing, null);
holder = new ViewHolder();
holder.txtBillingListCompanyName = (TextView) view1.findViewById(R.id.txtBillingListCompanyName);
holder.txtBillingListInvoice = (TextView) view1.findViewById(R.id.txtBillingListInvoice);
holder.txtBillingListStartDate = (TextView) view1.findViewById(R.id.txtBillingListStartDate);
holder.txtBillingListEndDate = (TextView) view1.findViewById(R.id.txtBillingListEndDate);
holder.txtBillingListVoiceCalls = (TextView) view1.findViewById(R.id.txtBillingListVoiceCalls);
holder.txtBillingListSMS = (TextView) view1.findViewById(R.id.txtBillingListSMS);
holder.txtBillingListDataPlan = (TextView) view1.findViewById(R.id.txtBillingListDataPlan);
view1.setTag(holder);
}else{
holder = (ViewHolder) view1.getTag();
}
holder.txtBillingListCompanyName.setText(listBillingSearch.get(position).getStrCompanyName());
holder.txtBillingListInvoice.setText(listBillingSearch.get(position).getStrInvoice());
holder.txtBillingListStartDate.setText(listBillingSearch.get(position).getStrStartDate());
holder.txtBillingListEndDate.setText(listBillingSearch.get(position).getStrEndDate());
holder.txtBillingListVoiceCalls.setText(listBillingSearch.get(position).getStrVoiceCall());
holder.txtBillingListSMS.setText(listBillingSearch.get(position).getStrSMS());
holder.txtBillingListDataPlan.setText(" "+listBillingSearch.get(position).getStrDataPlan());
return view1;
}
#Override
public Filter getFilter() {
// TODO Auto-generated method stub
return itemfilter;
}
private class ItemFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<Billing> list = listBilling;
int count = list.size();
final ArrayList<String> nlist = new ArrayList<String>(count);
String filterableString ;
ArrayList<Billing> filterdata = new ArrayList<Billing>();
if(constraint.equals(null) || constraint.equals("")){
//filterdata = listBilling;
results.values = listBilling;
results.count = listBilling.size();
}else{
// listBillingSearch.clear();
for (int i = 0; i < count; i++) {
filterableString = list.get(i).getStrCompanyName();
if (filterableString.toLowerCase().startsWith(filterString)) {
filterdata.add(listBilling.get(i));
}
}
results.values = filterdata;
results.count = filterdata.size();
}
return results;
}
#Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
//if(results.count > 0)
listBillingSearch = (ArrayList<Billing>) results.values;
notifyDataSetChanged();
}
}
}
public static class ViewHolder {
TextView txtBillingListCompanyName;
TextView txtBillingListInvoice ;
TextView txtBillingListStartDate;
TextView txtBillingListEndDate;
TextView txtBillingListVoiceCalls;
TextView txtBillingListSMS;
TextView txtBillingListDataPlan;
}
I hope it will help you.
Let me know if you find any issue.
This is the code i just want to show the listview item in SlidingTab fragment.
Here sir i already post the single post.xml
public class Tab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.productlist,container,false);
return v;
}
}
this is for the getting the listviewitem
public class productlist extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
private static final String READ_COMMENTS_URL = "http://192.168.254.101/productlist.php";
private static final String TAG_POSTS = "message";
private static final String TAG_BRAND = "Brand";
private static final String TAG_CATEGORY = "Category";
private static final String TAG_DESCRIPTION = "Description";
private static final String TAG_CODE = "Code";
private static final String TAG_QUANTITY = "Quantity";
private static final String TAG_UNIT = "Unit";
private static final String TAG_UNITPRICE = "Unitprice";
private JSONArray mComments = null;
//manages all of our comments in a list.
private ArrayList<HashMap<String, String>> mCommentList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productlist);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
//loading the comments via AsyncTask
new LoadComments().execute();
}
public void updateJSONdata() {
mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
try {
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
//gets the content of each tag
String brand = c.getString(TAG_BRAND);
String category = c.getString(TAG_CATEGORY);
String description = c.getString(TAG_DESCRIPTION);
String code = c.getString(TAG_CODE);
String quantity = c.getString(TAG_QUANTITY);
String unit = c.getString(TAG_UNIT);
String unitprice = c.getString(TAG_UNITPRICE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_BRAND, brand);
map.put(TAG_CATEGORY, category);
map.put(TAG_DESCRIPTION, description);
map.put(TAG_CODE, code);
map.put(TAG_QUANTITY, quantity);
map.put(TAG_UNIT, unit);
map.put(TAG_UNITPRICE, unitprice);
mCommentList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void updateList() {
ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_post, new String[] { TAG_BRAND, TAG_CATEGORY,
TAG_DESCRIPTION, TAG_CODE, TAG_QUANTITY, TAG_UNIT, TAG_UNITPRICE}, new int[]{ R.id.Brand, R.id.Category,
R.id.Description, R.id.Code, R.id.Quantity, R.id.Unit, R.id.Price });
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
public class LoadComments extends AsyncTask<Void, Void, Boolean> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(productlist.this);
pDialog.setMessage("Loading Products...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;
}
#Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
pDialog.dismiss();
updateList();
}
}
}
productlist.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<LinearLayout
android:id="#+id/top_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<TextView
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Products"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_layover"
android:layout_below="#+id/top_layover"
android:background="#fff"
android:divider="#android:color/transparent"
android:scrollbars="none" />
<LinearLayout
android:id="#+id/bottom_layover"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
single_post.xml
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#f0f0f0"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dp"
android:background="#ffffff">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Brand: ">
</TextView>
<TextView
android:id="#+id/Brand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Category: " >
</TextView>
<TextView
android:id="#+id/Category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Description: " >
</TextView>
<TextView
android:id="#+id/Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Code: " >
</TextView>
<TextView
android:id="#+id/Code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Quantity: ">
</TextView>
<TextView
android:id="#+id/Quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Unit: " >
</TextView>
<TextView
android:id="#+id/Unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="5dp"
android:text="Price: " >
</TextView>
<TextView
android:id="#+id/Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#5d5d5d"
android:textStyle="bold" >
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Your tab should extend ListFragment.
public class Tab extends ListFragment{
//your existing code...
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new LoadComments().execute();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.productlist,container,false);
return v;
}
//your existing code...
}
I didn't test the code but it should work.
i have an app in which i am retrieving data from json and showing it in listview,i getting an problem only single data is getting visible in the list,i tried to figure out the problem but get nothing,where i am going wrong please help me to sort it out.In log i am getting different data but on adding it to array list getting single data repition multiple time,and no other item is visible.
BussinessActivity
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bussiness_details);
Log.i("DetailsUrl.Bussinessurl", DetailsUrl);
new JSONAsyncTask().execute(DetailsUrl);
weblink=(TextView)findViewById(R.id.txtweb);
list1 = (ListView) findViewById(R.id.list1);
list1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
//Toast.makeText(getApplication(),
//catagery.get(position).getcategory_name(),
//Toast.LENGTH_LONG).show();
}
});
}
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(BussinessDetails.this);
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
catageries = new ArrayList<ProfileDetails>();
}
#Override
protected Boolean doInBackground(String... urls) {
try {
// ------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONObject jarray = jsono.getJSONObject("business");
ProfileDetails category = new ProfileDetails();
//String business_id = jarray.getString("business_id");
//Log.i("business_id", business_id);
//String business_name = jarray.getString("business_name");
//Log.i("business_name", business_name);
String web_site = jarray.getString("web_site");
category.setData1(web_site);
category.setNumber2(web_site);
//weblink.setText(web_site);
Log.i("web_site", web_site);
//String about_business = jarray.getString("about_business");
//Log.i("web_site", web_site);
JSONObject phn = jarray.getJSONObject("Phones");
List<String> listArray = new ArrayList<String>();
Iterator iter = phn.keys();
int count=0;
while(iter.hasNext())
{
String key = (String)iter.next();
// listArray.add((String)iter.next());
Log.i("Name",key);
count +=1;
// String key = (String)iter.next();
// Object o = phn.get(key);
if( phn.get(key) instanceof JSONArray ){
JSONArray arry = phn.getJSONArray(key);
int size = arry.length();
for (int i = 0; i < size; i++) {
String CC_info_left = arry.getJSONObject(i).getString("CC_info_left");
Log.i("CC_info_left", CC_info_left);
String CC_info_right = arry.getJSONObject(i).getString("CC_info_right");
Log.i("CC_info_right", CC_info_right);
String CC_info_short_desc = arry.getJSONObject(i).getString("CC_info_short_desc");
Log.i("CC_info_short_desc", CC_info_short_desc);
String CC_info_short_desc_align = arry.getJSONObject(i).getString("CC_info_short_desc_align");
Log.i("CC_info_short_desc_align", CC_info_short_desc_align);
category.setName(key);
category.setNumber1(CC_info_right);
category.setData(CC_info_left);
category.setDescription(CC_info_short_desc);
// Log.i("category", category.toString());
catageries.add(category);
}
}
}
//Collections.sort(listArray);
// System.out.println(listArray);
JSONObject EmailAddress = jarray.getJSONObject("Emails");
Iterator iter1 = EmailAddress.keys();
int count1=0;
while(iter1.hasNext()){
String key1 = (String)iter1.next();
Log.i("Name1",key1);
count +=1;
// String key = (String)iter.next();
// Object o = phn.get(key);
if( EmailAddress.get(key1) instanceof JSONArray ){
JSONArray arry1 = EmailAddress.getJSONArray(key1);
int size1 = arry1.length();
for (int i = 0; i < size1; i++) {
String CC_info_left1 =arry1.getJSONObject(i).getString("CC_info_left");
String CC_info_right1 = arry1.getJSONObject(i).getString("CC_info_right");
String CC_info_short_desc1 = arry1.getJSONObject(i).getString("CC_info_short_desc");
String CC_info_short_desc_align1 =arry1.getJSONObject(i).getString("CC_info_short_desc_align");
category.setEmail(key1);
category.setEmailleft(CC_info_left1);
category.setEmailright(CC_info_right1);
category.setEmailDesc(CC_info_short_desc1);
catageries.add(category);
}
}
}
// /}
// }
return true;
}
// ------------------>>
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.cancel();
//adapter.notifyDataSetChanged();
if (result == false){
Toast.makeText(BussinessDetails.this,
"Unable to fetch data from server", Toast.LENGTH_LONG)
.show();
}else{
adapter = new DetailAdaptor(BussinessDetails.this, R.layout.list_details, catageries);
list1.setAdapter(adapter);
//emailAdaptor=new EmailAdaptor(BussinessDetails.this, R.layout.list_details, catagery);
//list2.setAdapter(emailAdaptor);
}
}
}
DetailAdaptor
public class DetailAdaptor extends ArrayAdapter<ProfileDetails> {
ArrayList<ProfileDetails> detailList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public DetailAdaptor(Context context, int resource, ArrayList<ProfileDetails> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
detailList = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.tvName = (TextView) v.findViewById(R.id.title_from_phone);
holder.tvData2 = (TextView) v.findViewById(R.id.data_from_phone1);
holder.tvData1 = (TextView) v.findViewById(R.id.data_from_phone2);
holder.tvDescription = (TextView) v.findViewById(R.id.data_from_phone3);
holder.email = (TextView) v.findViewById(R.id.title_for_email);
holder.email_left = (TextView) v.findViewById(R.id.email_left);
holder.email_right = (TextView) v.findViewById(R.id.email_right);
holder.emailDescription = (TextView) v.findViewById(R.id.email_desc);
v.setTag(holder);
}
else{
holder = (ViewHolder) v.getTag();
}
holder.tvName.setText(detailList.get(position).getName());
holder.tvDescription.setText(detailList.get(position).getDescription());
holder.tvData1.setText(detailList.get(position).getNumber1());
holder.tvData2.setText(detailList.get(position).getData());
holder.email.setText(detailList.get(position).getEmail());
holder.email_left.setText(detailList.get(position).getEmailleft());
holder.email_right.setText(detailList.get(position).getEmailright());
holder.emailDescription.setText(detailList.get(position).getEmailDesc());
return v;
}
static class ViewHolder {
public ImageView imageview;
public TextView tvName;
public TextView tvDescription;
public TextView tvData1;
public TextView tvData2;
public TextView email;
public TextView emailDescription;
public TextView email_left;
public TextView email_right;
}
}
ProfileDetails
public class ProfileDetails {
private String title;
private String email;
private String email_left;
private String email_right;
private String email_desc;
private String number1;
private String number2;
private String description;
private String detail1;
private String detail2;
//private String children;
//private String image;
public ProfileDetails() {
// TODO Auto-generated constructor stub
}
public ProfileDetails(String name,String num, String num2, String desc,
String data1, String data2,String emails,String emailleft ,String emailright,String emaildesc) {
super();
title = name;
description = desc;
number1 = num;
number2 = num2;
detail1 = data1;
detail2 = data2;
email =emails;
email_left=emailleft;
email_right=emailright;
email_desc=email_desc;
//this.children = children;
//this.image = image;
}
public String getName() {
return title;
}
public void setName(String name) {
title = name;
}
public String getEmail() {
return email;
}
public void setEmail(String ename) {
email = ename;
}
public String getEmailleft() {
return email_left;
}
public void setEmailleft(String Eleft) {
email_left = Eleft;
}
public String getEmailright() {
return email_right;
}
public void setEmailright(String Eright) {
email_right = Eright;
}
public String getEmailDesc() {
return email_desc;
}
public void setEmailDesc(String Edesc) {
email_desc = Edesc;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getData() {
return detail1;
}
public void setData(String data) {
detail1 = data;
}
public String getData1() {
return detail2;
}
public void setData1(String data) {
detail2 = data;
}
public String getNumber1() {
return number1;
}
public void setNumber1(String num) {
number1 = num;
}
public String getNumber2() {
return number2;
}
public void setNumber2(String num) {
number2 = num;
}
/*public String getSpouse() {
return spouse;
}
public void setSpouse(String spouse) {
this.spouse = spouse;
}
public String getChildren() {
return children;
}
public void setChildren(String children) {
this.children = children;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
*/
}
BussinessDetail.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<View
android:id="#+id/line1"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_above="#+id/rel1"
android:background="#android:color/darker_gray" />
<RelativeLayout
android:id="#+id/rel1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Start a New ChaBu"
android:textColor="#030303"
android:textSize="12dp" />
<ImageView
android:id="#+id/chatIcon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:src="#drawable/new_chabu_icon" />
</RelativeLayout>
<View
android:id="#+id/line"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/rel1"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/line"
android:layout_marginTop="5dp"
android:scrollbars="none"
android:textColor="#android:color/black"
tools:listitem="#layout/list_details" >
</ListView>
</RelativeLayout>
List_details
<?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="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/rel2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#+id/line"
android:layout_marginTop="10dp"
android:background="#E6E6E6" >
<TextView
android:id="#+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Customer Care Numbers"
android:textColor="#030303"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="#+id/line4"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/rel2"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
<RelativeLayout
android:id="#+id/txtlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/line4"
android:background="#E6E6E6" >
<TextView
android:id="#+id/title_from_phone"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:background="#E6E6E6"
android:gravity="center_vertical"
android:text=""
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="#+id/line3"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_above="#+id/rel1"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
<RelativeLayout
android:id="#+id/rel1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_below="#+id/txtlay"
android:background="#E6E6E6" >
<TextView
android:id="#+id/data_from_phone1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#E6E6E6"
android:gravity="center_vertical"
android:text="" />
<TextView
android:id="#+id/data_from_phone2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:gravity="center_vertical"
android:text="" />
<TextView
android:id="#+id/data_from_phone3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/data_from_phone1"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:text="" />
</RelativeLayout>
<View
android:id="#+id/line1"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_above="#+id/whitelay"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
<LinearLayout
android:id="#+id/whitelay"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="#+id/rel1"
android:background="#android:color/white"
android:orientation="vertical" >
</LinearLayout>
<View
android:id="#+id/line2"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/line1"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
<RelativeLayout
android:id="#+id/rel3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#+id/whitelay"
android:visibility="gone"
android:layout_marginTop="10dp"
android:background="#E6E6E6" >
<TextView
android:id="#+id/title3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Customer Care Emails"
android:textColor="#030303"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="#+id/line5"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/rel3"
android:layout_marginTop="10dp"
android:visibility="gone"
android:background="#android:color/darker_gray" />
<RelativeLayout
android:id="#+id/txtlay1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_below="#+id/line5"
android:background="#E6E6E6" >
<TextView
android:id="#+id/title_for_email"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginLeft="10dp"
android:background="#E6E6E6"
android:gravity="center_vertical"
android:text=""
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="#+id/line6"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/txtlay1"
android:visibility="gone"
android:layout_marginTop="0dp"
android:background="#android:color/darker_gray" />
<RelativeLayout
android:id="#+id/rel4"
android:layout_width="match_parent"
android:layout_height="50dp"
android:visibility="gone"
android:layout_below="#+id/line6"
android:background="#E6E6E6" >
<TextView
android:id="#+id/email_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:background="#E6E6E6"
android:gravity="center_vertical"
android:text="" />
<TextView
android:id="#+id/email_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:gravity="center_vertical"
android:text="" />
<TextView
android:id="#+id/email_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/email_left"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:text="" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rel5"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#+id/rel4"
android:visibility="gone"
android:layout_marginTop="10dp"
android:background="#E6E6E6" >
<TextView
android:id="#+id/title5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="About Us"
android:textColor="#030303"
android:textSize="15dp"
android:textStyle="bold" />
<View
android:id="#+id/line9"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/title5"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
</RelativeLayout>
<View
android:id="#+id/line11"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/rel5"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
<RelativeLayout
android:id="#+id/rel6"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#+id/line10"
android:visibility="gone"
android:layout_marginTop="10dp"
android:background="#E6E6E6" >
<TextView
android:id="#+id/about_us"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Website"
android:textColor="#030303"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="#+id/line10"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/rel6"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/rel5"
android:layout_marginLeft="10dp"
android:layout_marginTop="18dp"
android:visibility="gone"
android:text="ffdhjggjhkkghjg"
android:textColor="#030303"
android:textSize="15dp"
android:textStyle="bold" />
<View
android:id="#+id/line10"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/txt"
android:layout_marginTop="10dp"
android:visibility="gone"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/txtweb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/rel6"
android:layout_marginLeft="10dp"
android:layout_marginTop="18dp"
android:text="ffdhjggjhkkghjg"
android:visibility="gone"
android:textColor="#030303"
android:textSize="15dp"
android:textStyle="bold" />
<View
android:id="#+id/line12"
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_below="#+id/txtweb"
android:visibility="gone"
android:layout_marginTop="10dp"
android:background="#android:color/darker_gray" />
</RelativeLayout>
You create Object of category once in pre execute and then you not create new object so the category last value set in the array list everytime you create category object in loop and try it is working