@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_list);
Resources res = getResources();
// contactList = new ArrayList<>();
arrayList = new ArrayList<>();
toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView=findViewById(R.id.customer_recyclerview);
/* lv = (ListView) findViewById(R.id.list);
action=(ImageView) findViewById(R.id.action);*/
// Session class instance
session = new com.example.admin.myapplication.SessionManager(getApplicationContext());
TextView username = (TextView) findViewById(R.id.username);
// Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();
/* *
* Call this function whenever you want to check user login
* This will redirect user to LoginActivity is he is not
* logged in
* */
session.checkLogin();
// get user data from session
HashMap<String, String> userProfile = session.getUserDetails();
// name
String name = userProfile.get(SessionManager.KEY_NAME);
// displaying userProfile data
username.setText(Html.fromHtml("" + name + "</b>"));
GetCustomers();
}
public void GetCustomers(){
// Showing progress dialog
pDialog = new ProgressDialog(CustomerActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
// get user data from session
HashMap<String, String> userProfile = session.getUserDetails();
String userId = userProfile.get(SessionManager.Login_ID_PREF);
StringRequest stringRequest = new StringRequest(Request.Method.GET,
url+userId,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
arrayList.clear();
Log.e("RESPONSE", response);
try {
//converting the string to json array object
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("order");
for (int i = 0; i < array.length(); i++) {
JSONObject jo = array.getJSONObject(i);
customerId= jo.getString("customer_id");
CustomerPojo pojo = new CustomerPojo();
pojo.setOrderid(jo.getString("order_id"));
pojo.setCustomername(jo.getString("customer_fullname"));
pojo.setProduct(jo.getString("order_product"));
pojo.setQuentity(jo.getString("order_quantity"));
pojo.setPaid(jo.getString("order_amount"));
arrayList.add(pojo);
SharedPreferences sp = getSharedPreferences("pref",MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("customer_id",customerId);
Log.e(TAG,"Customerid---"+customerId);
editor.apply();
Toast.makeText(CustomerActivity.this, "Customerid"+customerId, Toast.LENGTH_SHORT).show();
}
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
//Collections.reverse(orderList); display data from reverse last in first show
customerAdapter = new CustomerAdapter(context,arrayList);
recyclerView.setAdapter(customerAdapter);
recyclerView.setHasFixedSize(true);
customerAdapter.notifyDataSetChanged();
/* rvInbox.addItemDecoration(new MyDividerItemDecoration(context, LinearLayoutManager.VERTICAL,16));
rvInbox.setAdapter(inboxAdapter);*/
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// progressDialog.dismiss();
Toast.makeText(CustomerActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
switch (id) {
case R.id.profile:
Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
return true;
case R.id.logout:
session.logoutUser();
Toast.makeText(this, "logout", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public class CustomerAdapter extends RecyclerView.Adapter<CustomerAdapter.Holder> {
ArrayList<CustomerPojo> arrayList;
Context context;
public CustomerAdapter(Context context,ArrayList<CustomerPojo> arrayList){
this.arrayList=arrayList;
this.context=context;
}
@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.customer_list, parent, false);
return new Holder(view);
}
@Override
public void onBindViewHolder(@NonNull Holder holder, int i) {
CustomerPojo pojo=arrayList.get(i);
holder.txtOrderid.setText(pojo.getOrderid());
holder.txtCustomername.setText(pojo.getCustomername());
holder.txtProduct.setText(pojo.getProduct());
holder.txtQuentity.setText(pojo.getQuentity());
holder.txtPaid.setText(pojo.getPaid());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
/* Intent intent=new Intent(v.getContext(),CustomerViewActivity.class);
v.getContext().startActivity(intent);*/
final String[] listItems = {"View","Deactive","Edit","Order","Billing"};
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Choose item");
//----------------------Pass next Activity -----------------------------//
builder.setItems(listItems, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0){
/ *我将意图传递给customerId,但每次获得相同的客户ID时我都会得到* /
Intent intent=new
Intent(v.getContext(),CustomerViewActivity.class);
intent.putExtra("CustomerID",customerId);
v.getContext().startActivity(intent);
Toast.makeText(v.getContext(), "View"+customerId, Toast.LENGTH_SHORT).show();
}
else if (which == 1){
Toast.makeText(v.getContext(), "Deactive", Toast.LENGTH_SHORT).show();
}
else if (which == 2){
Intent intent=new Intent(context,CustomerEditActivity.class);
context.startActivity(intent);
}
else if (which == 3){
Intent intent=new Intent(context,CustomerOrderActivity.class);
context.startActivity(intent);
}
else if (which == 4){
Intent intent=new Intent(context,CustomerBillingActivity.class);
context.startActivity(intent);
}
//Toast.makeText(CustomerActivity.this, "Position: " + which + " Value: " + listItems[which], Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class Holder extends RecyclerView.ViewHolder{
TextView txtOrderid,txtCustomername,txtProduct,txtQuentity,txtPaid;
public Holder(View itemView) {
super(itemView);
txtOrderid=itemView.findViewById(R.id.id);
txtCustomername=itemView.findViewById(R.id.name);
txtProduct=itemView.findViewById(R.id.product);
txtQuentity=itemView.findViewById(R.id.qunty);
txtPaid=itemView.findViewById(R.id.price);
}
}
}
答案 0 :(得分:0)
代替intent.putExtra("CustomerID",customerId);
尝试intent.putExtra("CustomerID",pojo.getCustomerId()); //or whatever is the method name
答案 1 :(得分:0)
嗨,欢迎来到StackOverflow。
通过bundle / extras调用意图的另一种方法是将意图放置在RecylerViewHolder类中,您可以尝试以下方法:
public class Holder extends RecyclerView.ViewHolder {
TextView txtOrderid, txtCustomername, txtProduct, txtQuentity, txtPaid;
public Holder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(itemView.getContext(), YourActivity.class);
Bundle bundle = new Bundle();
bundle.putString("CustomerID", txtCustomerId.getText().toString().trim());
intent.putExtras(bundle);
itemView.getContext().startActivity(intent);
}
});
...
}
}
在目标活动中,我们将其称为YourActivity.class,您可以通过添加以下代码来添加以下代码以获取更多功能:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_krs_detail);
TextView output;
Bundle bundle = getIntent().getExtras();
output.setText(bundle.getString("CustomerID"));
...
}
希望这会有所帮助, 干杯。
答案 2 :(得分:0)
您要更改Intent
中的解析值,以便CustomerID从 pojo
替换
Intent intent=new Intent(v.getContext(),CustomerViewActivity.class);
intent.putExtra("CustomerID",customerId);
v.getContext().startActivity(intent);
收件人
Intent intent=new Intent(v.getContext(),CustomerViewActivity.class);
intent.putExtra("CustomerID",pojo.customerId);
v.getContext().startActivity(intent);
对于您而言,您也可以尝试
Intent intent=new Intent(v.getContext(),CustomerViewActivity.class);
intent.putExtra("CustomerID",txtOrderid,getText().toString());
v.getContext().startActivity(intent);
答案 3 :(得分:0)
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
arrayList.clear();
Log.e("RESPONSE", response);
try {
//converting the string to json array object
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("order");
for (int i = 0; i < array.length(); i++) {
JSONObject jo = array.getJSONObject(i);
customerId= jo.getString("customer_id"); // problem is here
CustomerPojo pojo = new CustomerPojo();
这里,不是存储customerId,而是在CustomerPojo中创建新字段作为customerId,然后创建getter和setter,然后在pojo中设置值
pojo.setCustomerId(jo.getString("customer_id"))
然后在bindViewHolder中
@Override
public void onBindViewHolder(@NonNull Holder holder, int i) {
CustomerPojo pojo=arrayList.get(i);
// pojo.getCustomerId(); this will return your current selected customerId
所以
builder.setItems(listItems, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0){
Intent intent=new Intent(v.getContext(),CustomerViewActivity.class);
intent.putExtra("CustomerID",pojo.getCustomerId());
这是导致错误的原因,是您在bindVeiwHolder的局部变量中设置了客户ID,因此它具有响应列表中最后一个绑定的customerId的值。