How to get the specific position of item in RecyclerView Android - android

I am using a table layout to present data in Recycler View and data is coming from a API I have used EditText and text views in the code. I use edit text so that the user could edit that particular data and send it back to the server.
My problem is that the App crashes every time when I edit the data from the edit text and post it API.
I think the problem is with the position to get the Edittext in recycler view
Please Help me I would really glad for your your help
Thanks in advance.
here is my code
Adapter class
public class PostingAdopter extends RecyclerView.Adapter<PostingAdopter.ViewHolder> {
Context context;
List<PostingModel> postingModelList;
public PostingAdopter(Context context, List<PostingModel> postingModelList) {
this.context = context;
this.postingModelList = postingModelList;
}
#NonNull
#Override
public PostingAdopter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.posting_item_ly,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull PostingAdopter.ViewHolder holder, int position) {
final PostingModel model = postingModelList.get(position);
if (postingModelList != null && postingModelList.size() > 0) {
holder.serialNo.setText(model.getSerialNo());
holder.party_name.setText(model.getPartyName());
holder.jbAmount.setText(model.getJaama_beanaam_Amount());
holder.Payment_type.setText(model.getPaymentType());
holder.remarks.setText(model.getRemarks());
holder.bill_no.setText(model.getBillno());
}
}
#Override
public int getItemCount() {
return postingModelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView serialNo,bill_no,party_name,Payment_type,remarks;
EditText jbAmount;
public ViewHolder(#NonNull View itemView) {
super(itemView);
serialNo = itemView.findViewById(R.id.serial_id_posting);
bill_no = itemView.findViewById(R.id.bill_no_posting);
party_name = itemView.findViewById(R.id.party_id_posting);
jbAmount = itemView.findViewById(R.id.jb_posting);
Payment_type = itemView.findViewById(R.id.payment_t_id);
remarks = itemView.findViewById(R.id.remark_posting);
}
}
}
Here is my ModelClass
public class PostingModel {
String SerialNo;
String jaama_beanaam_Amount;
String PartyName;
String Billno;
String PaymentType;
String Remarks;
public String getSerialNo() {
return SerialNo;
}
public void setSerialNo(String serialNo) {
SerialNo = serialNo;
}
public String getJaama_beanaam_Amount() {
return jaama_beanaam_Amount;
}
public void setJaama_beanaam_Amount(String jaama_beanaam_Amount) {
this.jaama_beanaam_Amount = jaama_beanaam_Amount;
}
public String getPartyName() {
return PartyName;
}
public void setPartyName(String partyName) {
PartyName = partyName;
}
public String getBillno() {
return Billno;
}
public void setBillno(String billno) {
Billno = billno;
}
public String getPaymentType() {
return PaymentType;
}
public void setPaymentType(String paymentType) {
PaymentType = paymentType;
}
public String getRemarks() {
return Remarks;
}
public void setRemarks(String remarks) {
Remarks = remarks;
}
public PostingModel() {
}
public PostingModel(String serialNo, String jaama_beanaam_Amount, String partyName, String billno, String paymentType, String remarks) {
SerialNo = serialNo;
this.jaama_beanaam_Amount = jaama_beanaam_Amount;
PartyName = partyName;
Billno = billno;
PaymentType = paymentType;
Remarks = remarks;
}
}
Here is my Main class
public class PostingActivity extends AppCompatActivity {
TextView time_ed,deletePosting,savePosting,postPosting;
RecyclerView recyclerView;
ImageView mirror;
EditText ed_code,paymentEditText;
RequestQueue requestQueue;
PostingAdopter adaptor;
String party_name;
String jbAmount;
String Bill_no;
int paymentid;
String paymentType;
String remarks;
JSONArray jsonArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posting);
paymentEditText = findViewById(R.id.jb_posting);
deletePosting = findViewById(R.id.deletePosting);
savePosting = findViewById(R.id.savePosting);
postPosting = findViewById(R.id.postPosting);
recyclerView = findViewById(R.id.recycler_view_for_posting);
ed_code = findViewById(R.id.ed_code_posting);
mirror = findViewById(R.id.mirror_posting);
mirror.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s = ed_code.getText().toString();
jsonData(s);
}
});
savePosting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
int npayment = Integer.parseInt(n);
;
jsonPosting(paymentid,npayment);
}
});
}
private void setRecyclerView(String response, JSONArray jsonArray) {
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adaptor = new PostingAdopter(this, setList(response, jsonArray));
recyclerView.setAdapter(adaptor);
}
private List<PostingModel> setList(String response, JSONArray jsonArray) {
try {
List<PostingModel> postingModelList= new ArrayList<>();
for (int i = 0; i <= jsonArray.length() - 1; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
party_name = jsonObject.getString("party_name");
Bill_no = jsonObject.getString("bill_no");
paymentid = jsonObject.getInt("payment_id");
paymentType = jsonObject.getString("detail");
remarks = jsonObject.getString("user_description");
jbAmount = jsonObject.getString("jaama_beanaam_Amount");
postingModelList.add(new
PostingModel(String.valueOf(i+1),jbAmount,party_name,Bill_no,paymentType,remarks));
}
return postingModelList;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
private void jsonData(String Data) {
String URL = getResources().getString(R.string.base_url) +
"/mapi/api/customer/getPostingRecords";
requestQueue = Volley.newRequestQueue(getApplicationContext());
String finalBody = "";
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("USER_CODE_NO", Data);
finalBody = jsonBody.toString();
} catch (JSONException e) {
e.printStackTrace();
}
final String requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new
Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
jsonArray = new JSONArray(response);
if (jsonArray.length() == 0) {
Toast.makeText(PostingActivity.this, "No Record Found",
Toast.LENGTH_SHORT).show();
return;
}
setRecyclerView(response, jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
//Log.v("VOLLEY", error.toString());
}
}) {
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
requestBody, "utf-8");
return null;
}
}
};
requestQueue.add(stringRequest);
}
private void jsonPosting(int paymentid,int jbAmount) {
String URL = getResources().getString(R.string.base_url) +
"/mapi/api/customer/updatePostingRecordSingle";
requestQueue = Volley.newRequestQueue(getApplicationContext());
String finalBody = "";
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("PaymentID", paymentid);
jsonBody.put("Amount",jbAmount);
finalBody = jsonBody.toString();
} catch (JSONException e) {
e.printStackTrace();
}
final String requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new
Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(PostingActivity.this, ""+response.toString(), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
//Log.v("VOLLEY", error.toString());
}
}) {
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
requestBody, "utf-8");
return null;
}
}
};
requestQueue.add(stringRequest);
}
here is xml for item(tablelayout in recyclerview)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="6">
<TableRow
android:id="#+id/tab_layout_row"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/serial_id_posting"
android:text="100"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_column="1"
android:gravity="center"
android:background="#drawable/line"
android:textSize="12sp"
android:textColor="#color/black"
android:textStyle="bold"
/>
<TextView
android:layout_width="45dp"
android:layout_height="match_parent"
android:id="#+id/bill_no_posting"
android:gravity="center"
android:text="BillNo"
android:layout_column="1"
android:lines="2"
android:textSize="12sp"
android:background="#drawable/line"
android:textColor="#color/black"
android:textStyle="bold"/>
<TextView
android:layout_width="110dp"
android:id="#+id/party_id_posting"
android:gravity="center_vertical"
android:paddingHorizontal="3dp"
android:layout_column="1"
android:layout_height="match_parent"
android:text="Party Time"
android:lines="2"
android:background="#drawable/line"
android:autoSizeMaxTextSize="12dp"
android:textSize="12sp"
android:textColor="#color/black"
android:textStyle="bold" />
<EditText
android:id="#+id/jb_posting"
android:text="Payment"
android:lines="1"
android:layout_width="65dp"
android:paddingHorizontal="3dp"
android:layout_column="1"
android:layout_height="match_parent"
android:background="#drawable/line"
android:gravity="center|right"
android:textSize="12sp"
android:textColor="#color/black"
android:textStyle="bold"
/>
<TextView
android:id="#+id/payment_t_id"
android:text="Type"
android:layout_width="71dp"
android:layout_column="1"
android:layout_height="match_parent"
android:background="#drawable/line"
android:gravity="center"
android:textSize="12sp"
android:paddingHorizontal="3dp"
android:lines="1"
android:textColor="#color/black"
android:textStyle="bold"
/>
<TextView
android:id="#+id/remark_posting"
android:text="Remarks"
android:layout_width="90dp"
android:layout_column="1"
android:layout_height="match_parent"
android:background="#drawable/line"
android:gravity="center"
android:textSize="12sp"
android:paddingHorizontal="3dp"
android:lines="1"
android:textColor="#color/black"
android:textStyle="bold"
/>
</TableRow>
XML for activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/mainpage"
android:orientation="vertical"
android:weightSum="8"
android:layout_weight="10"
tools:context=".ActiveWorker">
<FrameLayout
android:id="#+id/ly_ed"
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:src="#drawable/homebutton"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Posting"
android:autoSizeMaxTextSize="14sp"
android:autoSizeMinTextSize="12sp"
android:autoSizeTextType="uniform"
android:textStyle="bold"
android:textColor="#color/red"
android:layout_gravity="center"/>
</FrameLayout>
<LinearLayout
android:layout_below="#id/ly_ed"
android:id="#+id/linear_ly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1.5">
<EditText
android:id="#+id/ed_code_posting"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="40dp"
android:textColor="#color/red"
android:layout_weight="1"
android:inputType="number"
android:textSize="20sp"
android:background="#drawable/button_down"
android:gravity="center"
android:hint="Code"
android:textColorHint="#color/light_red" />
<ImageView
android:id="#+id/mirror_posting"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/mirror"
android:padding="5dp"
android:background="#drawable/circle_search"/>
</LinearLayout>
<LinearLayout
android:id="#+id/ly_rece"
android:layout_below="#id/linear_ly"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_marginVertical="10dp"
android:layout_marginHorizontal="8dp"
android:background="#drawable/card_view_bg"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="10dp"
android:layout_marginTop="20dp"
android:stretchColumns="1,2,3,4,5,6">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Sr.#"
android:layout_width="30dp"
android:layout_height="match_parent"
android:layout_column="1"
android:gravity="center"
android:background="#drawable/line"
android:textSize="12sp"
android:textColor="#color/black"
android:textStyle="bold"
/>
<TextView
android:layout_width="45dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="Bill No"
android:layout_column="1"
android:lines="2"
android:textSize="12sp"
android:background="#drawable/line"
android:textColor="#color/black"
android:textStyle="bold"/>
<TextView
android:layout_width="110dp"
android:gravity="center_vertical"
android:paddingHorizontal="3dp"
android:layout_column="1"
android:layout_height="match_parent"
android:text="Party Time"
android:lines="1"
android:background="#drawable/line"
android:autoSizeMaxTextSize="12dp"
android:textSize="12sp"
android:textColor="#color/black"
android:textStyle="bold" />
<TextView
android:text="Payment"
android:lines="1"
android:layout_width="65dp"
android:paddingHorizontal="3dp"
android:layout_column="1"
android:layout_height="match_parent"
android:background="#drawable/line"
android:gravity="center"
android:textSize="12sp"
android:textColor="#color/black"
android:textStyle="bold"
/>
<TextView
android:text="Type"
android:layout_width="71dp"
android:layout_column="1"
android:layout_height="match_parent"
android:background="#drawable/line"
android:gravity="center"
android:textSize="12sp"
android:paddingHorizontal="3dp"
android:lines="1"
android:textColor="#color/black"
android:textStyle="bold"
/>
<TextView
android:text="Remarks"
android:layout_width="90dp"
android:layout_column="1"
android:layout_height="match_parent"
android:background="#drawable/line"
android:gravity="center"
android:textSize="12sp"
android:paddingHorizontal="3dp"
android:lines="1"
android:textColor="#color/black"
android:textStyle="bold"
/>
</TableRow>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_for_posting"
android:layout_marginBottom="14dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/ly_rece"
android:id="#+id/send_ly"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="90dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/savePosting"
android:layout_width="120dp"
android:layout_height="90dp"
android:background="#drawable/buttonup"
android:text="Save"
android:autoSizeMaxTextSize="14sp"
android:autoSizeMinTextSize="12sp"
android:autoSizeTextType="uniform"
android:layout_gravity="center"
android:textColor="#color/red"
android:gravity="center"
android:textStyle="bold"/>
<TextView
android:id="#+id/deletePosting"
android:layout_width="120dp"
android:layout_height="90dp"
android:background="#drawable/buttonup"
android:text="Delete"
android:autoSizeMaxTextSize="14sp"
android:autoSizeMinTextSize="12sp"
android:autoSizeTextType="uniform"
android:layout_gravity="center"
android:textColor="#color/red"
android:gravity="center"
android:textStyle="bold"/>
<TextView
android:id="#+id/postPosting"
android:layout_width="120dp"
android:layout_height="90dp"
android:background="#drawable/buttonup"
android:text="Post"
android:autoSizeMaxTextSize="14sp"
android:autoSizeMinTextSize="12sp"
android:autoSizeTextType="uniform"
android:layout_gravity="center"
android:textColor="#color/red"
android:gravity="center"
android:textStyle="bold"/>
</LinearLayout>
Here is the error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 6902
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.myapplication.PostingActivity$2.onClick(PostingActivity.java:78)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I/Process: Sending signal. PID: 6902 SIG: 9

Related

I am trying to display data from server to Recycler View but getting error

I have made a recyclerview and populating it with data from server with volley, but this error keep coming back, I have tried all the solutions available but it won't work. Some help will be really appreciated.
I am using MySQL database my api for getting data from database is working fine, but recyclerview is giving this error
"android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText"
Data Class:
public class bus_data {
private int id;
private String bus_number;
private String bus_total_seats;
private String bus_available_seats;
private String bus_route;
private String bus_leaving_time;
private String bus_reaching_time;
private String bus_driver_name;
private String bus_ticketchecker_name;
private String bus_rating;
private String bus_break_time;
private String bus_company;
public bus_data(int id,String bus_number,String bus_total_seats,String bus_available_seats,
String bus_route,String bus_leaving_time,String bus_reaching_time,
String bus_driver_name,String bus_ticketchecker_name,String bus_rating,String bus_break_time,String bus_company) {
this.id = id;
this.bus_number = bus_number;
this.bus_total_seats = bus_total_seats;
this.bus_available_seats = bus_available_seats;
this.bus_route = bus_route;
this.bus_leaving_time = bus_leaving_time;
this.bus_reaching_time = bus_reaching_time;
this.bus_driver_name = bus_driver_name;
this.bus_ticketchecker_name = bus_ticketchecker_name;
this.bus_rating = bus_rating;
this.bus_break_time = bus_break_time;
this.bus_company = bus_company;
}
public int getId() {
return id;
}
public String getbus_number() {
return bus_number;
}
public String getbus_total_seats() {
return bus_total_seats;
}
public String getbus_available_seats() {
return bus_available_seats;
}
public String getbus_route() {
return bus_route;
}
public String getbus_leaving_time() {
return bus_leaving_time;
}
public String getbus_reaching_time() {
return bus_reaching_time;
}
public String getbus_driver_name() {
return bus_driver_name;
}
public String getbus_ticketchecker_name() {
return bus_ticketchecker_name;
}
public String getbus_rating() {
return bus_rating;
}
public String getbus_break_time() {
return bus_break_time;
}
public String getbus_company() {
return bus_company;
}
}
Adapter Class:
public class adapter_class extends RecyclerView.Adapter<adapter_class.bus_dataViewHolder> {
private Context mCtx;
private List<bus_data> productList;
public adapter_class(Context mCtx, List<bus_data> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
#Override
public bus_dataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.bus_detail_list, null);
return new bus_dataViewHolder(view);
}
#Override
public void onBindViewHolder(bus_dataViewHolder holder, int position) {
bus_data busDetail = productList.get(position);
//loading the image
holder.bus_number.setText(busDetail.getbus_number());
holder.total_seats.setText(busDetail.getbus_total_seats());
holder.available_seats.setText(String.valueOf(busDetail.getbus_available_seats()));
holder.bus_route.setText(String.valueOf(busDetail.getbus_route()));
holder.bus_leaving_time.setText(String.valueOf(busDetail.getbus_leaving_time()));
holder.bus_reaching_time.setText(String.valueOf(busDetail.getbus_reaching_time()));
holder.bus_driver_name.setText(String.valueOf(busDetail.getbus_driver_name()));
holder.bus_ticketchecker_name.setText(String.valueOf(busDetail.getbus_ticketchecker_name()));
holder.bus_rating.setText(String.valueOf(busDetail.getbus_rating()));
holder.bus_break_time.setText(String.valueOf(busDetail.getbus_break_time()));
holder.bus_company.setText(String.valueOf(busDetail.getbus_company()));
}
#Override
public int getItemCount() {
return productList.size();
}
class bus_dataViewHolder extends RecyclerView.ViewHolder {
EditText bus_number, total_seats, available_seats, bus_route, bus_leaving_time, bus_reaching_time,
bus_driver_name, bus_ticketchecker_name, bus_rating, bus_break_time, bus_company;
public bus_dataViewHolder(View itemView) {
super(itemView);
bus_number = itemView.findViewById(R.id.bus_number);
total_seats = itemView.findViewById(R.id.total_seats);
available_seats = itemView.findViewById(R.id.available_seats);
bus_route = itemView.findViewById(R.id.route);
bus_leaving_time = itemView.findViewById(R.id.leaving_time);
bus_reaching_time = itemView.findViewById(R.id.reaching_time);
bus_driver_name = itemView.findViewById(R.id.driver_name);
bus_ticketchecker_name = itemView.findViewById(R.id.tk_checker_name);
bus_rating = itemView.findViewById(R.id.rating);
bus_break_time = itemView.findViewById(R.id.break_time);
bus_company = itemView.findViewById(R.id.bus_company);
}
}
}
Main Class:
public class Bus_Details extends AppCompatActivity {
private static final String URL_PRODUCTS = "http://192.168.10.17/AutoBus/api.php";
//a list to store all the products
List<bus_data> productList;
//the recyclerview
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bus_details);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
recyclerView = findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//initializing the productlist
productList = new ArrayList<>();
//this method will fetch and parse json
//to display it in recyclerview
loadProducts();
}
private void loadProducts() {
/*
* Creating a String Request
* The request type is GET defined by first parameter
* The URL is defined in the second parameter
* Then we have a Response Listener and a Error Listener
* In response listener we will get the JSON response as a String
* */
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
//converting the string to json array object
JSONArray array = new JSONArray(response);
//traversing through all the object
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject data = array.getJSONObject(i);
//adding the product to product list
productList.add(new bus_data(
data.getInt("id"),
data.getString("bus_number"),
data.getString("bus_total_seats"),
data.getString("bus_available_seats"),
data.getString("bus_route"),
data.getString("bus_leaving_time"),
data.getString("bus_reaching_time"),
data.getString("bus_driver_name"),
data.getString("bus_ticketchecker_name"),
data.getString("bus_rating"),
data.getString("bus_break_time"),
data.getString("bus_company")
));
}
//creating adapter object and setting it to recyclerview
adapter_class adapter = new adapter_class(Bus_Details.this, productList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(Bus_Details.this, "Error"+e.toString(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Bus_Details.this, "Error"+error.toString(), Toast.LENGTH_SHORT).show();
}
});
//adding our stringrequest to queue
Volley.newRequestQueue(this).add(stringRequest);
}
}
Here are the layout files.
recycler_activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Passenger.Bus_Details">
<android.support.v7.widget.RecyclerView
android:id="#+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="745dp"
tools:layout_editor_absoluteY="-51dp" />
</RelativeLayout>
List_activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="#+id/layout">
<TextView
android:id="#+id/bus_company"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Daewo"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textColor="#ffffff" />
<TextView
android:id="#+id/bus_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/bus_company"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="GJN-1234"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small" />
<TextView
android:id="#+id/total_seats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/bus_number"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#color/colorPrimary"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="70"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small.Inverse"
android:textStyle="bold" />
<TextView
android:id="#+id/available_seats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/total_seats"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/leaving_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/available_seats"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/reaching_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/leaving_time"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/driver_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/reaching_time"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/tk_checker_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/driver_name"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/break_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tk_checker_name"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/break_time"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
<TextView
android:id="#+id/route"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rating"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="50"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
This is the error I am getting
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.autobus, PID: 10266
java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText
Screenshot of error:
In your bus_dataViewHolder class replace EditText to TextView.
because in layout you can use TextView and inside the program you use Edittext, that's while this issue generated.
This issue is because you take TextView in your adapter xml and get it like as a EditText.
So change in your adapter onCreateViewHolder from EditText to TextView .
Change below line in your adapter class.
TextView bus_number, total_seats, available_seats, bus_route, bus_leaving_time, bus_reaching_time,
bus_driver_name, bus_ticketchecker_name, bus_rating, bus_break_time, bus_company;

android: recycler view adapter

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);
}

RecyclerView displays only one item

Am trying to build my first app and my recyclerView only displays the first item from the json.I have tried changing the layout height to wrap_content but its still not working
Here is the xml layout for the recyclerview activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.aleky.carrental.cars">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rv"
android:clickable="false"
/>
</RelativeLayout>
</RelativeLayout>
the cardview xml layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
cardview:cardCornerRadius="2dp"
cardview:cardElevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:paddingBottom="15dp">
<TextView
android:id="#+id/car_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:fontFamily="sans-serif-smallcaps"
android:text="Car Name"
android:textColor="#996515"
android:textSize="24sp"
android:textStyle="bold" />
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/Car_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_below="#+id/car_name"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp" />
<TextView
android:id="#+id/tvfeature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="90dp"
android:layout_marginTop="25dp"
android:layout_toRightOf="#+id/car_name"
android:text="Features"
android:textColor="#000"
android:textSize="22sp" />
<View
android:layout_width="100dp"
android:layout_height="1dp"
android:layout_below="#+id/tvfeature"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/Car_image"
android:background="#000" />
<ImageView
android:id="#+id/ac"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_below="#+id/tvfeature"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/Car_image"
android:src="#drawable/air_conditioner" />
<TextView
android:id="#+id/feature1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvfeature"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/ac"
android:text="Air conditioner"
android:textColor="#000"
android:textSize="16sp" />
<ImageView
android:id="#+id/tr"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_below="#+id/feature1"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/Car_image"
android:src="#drawable/transmission" />
<TextView
android:id="#+id/feature2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/feature1"
android:layout_marginLeft="2dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/tr"
android:text="Manual"
android:textColor="#333333"
android:textSize="16sp" />
<ImageView
android:id="#+id/pass"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_below="#+id/tr"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/Car_image"
android:src="#drawable/passengers" />
<TextView
android:id="#+id/feature3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/feature2"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/pass"
android:text="6 Passengers"
android:textColor="#333333"
android:textSize="16sp" />
<TextView
android:id="#+id/pricetxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Car_image"
android:text="Price :"
android:textColor="#333333"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="#+id/car_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Car_image"
android:layout_marginLeft="2dp"
android:layout_toRightOf="#+id/pricetxt"
android:text="KSH.2000"
android:textColor="#996515"
android:textSize="22sp" />
<TextView
android:id="#+id/organiser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pricetxt"
android:layout_marginTop="5dp"
android:text="Owner :"
android:textColor="#333333"
android:textSize="14sp" />
<TextView
android:id="#+id/crAgent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/pricetxt"
android:layout_marginLeft="2dp"
android:layout_marginTop="5dp"
android:layout_toEndOf="#+id/organiser"
android:layout_toRightOf="#+id/organiser"
android:text="Supercars Rentals"
android:textColor="#05056c"
android:textSize="14sp" />
<Button
android:id="#+id/btn_rent"
style="#style/AppTheme.ButtonCorners"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/Car_image"
android:layout_marginRight="12dp"
android:clickable="true"
android:text="Rent"
android:textColor="#eef525" />
<TextView
android:id="#+id/car_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" />
</RelativeLayout>
</android.support.v7.widget.CardView>
The cars.java class to load items into the recyclerview
public class cars extends AppCompatActivity {
String url = "http://192.168.43.201/Car_rental/retrievecars.php";
Context context = null;
ProgressDialog dialog;
private List<CarsItem> array_cars = new ArrayList<>();
AdapterClass adapter;
NetworkImageView carsImageview;
private String user_Location = "cLocation";
private String Category = "category";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cars);
adapter = new AdapterClass(cars.this, array_cars);
RecyclerView recycler = (RecyclerView) findViewById(R.id.rv);
recycler.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recycler.setLayoutManager(layoutManager);
recycler.setItemAnimator(new DefaultItemAnimator());
recycler.setAdapter(adapter);
setCars();
recycler.addOnItemTouchListener(new RecyclerTouchListener(this, recycler, new ClickListener() {
#Override
public void onClick(View view, int position) {
}
#Override
public void onLongClick(View view, int position) {
}
}));
}
public void setCars() {
dialog = new ProgressDialog(this);
dialog.setMessage("Loading cars...");
dialog.show();
if (array_cars != null) {
array_cars.clear();
}
StringRequest request = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String s) {
try {
hideDialog();
Log.d("Sever Message", s);
JSONObject cars = new JSONObject(s);
JSONArray jsonArray = cars.getJSONArray("Cars");
JSONObject jobject = null;
int len = jsonArray.length();
for (int i = 0; i < len; i++) {
jobject = jsonArray.getJSONObject(i);
CarsItem carsdata = new CarsItem();
carsdata.setId(jobject.getString("0"));
carsdata.setCarname(jobject.getString("1"));
carsdata.setImages(jobject.getString("car_image"));
carsdata.setfeature1(jobject.getString("5"));
carsdata.setfeature2(jobject.getString("6"));
carsdata.setfeature3(jobject.getString("7"));
carsdata.setPrice(jobject.getString("8"));
array_cars.add(carsdata);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
//Getting Image Location & category
String Location = "";
String Ccategory = "cars";
//Creating parameters
Map<String, String> params = new Hashtable<String, String>();
//Adding parameters
params.put(Category, Ccategory);
params.put(user_Location, Location);
//returning parameters
return params;
}
};
//RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding our request to the queue
// requestQueue.add(request);
AppController.getmInstance().addToRequestQueue(request);
}
public void hideDialog() {
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
GestureDetector gestureDetector;
cars.ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final cars.ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView recycler, MotionEvent e) {
View child = recycler.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, recycler.getChildPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView recycler, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
}
and finally my adapter class
public class AdapterClass extends RecyclerView.Adapter<AdapterClass.CustomViewHolder> {
private List<CarsItem> Array_cars;
private ImageLoader imageLoader;
private Context mContext;
public AdapterClass(Context context, List<CarsItem> array_cars) {
this.Array_cars = array_cars;
this.mContext = context;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cars_design, null);
return new CustomViewHolder(view);
}
#Override
public void onBindViewHolder(CustomViewHolder holder,final int i) {
final CarsItem carsItem = Array_cars.get(i);
holder.imageUrl = carsItem.getImages();
imageLoader = AppController.getmInstance().getmImageLoader();
holder.Carname.setText(carsItem.getCarname());
holder.feature1.setText(carsItem.getfeature1());
holder.feature2.setText(carsItem.getfeature2());
holder.feature3.setText(carsItem.getfeature3());
holder.price.setText(carsItem.getPrice());
holder.agent.setText(carsItem.getcarowner());
holder.carid.setText(carsItem.getId());
holder.carimage.setImageUrl("http://192.168.43.201/Car_rental/cars/"+carsItem.getImages(), imageLoader);
holder.rent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, HireCar.class);
intent.putExtra("carname",carsItem.getCarname());
intent.putExtra("car_rate",carsItem.getPrice());
intent.putExtra("car_image", carsItem.getImages());
intent.putExtra("car_id", carsItem.getId());
intent.putExtra("c_owner", carsItem.getcarowner());
mContext.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return (null != Array_cars ? Array_cars.size() : 0);
}
class CustomViewHolder extends RecyclerView.ViewHolder {
String imageUrl;
TextView Carname, feature1, feature2, feature3, price,agent,carid;
NetworkImageView carimage;
Button rent;
public CustomViewHolder(View view) {
super(view);
this.Carname = (TextView) view.findViewById(R.id.car_name);
this.feature1 = (TextView) view.findViewById(R.id.feature1);
this.feature3 = (TextView) view.findViewById(R.id.feature3);
this.price = (TextView) view.findViewById(R.id.car_price);
this.feature2=(TextView) view.findViewById(R.id.feature2);
this.carimage = (NetworkImageView) view.findViewById(R.id.Car_image);
this.rent=(Button)view.findViewById(R.id.btn_rent);
this.agent=(TextView)view.findViewById(R.id.crAgent);
this.carid=(TextView)view.findViewById(R.id.car_id);
}
}
}
I think the problem is that you are not adding the elements to the adapter. You need to have a method in the adapter to add items after the adapter has been created. Inside that method you need to notify. Example:
void addItems(List<CarItem> carItems){
Array_cars.addAll(carItems);
notifyDataSetChanged();
}
However, I would go a bit far and find the number of items and try to notify only for the newly inserted ones(in case there is something already).
This can be done like this:
void addItems(List<CarItem> carItems){
int position = Array_cars.size();
Array_cars.addAll(carItems);
notifyItemRangeChanged(position, Array_cars.size() - position);
}
EDIT
Look at this example below:
RecycleView
Here is the xml layout for the recyclerview activity
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.aleky.carrental.cars">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rv"
android:clickable="false"
/>
</RelativeLayout>
</RelativeLayout>
You have added match_parent height of "RecyclerView" please change to wrap_content.
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.aleky.carrental.cars">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rv"
android:clickable="false"
/>
</RelativeLayout>
</RelativeLayout>

BaseAdapter returns null [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I have a fragment with a foldingcell view
here is the mainfragment -
public class FriendFragmentMain extends Fragment {
private static final String TAG = FriendFragmentMain.class.getSimpleName();
private FriendsCellListAdapter friendsCellListAdapter;
private List<FriendsItem> friendsItems;
private String URL_FRIEND="http://212.224.76.127/friends/friends.json";
private FragmentActivity fragmentActivity;
private Activity mActivity;
private ListView friendsListView;
public static FriendFragmentMain newInstance(){
FriendFragmentMain friendFragmentMain = new FriendFragmentMain();
return friendFragmentMain;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.friend_fragment_main, container, false);
friendsListView = (ListView) getActivity().findViewById(R.id.friend_super_list);
friendsItems = new ArrayList<>();
friendsCellListAdapter = new FriendsCellListAdapter(mActivity, friendsItems);
friendsListView.setAdapter(friendsCellListAdapter);
friendsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
((FoldingCell) view).toggle(false);
friendsCellListAdapter.registerToggle(pos);
}
});
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(URL_FRIEND);
if (entry != null){
try {
String data = new String(entry.data, "UTF-8");
try {
parseJsonFriend(new JSONObject(data));
} catch (JSONException e){
e.printStackTrace();
}
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
} else {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
URL_FRIEND, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
VolleyLog.d(TAG, "Response:" + response.toString());
if (response != null) {
parseJsonFriend(response);
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "ERROR:" + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(jsonObjectRequest);
}
return view;
}
private void parseJsonFriend(JSONObject response){
try {
JSONArray friendArray = response.getJSONArray("friends");
for (int i =0; i < friendArray.length(); i++){
JSONObject friendObj = (JSONObject) friendArray.get(i);
FriendsItem item = new FriendsItem();
item.setId(friendObj.getInt("id"));
item.setName(friendObj.getString("name"));
item.setProfilePic(friendObj.getString("profilePic"));
item.setBackgroundImage(friendObj.getString("backgroundImage"));
item.setStatus(friendObj.getString("status"));
item.setWork(friendObj.getString("work"));
item.setLocation(friendObj.getString("location"));
String friendUrl = friendObj.isNull("website")? null : friendObj
.getString("website");
item.setWebsite(friendUrl);
friendsItems.add(item);
}
friendsCellListAdapter.notifyDataSetChanged();
} catch (JSONException e){
e.printStackTrace();
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = activity;
}
}
the base adapter looks like this -
public class FriendsCellListAdapter extends BaseAdapter{
private HashSet<Integer> unfoldedIndexes = new HashSet<>();
private View.OnClickListener defaultMessageButton;
private View.OnClickListener defaultViewProfileButton;
private Activity activity;
private Context context;
private LayoutInflater inflater;
private List<FriendsItem> friendsItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public FriendsCellListAdapter(Activity activity, List<FriendsItem> friendsItems){
this.activity = activity;
this.friendsItems = friendsItems;
}
#Override
public int getCount() {
return friendsItems.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public Object getItem(int position) {
return friendsItems.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
if (inflater==null)
inflater = (LayoutInflater)activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FriendsItem item = friendsItems.get(position);
FoldingCell cell = (FoldingCell) convertView;
ViewHolder viewHoler;
if (cell == null){
viewHoler = new ViewHolder();
cell = (FoldingCell) inflater.inflate(R.layout.friends_cell, parent, false);
viewHoler.profilePic = (NetworkImageView) cell.findViewById(R.id.friends_profile_pic);
viewHoler.clientName = (LoginTextView) cell.findViewById(R.id.client_name);
viewHoler.friendStatus = (LoginTextView) cell.findViewById(R.id.friend_status);
viewHoler.backgroundImage = (NetworkImageView) cell.findViewById(R.id.friend_background_image);
viewHoler.friendAvatar = (NetworkImageView) cell.findViewById(R.id.friends_avatar);
viewHoler.friendName = (LoginTextView) cell.findViewById(R.id.friend_name);
viewHoler.friendLocation = (LoginTextView) cell.findViewById(R.id.friend_location);
viewHoler.friendURL = (LoginTextView) cell.findViewById(R.id.friend_url);
viewHoler.friendWork = (LoginTextView) cell.findViewById(R.id.friend_work);
cell.setTag(viewHoler);
} else {
if (unfoldedIndexes.contains(position)){
cell.unfold(true);
} else {
cell.fold(true);
}
viewHoler = (ViewHolder) cell.getTag();
}
viewHoler.clientName.setText(item.getName());
viewHoler.friendName.setText(item.getName());
//chech for empty status
if (!TextUtils.isEmpty(item.getStatus())){
viewHoler.friendStatus.setText(item.getStatus());
viewHoler.friendStatus.setVisibility(View.VISIBLE);
} else {
viewHoler.friendStatus.setVisibility(View.GONE);
}
//check for empty location
if (!TextUtils.isEmpty(item.getLocation())){
viewHoler.friendLocation.setText(item.getLocation());
viewHoler.friendLocation.setVisibility(View.VISIBLE);
} else {
viewHoler.friendLocation.setVisibility(View.GONE);
}
//check for null url
if (item.getWebsite() != null){
viewHoler.friendURL.setText(Html.fromHtml("" + item.getWebsite()+""));
viewHoler.friendURL.setMovementMethod(LinkMovementMethod.getInstance());
viewHoler.friendURL.setVisibility(View.VISIBLE);
} else {
viewHoler.friendURL.setVisibility(View.GONE);
}
//check for empty work
if (!TextUtils.isEmpty(item.getWork())){
viewHoler.friendWork.setText(item.getWork());
viewHoler.friendWork.setVisibility(View.VISIBLE);
}else {
viewHoler.friendWork.setVisibility(View.GONE);
}
//profile pic
viewHoler.profilePic.setImageUrl(item.getProfilePic(), imageLoader);
viewHoler.friendAvatar.setImageUrl(item.getProfilePic(), imageLoader);
//background image
viewHoler.backgroundImage.setImageUrl(item.getBackgroundImage(), imageLoader);
return convertView;
}
public void registerToggle(int position){
if (unfoldedIndexes.contains(position))
registerFold(position);
else
registerUnfold(position);
}
public void registerFold(int position){unfoldedIndexes.remove(position);}
public void registerUnfold(int position){
unfoldedIndexes.add(position);
}
private class ViewHolder{
NetworkImageView profilePic;
LoginTextView clientName;
LoginTextView friendStatus;
NetworkImageView backgroundImage;
NetworkImageView friendAvatar;
LoginTextView friendName;
LoginTextView friendLocation;
LoginTextView friendURL;
LoginTextView friendWork;
}
}
now when i run it it throws -
Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.evolustudios.askin.askin.src.fragments.FriendFragmentMain.onCreateView(FriendFragmentMain.java:60)
this error. i am unable to find out why its throwing a null object? can anyone throw a light on why its showing null object.
the views are here- friends_cell
<com.ramotion.foldingcell.FoldingCell xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
xmlns:folding-cell="http://schemas.android.com/apk/res-auto"
folding-cell:additionalFlipsCount="2"
folding-cell:animationDuration="1300"
folding-cell:backSideColor="#color/bgBackSideColor">
<include layout="#layout/friends_content_layout"/>
<include layout="#layout/friends_cell_title_layout"/>
</com.ramotion.foldingcell.FoldingCell>
friends_cell_title_layout -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false">
<!--LEFT Part -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="155dp"
android:layout_weight="3"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="20dp"
android:background="#color/feed_item_border">
<com.android.volley.toolbox.NetworkImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/friends_profile_pic"
android:scaleType="fitCenter"/>
</RelativeLayout>
<!--RIGHT Part-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingBottom="20dp"
android:paddingEnd="20dp"
android:paddingStart="15dp"
android:paddingTop="20dp"
android:background="#color/feed_item_border">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:id="#+id/title_from_to_dots"
android:layout_marginEnd="10dp"
android:src="#drawable/from_to_purple"/>
<com.evolustudios.askin.askin.src.customfonts.LoginTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/client_name"
android:layout_alignTop="#+id/title_from_to_dots"
android:layout_marginTop="-5dp"
android:layout_toEndOf="#+id/title_from_to_dots"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:singleLine="true"
android:textColor="#android:color/holo_blue_dark"
android:textSize="16sp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="#+id/friend_profile_divider"
android:layout_below="#+id/client_name"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_toEndOf="#+id/title_from_to_dots"
android:src="#color/contentDividerLine"/>
<com.evolustudios.askin.askin.src.customfonts.LoginTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/friend_status"
android:layout_below="#+id/friend_profile_divider"
android:layout_toEndOf="#+id/title_from_to_dots"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:singleLine="true"
android:textColor="#android:color/holo_blue_dark"
android:textSize="16sp"/>
</RelativeLayout>
and friend_content_layout -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<!--Content header line-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/text_password"
android:paddingBottom="7dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:paddingTop="7dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:src="#drawable/menu_icon"/>
</RelativeLayout>
<!--Content header Image-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.android.volley.toolbox.NetworkImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/friend_background_image"
android:scaleType="centerCrop"/>
</RelativeLayout>
<!--Content body layout-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="6dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="9dp">
<!--avatar and name-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<com.android.volley.toolbox.NetworkImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/friends_avatar"
android:layout_alignParentStart="true"
android:layout_marginBottom="5dp"/>
<com.evolustudios.askin.askin.src.customfonts.LoginTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/friend_name"
android:layout_alignTop="#+id/friends_avatar"
android:layout_marginBottom="2dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#+id/friends_avatar"
android:textColor="#color/mainTextColor"
android:textSize="18sp"
android:textStyle="bold"/>
<com.evolustudios.askin.askin.src.customfonts.LoginTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/friend_location"
android:layout_alignStart="#+id/friend_name"
android:layout_marginBottom="-2dp"
android:layout_marginStart="3dp"
android:layout_below="#+id/friend_name"
android:textColor="#a9a9a9"
android:textSize="12sp"/>
</RelativeLayout>
<!--Divider Line-->
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="6dp"
android:layout_marginTop="9dp"
android:src="#color/contentDividerLine"/>
<!--Address Part-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.evolustudios.askin.askin.src.customfonts.LoginTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/url_badge"
android:textColor="#a9a9a9"
android:layout_alignParentStart="true"
android:text="WEBSITE"/>
<com.evolustudios.askin.askin.src.customfonts.LoginTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/friend_url"
android:layout_alignStart="#+id/url_badge"
android:layout_below="#+id/url_badge"
android:textColor="#color/mainTextColor"
android:textSize="18sp"
android:textStyle="bold"/>
</RelativeLayout>
</LinearLayout>
<!--Divider Line-->
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="6dp"
android:layout_marginTop="7dp"
android:src="#color/contentDividerLine"/>
<!--Work Part-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.evolustudios.askin.askin.src.customfonts.LoginTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/work_badge"
android:textColor="#a9a9a9"
android:layout_alignParentStart="true"
android:text="WORK"/>
<com.evolustudios.askin.askin.src.customfonts.LoginTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/friend_work"
android:layout_alignStart="#+id/work_badge"
android:layout_below="#+id/work_badge"
android:textColor="#color/mainTextColor"
android:textSize="18sp"
android:textStyle="bold"/>
</RelativeLayout>
</LinearLayout>
<!--Buttons-->
<com.evolustudios.askin.askin.src.customfonts.LoginTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/friend_send_message"
android:layout_marginTop="16dp"
android:background="#color/btnRequest"
android:padding="10dp"
android:text="Send Message"
android:textAlignment="center"
android:textColor="#color/mainTextColor"
android:textSize="20sp"/>
<com.evolustudios.askin.askin.src.customfonts.LoginTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/friend_view_profile"
android:layout_marginTop="16dp"
android:background="#color/btnRequest"
android:padding="10dp"
android:text="View Profile"
android:textAlignment="center"
android:textColor="#color/mainTextColor"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
Initialize friendsItems before passing it to friendsCellListAdapter
friendsItems = new ArrayList<FriendsItem>(); // add this line
friendsCellListAdapter = new FriendsCellListAdapter(mActivity, friendsItems);
friendsListView.setAdapter(friendsCellListAdapter);

single data is getting repeating 18 times in listview

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

Categories

Resources