Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
97 views
in Technique[技术] by (71.8m points)

How to get the specific position of item in RecyclerView 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=

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...