package com.isl.incident;
import android.app.Activity; import infozech.itower.R;
import com.isl.SingleSelectSearchable.SearchableSpinner; import com.isl.dao.cache.AppPreferences; import com.isl.constant.WebMethods; import com.isl.dao.DataBaseHelper; import com.isl.modal.BeanSearchTktList; import com.isl.modal.BeanSearchTktRptList; import com.isl.util.Utils;
import java.text.DateFormatSymbols; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.Locale;
import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import android.app.Activity; import android.app.DatePickerDialog; import android.app.ProgressDialog; import android.app.TimePickerDialog; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.text.Html; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import com.google.gson.Gson; //after added import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap;
import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response;
//
public class SalesClass extends Activity { AppPreferences mAppPreferences; Button bt_back; Button btn_get_data; DataBaseHelper db = null; String moduleUrl = ""; TextView tv_brand_logo; LinearLayout rl1;
Spinner sp_txn_date; EditText et_productid; //after added private RecyclerView recyclerView; private SalesDataAdapter adapter; LinearLayout rl; private boolean isDataDisplayed = false; List<String> monthsList = new ArrayList<>(); ArrayAdapter<String> spinnerAdapter; // Declared a variable to store the initial list of sales data private List<SalesData> initialSalesData = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAppPreferences = new AppPreferences(getApplicationContext()); db = new DataBaseHelper(getApplicationContext()); db.open(); setContentView(R.layout.sales_class); getControllerId(); if (mAppPreferences.getTTModuleSelection().equalsIgnoreCase("955")) { moduleUrl = db.getModuleIP("HealthSafty"); } else { moduleUrl = db.getModuleIP("Incident"); } bt_back.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { finish(); } }); // recyclerView = (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); // spinnerAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, monthsList); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp_txn_date.setAdapter(spinnerAdapter); //added adapter = new SalesDataAdapter(new ArrayList<>()); recyclerView.setAdapter(adapter); //
// fetchDataFromApi(); fetchDataFromSpinner();
btn_get_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String productId = et_productid.getText().toString(); String selectedMonth = sp_txn_date.getSelectedItem().toString(); List<SalesData> filteredData = filterData(initialSalesData, productId);
// if (!selectedMonth.equals("Select Month Value")) {
// List filteredData = filterData(initialSalesData, productId);
updateRecyclerView(filteredData);
// filteredDataFromApi(productId, selectedMonth); // } else { // Toast.makeText(SalesClass.this, "Please select a valid month", Toast.LENGTH_SHORT).show(); // }
} }); }
// private void filteredDataFromApi(String productId, String selectedMonth) { // ApiService apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class); // Call call = apiService.getSalesData(productId,selectedMonth); // call.enqueue(new Callback() { // @Override // public void onResponse(Call call, Response response) { // if (response.isSuccessful()) { // GetData data = response.body(); // if (data != null && data.getSalesList() != null) { // List filteredData = filterByMonth(data.getSalesList(), selectedMonth); // updateRecyclerView(filteredData); // updateRecyclerView(data.getSalesList()); // } // } else { // Toast.makeText(SalesClass.this, "Failed to fetch data from the API", Toast.LENGTH_SHORT).show(); // } // } // // @Override // public void onFailure(Call call, Throwable t) { // // Toast.makeText(SalesClass.this, "Error fetching data: " + t.getMessage(), Toast.LENGTH_SHORT).show(); // } // }); // } private List filterData(List salesList, String productId){ // private List filterData(List salesList, String productId, String selectedMonth) { List filteredData = new ArrayList<>(); for (SalesData data : salesList) { // if (data.getP_ID() == Integer.parseInt(productId) && data.getTXN_DATE().equals(selectedMonth)) { if (data.getP_ID() == Integer.parseInt(productId)) { filteredData.add(data); } else{ int a=1;
} } return filteredData; } private void updateRecyclerView(List<SalesData> salesList) { adapter.setData(salesList); adapter.notifyDataSetChanged(); } private void fetchDataFromSpinner(){ ApiService apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class); Call<GetData> call = apiService.getSalesData(et_productid.getText().toString(), ""); call.enqueue(new Callback<GetData>() { @Override public void onResponse(Call<GetData> call, Response<GetData> response) { if (response.isSuccessful()) { GetData data = response.body(); if (data != null && data.getSalesList() != null) { initialSalesData = data.getSalesList(); updateSpinnerWithMonths(data.getSalesList()); } } else { Toast.makeText(SalesClass.this, "Failed to fetch data from the API", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(Call<GetData> call, Throwable t) { // Handle network or other errors Toast.makeText(SalesClass.this, "Error fetching data: " + t.getMessage(), Toast.LENGTH_SHORT).show(); } }); }
// private void fetchDataFromApi() { // ApiService apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class); // Call call = apiService.getSalesData(et_productid.getText().toString(), ""); // call.enqueue(new Callback() { // @Override // public void onResponse(Call call, Response response) { // if (response.isSuccessful()) { // GetData data = response.body(); // if (data != null && data.getSalesList() != null) { // // Clear previous data // adapter.setData(data.getSalesList()); // isDataDisplayed = true; // // } // } else { // // Toast.makeText(SalesClass.this, "Failed to fetch data from the API", Toast.LENGTH_SHORT).show(); // } // } // // @Override // public void onFailure(Call call, Throwable t) { // // Handle network or other errors // Toast.makeText(SalesClass.this, "Error fetching data: " + t.getMessage(), Toast.LENGTH_SHORT).show(); // } // }); // }
private void updateSpinnerWithMonths(List<SalesData> salesList) { monthsList.clear(); // Add the "Select month" item as the default selection String defaultSelection = "Select Month Value"; monthsList.add(defaultSelection); // Extract and add only the months from TXN_DATE in the response to the monthsList for (SalesData salesData : salesList) { String txnDate = salesData.getTXN_DATE(); String month = extractMonthFromTxnDate(txnDate); if (!monthsList.contains(month)) { monthsList.add(month); } } for (String month : monthsList) { String TAG="MonthList"; Log.d("TAG", "Month: " + month); } // Sort months to get in ascending order Collections.sort(monthsList, new Comparator<String>() { DateFormatSymbols dfs = new DateFormatSymbols(); @Override public int compare(String month1, String month2) { int num1 = Arrays.asList(dfs.getMonths()).indexOf(month1); int num2 = Arrays.asList(dfs.getMonths()).indexOf(month2); if (num1 == -1 || num2 == -1) { return month1.compareTo(month2); // In case month name is not found, compare based on string value } return Integer.compare(num1, num2); } }); spinnerAdapter.notifyDataSetChanged(); // Set the default selection int defaultSelectionIndex = monthsList.indexOf(defaultSelection); if (defaultSelectionIndex != -1) { sp_txn_date.setSelection(defaultSelectionIndex); } } private String extractMonthFromTxnDate(String txnDate) { // Assuming txnDate is in the format "yyyy-MM-dd", extract the month String[] parts = txnDate.split("-"); if (parts.length >= 2) { return parts[1]; // Return the month part } return ""; } public void setMsg() { if (mAppPreferences.getTTModuleSelection().equalsIgnoreCase("955")) { Utils.msgText(SalesClass.this, "528", tv_brand_logo); Utils.msgButton(SalesClass.this, "71", bt_back); } else { Utils.msgText(SalesClass.this, "75", tv_brand_logo); Utils.msgText(SalesClass.this, "75", tv_brand_logo); Utils.msgButton(SalesClass.this, "71", bt_back); } } @Override public void onBackPressed() { finish(); } public void getControllerId() { tv_brand_logo = (TextView) findViewById(R.id.tv_brand_logo); bt_back = (Button) findViewById(R.id.button_back); btn_get_data = (Button) findViewById(R.id.btn_sales_data); et_productid= (EditText) findViewById(R.id.et_productid); sp_txn_date = (Spinner) findViewById(R.id.sp_txn_date); rl = (LinearLayout) findViewById(R.id.rl_submit); }
}, in this line of code :private List filterData(List salesList, String productId){ // private List filterData(List salesList, String productId, String selectedMonth) { List filteredData = new ArrayList<>(); for (SalesData data : salesList) { // if (data.getP_ID() == Integer.parseInt(productId) && data.getTXN_DATE().equals(selectedMonth)) { if (data.getP_ID() == Integer.parseInt(productId)) { filteredData.add(data); } else{ int a=1;
}, } return filteredData; }, in this code if (data.getP_ID() == Integer.parseInt(productId)) { filteredData.add(data); }when i use use this condition it takes the data from api sequencially i want that it when i take the value from edittext then it check the value from api, if it is present in the api then we get the same all value else data is not present in api
It seems that you want to filter the sales data based on the product ID entered by the user. If the product ID matches any of the entries in the API response, you want to display the corresponding data; otherwise, you want to indicate that the data is not present in the API.
To achieve this, you can modify the filterData
method as follows:
private List<SalesData> filterData(List<SalesData> salesList, String productId) { List<SalesData> filteredData = new ArrayList<>(); boolean isDataFound = false; for (SalesData data : salesList) { if (data.getP_ID() == Integer.parseInt(productId)) { filteredData.add(data); isDataFound = true; } } if (!isDataFound) { // Handle the case when data is not present in the API for the entered product ID // You can display a message or take appropriate action here } return filteredData; }
In this modified method, we iterate through the salesList
and add the matching SalesData
objects to the filteredData
list. Additionally, we introduce a boolean flag isDataFound
to track if any data has been found for the entered product ID. If no data is found, you can handle this scenario as per your requirements, such as displaying a message or taking any other appropriate action.
when i enter the value lets suppose 1 , it goes into the first object of api not checking in the whole api
To address this issue, you can modify the filterData method to only add the data to the filtered list if the product ID matches the value entered in the EditText. Here's the modified filterData method:
private List<SalesData> filterData(List<SalesData> salesList, String productId) { List<SalesData> filteredData = new ArrayList<>(); for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).equals(productId)) { filteredData.add(data); } } return filteredData}
This modification checks if the product ID from the API matches the value entered in the EditText, and only adds the matching data to the filtered list.
Please ensure to replace the existing filterData method with this modified version in your SalesClass.
not giving the output i want that when i give the value of Product ID edittext ,then it check the whole P_ID value in the api, if it present it sends all the similar value in the recycler view
To achieve the functionality where the entered Product ID is compared with the P_ID values of the data retrieved from the API, and if a match is found, the matching data is displayed in the RecyclerView, the filterData
method needs to be modified. Here's a revised implementation:
private List<SalesData> filterData(List<SalesData> salesList, String productId) { List<SalesData> filteredData = new ArrayList<>(); for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).contains(productId)) { filteredData.add(data); } } return filteredData; }
In this updated method, I'm using the contains
method to check if the P_ID
contains the entered productId
. This will filter the data based on whether the entered productId
is contained within the P_ID
.
After making this change, the onClick
method of btn_get_data
can be simplified as follows:
btn_get_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String productId = et_productid.getText().toString(); List<SalesData> filteredData = filterData(initialSalesData, productId); updateRecyclerView(filteredData); } });
This revised code will compare the entered Product ID with the P_ID values from the API data, and if a match is found, it will display the corresponding data in the RecyclerView.
not giving the output
It seems that the current condition for filtering the sales data within the filterData
method is not producing the expected output. To address this, we need to clarify a few points:
The current code applies a filter to the sales data based on the product ID only. However, you mentioned that you want to filter the data based on the value from the EditText
and ensure that the data is present in the API response. Could you confirm if the product ID is the value from the EditText
or if there is another value being used for comparison?
Additionally, is the issue related to the filter not working as expected, or is it about the data not being present in the API response?
Once I have a clearer understanding of the specific scenario and requirements, I can provide guidance on adjusting the code accordingly.
write the code to confirm that product ID is the value from the EditText
To confirm that the product ID is the value from the EditText in Android, you can use the following code snippet.
Assuming you have an EditText with the ID "et_productId" and a Button with the ID "btn_get_data", you can add an OnClickListener to the Button to retrieve the value from the EditText and use it as the product ID.
Here's a sample code snippet to achieve this:
EditText et_productId = findViewById(R.id.et_productId); Button btn_get_data = findViewById(R.id.btn_get_data); RecyclerView recyclerView = findViewById(R.id.recyclerView); btn_get_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String productId et_productId.getText().toString(); // Call a method to fetch data from the API based on productId and update the RecyclerView fetchDataAndUpdateRecyclerView(productId); } }); private void fetchDataAndUpdateRecyclerView(String productId) { // Assuming you have a method to fetch data from the API based on the productId // Replace this with your actual method to fetch data // Sample code to fetch data based on productId and update the RecyclerView List<SalesData> filteredData = filterData(initialSalesData, productId); updateRecyclerView(filteredData); } private List<SalesData> filterData(List<SalesData> salesList, String productId) { List<SalesData> filteredData = new ArrayList<>(); for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).equals(productId)) { filteredData.add(data); } } return filteredData; } private void updateRecyclerView(List<SalesData> salesDataList) { // Update the RecyclerView with the filtered sales data // For example, if you are using an adapter, you can update the adapter dataset here YourAdapterClass adapter = new YourAdapterClass(salesDataList); recyclerView.setAdapter(adapter); }
In this code snippet, when the Button "btn_get_data" is clicked, it retrieves the text from the EditText "et_productId" and uses it as the product ID to fetch and display the filtered data in the RecyclerView.
i have write the same code still not geting the output
I understand that you're still experiencing issues despite implementing the code. To further assist you, I'd need a bit more information about the problem you're encountering.
Could you provide specific details about the behavior you are observing? For example, are you seeing an error message, is the RecyclerView not updating, or is the filtering not working as expected? Any additional context you can provide will help me assist you better.
filtering not working as expected
It appears that the filtering logic within the filterData
method might not be working correctly. Let's make sure the comparison between the product ID and the data from the API is correctly implemented. We can tweak the filtering logic to ensure it works as expected.
Here's the modified filterData
method to directly compare the product ID entered in the EditText with the P_ID
of the sales data:
private List<SalesData> filterData(List<SalesData> salesList, String productId) { List<SalesData> filteredData = new ArrayList<>(); for (SalesData data : salesList) { if (data.getP_ID() == Integer.parseInt(productId)) { filteredData.add(data); } } return filteredData; }
In this modified method, we are comparing the integer value P_ID
with the integer value obtained from parsing the productId
entered in the EditText. This should ensure a direct and accurate comparison.
After making this change, test the behavior to see if the filtering now works as expected. If there are still issues, please provide additional details so that I can further assist you.
i check this logic earlier not getting the output
I understand the frustration. Let's troubleshoot the issue further. It's possible that the comparison might not be working as expected due to data type or formatting issues. Here's an additional adjustment to the filtering logic:
private List<SalesData> filterData(List<SalesData> salesList, String productId) { List<SalesData> filteredData = new ArrayList<>(); for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).equals(productId.trim())) { filteredData.add(data); } } return filteredData; }
In this modification, I've added the trim()
method to remove any leading or trailing white spaces from the entered productId
in case there are any. This can help ensure that the comparison is accurate and that there are no unintended whitespace characters causing mismatches.
After implementing this change, please test the code again to see if the filtering produces the expected results. If it still doesn't work as intended, please provide additional information so I can assist you further.
(String.valueOf(data.getP_ID()) explain this logic
The logic String.valueOf(data.getP_ID())
is used to convert the numerical value of P_ID
to a string. This is often necessary when working with user input, such as comparing a numeric ID entered in an EditText (which is represented as a string) with the integer value of P_ID
from the data retrieved from the API.
Let's break down this logic further:
data.getP_ID()
: This retrieves the integer value of the P_ID
attribute from the SalesData
object data
.
String.valueOf(data.getP_ID())
: This part then converts the integer value obtained from data.getP_ID()
to its string representation. Essentially, it converts the numeric P_ID
to a string so that it can be compared with the user input (which is also a string).
The purpose of this logic is to ensure that both values being compared are of the same data type, which is crucial for accurate comparison when dealing with user input and data retrieved from an API.
If you have additional questions or need further clarification, feel free to ask!
private List filterData(List salesList, String productId){ // private List filterData(List salesList, String productId, String selectedMonth) { List filteredData = new ArrayList<>(); for (SalesData data : salesList) { // if (data.getP_ID() == Integer.parseInt(productId) && data.getTXN_DATE().equals(selectedMonth)) { if (String.valueOf(data.getP_ID()).equals(Integer.parseInt(productId.trim()))) { filteredData.add(data); } else{ int a=1;
} } return filteredData; } make chnages in the code to check the P_ID value in the whole api , when i give the value in the edit text
It looks like you want to filter the SalesData
list based on the P_ID
value when a particular product ID is entered in the EditText. Below is the modified filterData
method to achieve this:
private List<SalesData> filterData(List<SalesData> salesList, String productId) { List<SalesData> filteredData = new ArrayList<>(); forSalesData data : salesList) { if (String.valueOf(data.getP_ID()).equals(productId.trim())) { filteredData.add(data); } } return filteredData; }
In this modified method, we are comparing the string value of P_ID
with the string value of the entered productId
. This update ensures that the P_ID
values are compared correctly and the data is filtered based on the entered product ID.
I have removed the Integer.parseInt
conversion as it's not necessary when comparing string representations. If P_ID
is already an integer, you can simply use String.valueOf(data.getP_ID())
for comparison with the entered productId
.
Please replace existing filterData
method with this modification, and test the behavior again to see if the filtering now produces the expected results. If you encounter any issues or need further assistance, feel free to ask!
String.valueOf(data.getP_ID()) this gives the data that present in the api at the topmost of value P_ID, i want to check the P_ID value in the whole api of P_ID value.
I see, it sounds like you want to filter the entire SalesData
list based on the P_ID
value entered in the EditText. To achieve this, you can modify the filterData
method as follows:
private List<SalesData> filterData(List<SalesData> salesList, String productId) { List<SalesData> filteredData = new ArrayList<>(); for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).contains(productId)) { filteredData.add(data); } } return filteredData; }
In this modified method, I've used the contains
method to check if the string representation of the P_ID
contains the entered productId
. This should filter the data based on whether the entered productId
is contained within the P_ID
.
With this implementation, the filtering logic will compare the entered Product ID string with the string representations of all the P_ID values present in the SalesData
list.
Please replace the existing filterData
method with this modification, and test the behavior to see if the filtering now produces the expected results. If you have further questions or need additional assistance, feel free to ask!
package com.isl.incident;
import android.app.Activity; import infozech.itower.R;
import com.isl.SingleSelectSearchable.SearchableSpinner; import com.isl.dao.cache.AppPreferences; import com.isl.constant.WebMethods; import com.isl.dao.DataBaseHelper; import com.isl.modal.BeanSearchTktList; import com.isl.modal.BeanSearchTktRptList; import com.isl.util.Utils;
import java.text.DateFormatSymbols; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.Locale;
import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import android.app.Activity; import android.app.DatePickerDialog; import android.app.ProgressDialog; import android.app.TimePickerDialog; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.text.Html; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import com.google.gson.Gson; //after added import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap;
import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response;
//
public class SalesClass extends Activity { AppPreferences mAppPreferences; Button bt_back; Button btn_get_data; DataBaseHelper db = null; String moduleUrl = ""; TextView tv_brand_logo; LinearLayout rl1;
Spinner sp_txn_date; EditText et_productid; //after added private RecyclerView recyclerView; private SalesDataAdapter adapter; LinearLayout rl; private boolean isDataDisplayed = false; List<String> monthsList = new ArrayList<>(); ArrayAdapter<String> spinnerAdapter; // Declared a variable to store the initial list of sales data private List<SalesData> initialSalesData = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mAppPreferences = new AppPreferences(getApplicationContext()); db = new DataBaseHelper(getApplicationContext()); db.open(); setContentView(R.layout.sales_class); getControllerId(); if (mAppPreferences.getTTModuleSelection().equalsIgnoreCase("955")) { moduleUrl = db.getModuleIP("HealthSafty"); } else { moduleUrl = db.getModuleIP("Incident"); } bt_back.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { finish(); } }); // recyclerView = (RecyclerView) findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); // spinnerAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, monthsList); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp_txn_date.setAdapter(spinnerAdapter); //added adapter = new SalesDataAdapter(new ArrayList<>()); recyclerView.setAdapter(adapter); //
// fetchDataFromApi(); fetchDataFromSpinner();
btn_get_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String productId = et_productid.getText().toString(); String selectedMonth = sp_txn_date.getSelectedItem().toString(); List<SalesData> filteredData = filterData(initialSalesData, productId);
// if (!selectedMonth.equals("Select Month Value")) {
// List filteredData = filterData(initialSalesData, productId);
updateRecyclerView(filteredData);
// filteredDataFromApi(productId, selectedMonth); // } else { // Toast.makeText(SalesClass.this, "Please select a valid month", Toast.LENGTH_SHORT).show(); // }
} }); }
// private void filteredDataFromApi(String productId, String selectedMonth) { // ApiService apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class); // Call call = apiService.getSalesData(productId,selectedMonth); // call.enqueue(new Callback() { // @Override // public void onResponse(Call call, Response response) { // if (response.isSuccessful()) { // GetData data = response.body(); // if (data != null && data.getSalesList() != null) { // List filteredData = filterByMonth(data.getSalesList(), selectedMonth); // updateRecyclerView(filteredData); // updateRecyclerView(data.getSalesList()); // } // } else { // Toast.makeText(SalesClass.this, "Failed to fetch data from the API", Toast.LENGTH_SHORT).show(); // } // } // // @Override // public void onFailure(Call call, Throwable t) { // // Toast.makeText(SalesClass.this, "Error fetching data: " + t.getMessage(), Toast.LENGTH_SHORT).show(); // } // }); // } private List filterData(List salesList, String productId){ // private List filterData(List salesList, String productId, String selectedMonth) { List filteredData = new ArrayList<>(); for (SalesData data : salesList) { // if (data.getP_ID() == Integer.parseInt(productId) && data.getTXN_DATE().equals(selectedMonth)) { if (data.getP_ID() == Integer.parseInt(productId)) { filteredData.add(data); } else{ int a=1;
} } return filteredData; } private void updateRecyclerView(List<SalesData> salesList) { adapter.setData(salesList); adapter.notifyDataSetChanged(); } private void fetchDataFromSpinner(){ ApiService apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class); Call<GetData> call = apiService.getSalesData(et_productid.getText().toString(), ""); call.enqueue(new Callback<GetData>() { @Override public void onResponse(Call<GetData> call, Response<GetData> response) { if (response.isSuccessful()) { GetData data = response.body(); if (data != null && data.getSalesList() != null) { initialSalesData = data.getSalesList(); updateSpinnerWithMonths(data.getSalesList()); } } else { Toast.makeText(SalesClass.this, "Failed to fetch data from the API", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(Call<GetData> call, Throwable t) { // Handle network or other errors Toast.makeText(SalesClass.this, "Error fetching data: " + t.getMessage(), Toast.LENGTH_SHORT).show(); } }); }
// private void fetchDataFromApi() { // ApiService apiService = RetrofitClient.getRetrofitInstance().create(ApiService.class); // Call call = apiService.getSalesData(et_productid.getText().toString(), ""); // call.enqueue(new Callback() { // @Override // public void onResponse(Call call, Response response) { // if (response.isSuccessful()) { // GetData data = response.body(); // if (data != null && data.getSalesList() != null) { // // Clear previous data // adapter.setData(data.getSalesList()); // isDataDisplayed = true; // // } // } else { // // Toast.makeText(SalesClass.this, "Failed to fetch data from the API", Toast.LENGTH_SHORT).show(); // } // } // // @Override // public void onFailure(Call call, Throwable t) { // // Handle network or other errors // Toast.makeText(SalesClass.this, "Error fetching data: " + t.getMessage(), Toast.LENGTH_SHORT).show(); // } // }); // }
private void updateSpinnerWithMonths(List<SalesData> salesList) { monthsList.clear(); // Add the "Select month" item as the default selection String defaultSelection = "Select Month Value"; monthsList.add(defaultSelection); // Extract and add only the months from TXN_DATE in the response to the monthsList for (SalesData salesData : salesList) { String txnDate = salesData.getTXN_DATE(); String month = extractMonthFromTxnDate(txnDate); if (!monthsList.contains(month)) { monthsList.add(month); } } for (String month : monthsList) { String TAG="MonthList"; Log.d("TAG", "Month: " + month); } // Sort months to get in ascending order Collections.sort(monthsList, new Comparator<String>() { DateFormatSymbols dfs = new DateFormatSymbols(); @Override public int compare(String month1, String month2) { int num1 = Arrays.asList(dfs.getMonths()).indexOf(month1); int num2 = Arrays.asList(dfs.getMonths()).indexOf(month2); if (num1 == -1 || num2 == -1) { return month1.compareTo(month2); // In case month name is not found, compare based on string value } return Integer.compare(num1, num2); } }); spinnerAdapter.notifyDataSetChanged(); // Set the default selection int defaultSelectionIndex = monthsList.indexOf(defaultSelection); if (defaultSelectionIndex != -1) { sp_txn_date.setSelection(defaultSelectionIndex); } } private String extractMonthFromTxnDate(String txnDate) { // Assuming txnDate is in the format "yyyy-MM-dd", extract the month String[] parts = txnDate.split("-"); if (parts.length >= 2) { return parts[1]; // Return the month part } return ""; } public void setMsg() { if (mAppPreferences.getTTModuleSelection().equalsIgnoreCase("955")) { Utils.msgText(SalesClass.this, "528", tv_brand_logo); Utils.msgButton(SalesClass.this, "71", bt_back); } else { Utils.msgText(SalesClass.this, "75", tv_brand_logo); Utils.msgText(SalesClass.this, "75", tv_brand_logo); Utils.msgButton(SalesClass.this, "71", bt_back); } } @Override public void onBackPressed() { finish(); } public void getControllerId() { tv_brand_logo = (TextView) findViewById(R.id.tv_brand_logo); bt_back = (Button) findViewById(R.id.button_back); btn_get_data = (Button) findViewById(R.id.btn_sales_data); et_productid= (EditText) findViewById(R.id.et_productid); sp_txn_date = (Spinner) findViewById(R.id.sp_txn_date); rl = (LinearLayout) findViewById(R.id.rl_submit); }
}, in this line of code :private List filterData(List salesList, String productId){ // private List filterData(List salesList, String productId, String selectedMonth) { List filteredData = new ArrayList<>(); for (SalesData data : salesList) { // if (data.getP_ID() == Integer.parseInt(productId) && data.getTXN_DATE().equals(selectedMonth)) { if (data.getP_ID() == Integer.parseInt(productId)) { filteredData.add(data); } else{ int a=1;
}, } return filteredData; }, in this code if (data.getP_ID() == Integer.parseInt(productId)) { filteredData.add(data); }, similarly also i do "data.getTXN_DATE().equals(selectedMonth)" but gives the TXN_DATE data from data but i only want month from the response of api i.e TXN_DATE ,make changes in the code
It seems like you are trying to filter theSalesDatalist based on the
P_IDand
TXN_DATEvalue. However, you want to extract and filter based on the month part of the
TXN_DATE` from the response API.
To achieve this you can modify the filterData
method to extract the month from the TXN_DATE
and compare it with the selected month. Here's the modified method:
private List<SalesData> filterData(List<SalesData> salesList, String productId, String selectedMonth) { List<SalesData> filteredData = new ArrayList<>(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); SimpleDateFormat monthFormat = new SimpleDateFormat("MM", Locale.getDefault()); for (SalesData data : salesList) { if (data.getP_ID() == Integer.parseInt(productId)) { try { Date txnDate = dateFormat.parse(data.getTXN_DATE()); String month = monthFormat.format(txnDate); if (month.equals(selectedMonth)) { filteredData.add(data); } } catch (ParseException e) { e.printStackTrace(); // Handle parsing exception if necessary } } } return filteredData; }
In this updated method,'ve added logic to parse the TXN_DATE
into a Date
object, extract the month, and compare it with the selected month. If they match, the data is added to the filteredData
list.
Please replace the existing filterData
method with this modified one use filterData(salesList, productId, selectedMonth)
when calling the method.
These updates should enable you to filter the sales data based on both the product ID and the selected month from the response. If you have any further questions or need additional assistance, feel free to ask!
private List filterData(List salesList, String productId){ // private List filterData(List salesList, String productId, String selectedMonth) { List filteredData = new ArrayList<>(); for (SalesData data : salesList) { // if (data.getP_ID() == Integer.parseInt(productId) && data.getTXN_DATE().equals(selectedMonth)) { // if (String.valueOf(data.getP_ID()).contains(Integer.parseInt(productId))) { if (String.valueOf(data.getP_ID()).contains(productId)) { filteredData.add(data); } else{ int a=1;
} } return filteredData; }, this code i have done P_ID, similarly make changes for TXN_DATE but i only want month from TXN_DATE ,make changes in the code
It seems like you want to filter the SalesData
based on P_ID
and TXN_DATE
, specifically by extracting the month from the TXN_DATE
. Below is the modified filterData
method to achieve this:
private List<SalesData> filterData(List<SalesData> salesList, String productId, String selectedMonth) { List<SalesData> filteredData = new ArrayList<>(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).equals(productId)) { String txnDate = data.getTXN_DATE(); try { Date date = dateFormat.parse(txnDate); String month = monthFormat.format(date); if (month.equals(selectedMonth)) { filteredData.add(data); } } catch (ParseException e) { e.printStackTrace(); } } } return filteredData; }
In this modified method, we are parsing the TXN_DATE
string to a Date
object using the SimpleDateFormat
and extracting the month using another SimpleDateFormat
object. We then compare the extracted month with the selectedMonth
to filter the data accordingly.
Please replace the existing filterData
method with this modification, pass productId
and the selected month to this method, and test if the filtering now works as expected. If you have any further questions or need additional help, feel free to ask!
btn_get_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {
String selectedMonth = sp_txn_date.getSelectedItem().toString(); String productId = et_productid.getText().toString();
// List filteredData = filterData(initialSalesData, productId); // updateRecyclerView(filteredData); // List filteredData = filterData(initialSalesData, selectedMonth); // updateRecyclerView(filteredData); List filteredData = filterData(initialSalesData, productId, selectedMonth); updateRecyclerView(filteredData);
} }); }
// private List filterData(List salesList, String productId){ // // List filteredData = new ArrayList<>(); // for (SalesData data : salesList) { // // if (String.valueOf(data.getP_ID()).contains(productId)) { // filteredData.add(data); // } // else{ // int a=1; // // // } // // } // return filteredData; // }
// private List filterData(List salesList, String selectedMonth) { // List filteredData = new ArrayList<>(); // SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); // SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); // // for (SalesData data : salesList) { // // String txnDate = data.getTXN_DATE(); // try { // Date date = dateFormat.parse(txnDate); // String month = monthFormat.format(date); // if (month.contains(selectedMonth)) { // filteredData.add(data); // } // } catch (ParseException e) { // e.printStackTrace(); // } // // } // return filteredData; // // }
private List<SalesData> filterData(List<SalesData> salesList, String productId, String selectedMonth) { List<SalesData> filteredData = new ArrayList<>(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).contains(productId)){ String txnDate = data.getTXN_DATE(); try { Date date = dateFormat.parse(txnDate); String month = monthFormat.format(date); if (month.contains(selectedMonth)) { filteredData.add(data); } } catch (ParseException e) { e.printStackTrace(); } } } return filteredData; }
i wanted to write all three consition in if-else statement which means if only filter applied on P_ID and button clicked then P_ID similar value , if only filter applied on TXN_DATE and button clicked then TXN_DATE similar value and both filter applied both common value will be displayed
It seems like you want to apply different filters based on the input criteria, either by product ID or transaction date, or both, when the button is clicked. To achieve this, you can modify the filterData
method to handle these multiple filter conditions. Here's the updated implementation:
private List<SalesData> filterData(List<SalesData> salesList, String productId, String selectedMonth) List<SalesData> filteredData = new ArrayList<>(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); if (!productId.isEmpty() && selectedMonth.isEmpty()) { // Apply filter only based on product ID for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).contains(productId)) { filteredData.add(data); } } } else if (productId.isEmpty() && !selectedMonth.isEmpty()) { Apply filter only based on transaction date for (SalesData data : salesList) { String txnDate = data.getTXN_DATE(); try { Date date = dateFormat.parse(txnDate); String month = monthFormat.format(date); if (month.contains(selectedMonth)) { filteredData.add(data); } } catch (ParseException e) { e.printStackTrace(); } } } else if (!productId.isEmpty() && !selectedMonth.isEmpty()) { // Apply filter based on both product ID and transaction date for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).contains(productId)) { String txnDate = data.getTXN_DATE(); try { Date date = dateFormat.parse(txnDate); String month = monthFormat.format(date); if (month.contains(selectedMonth)) { filteredData.add(data); } } catch (ParseException e) { e.printStackTrace(); } } } } return filteredData; }
With this modification, the filterData
method will apply the appropriate filter based on the provided input criteria: product ID, transaction date, or both. This approach ensures that only the relevant data is included based on the chosen filter criteria when the button is clicked.
here selected is never empty , private void updateSpinnerWithMonths(List salesList) {
monthsList.clear(); // Add the "Select month" item as the default selection String defaultSelection = "Select Month Value"; monthsList.add(defaultSelection);,make changes in the code that that if selected month have default value
To handle the case where the selected month is the default value, you can update the updateSpinnerWithMonths
method as shown below:
private void updateSpinnerWithMonths(List<SalesData> salesList) { monthsList.clear(); // Add the "Select month" item as the default selection String defaultSelection = "Select Month Value"; monthsList.add(defaultSelection); String selectedMonth = sp_txn_date.getSelectedItem().toString(); if (!selectedMonth.equals(defaultSelection)) { // Populate the spinner with months only if a specific month is selected for (SalesData data : salesList) { String txnDate = data.getTXN_DATE(); try { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); Date date = dateFormat.parse(txnDate); String month = new SimpleDateFormat("MM").format(date); if (!monthsList.contains(month)) { // Add the unique month to the list monthsList.add(month); } } catch (ParseException e) { e.printStackTrace(); } } // Set the adapter for the spinner ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, monthsList); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp_txn_date.setAdapter(dataAdapter); }
In this update, if the selected month is the default "Select Month Value," the method will not populate the spinner with specific months. Only when a specific month is selected will the spinner be populated with the corresponding months based on the available sales data.
private List filterData(List salesList, String productId, String selectedMonth) { List filteredData = new ArrayList<>(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
if (!productId.isEmpty() && selectedMonth.isEmpty()) { // Apply filter only based on product ID for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).contains(productId)) { filteredData.add(data); } } } else if (productId.isEmpty() && !selectedMonth.isEmpty()) { // Apply filter only based on transaction date for (SalesData data : salesList) { String txnDate = data.getTXN_DATE(); try { Date date = dateFormat.parse(txnDate); String month = monthFormat.format(date); if (month.contains(selectedMonth)) { filteredData.add(data); } } catch (ParseException e) { e.printStackTrace(); } } } else if (!productId.isEmpty() && !selectedMonth.isEmpty()) { // Apply filter based on both product ID and transaction date for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).contains(productId)) { String txnDate = data.getTXN_DATE(); try { Date date = dateFormat.parse(txnDate); String month = monthFormat.format(date); if (month.contains(selectedMonth)) { filteredData.add(data); } } catch (ParseException e) { e.printStackTrace(); } } } } return filteredData;
} make changes in this code to check for default condtion of selectedmonth
You can update the filterData
method to check for the default condition of the selectedMonth
as follows:
private List<SalesData> filterData(List<SalesData> salesList, String productId, String selectedMonth) { List<SalesData> filteredData = new ArrayList<>(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); if (!productId.isEmpty() && !selectedMonth.equals("Select Month Value")) { // Apply filter based on both product ID and transaction date for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).contains(productId)) { String txnDate = data.getTXN_DATE(); try { Date date = dateFormat.parse(txnDate); String month = monthFormat.format(date); if (month.contains(selectedMonth)) { filteredData.add(data); } } catch (ParseException e) { e.printStackTrace(); } } } } else if (!productId.isEmpty() && selectedMonth.equals("Select Month Value")) { // Apply filter only based on product ID for (SalesData data : salesList) { if (String.valueOf(data.getP_ID()).contains(productId)) { filteredData.add(data); } } } else if (productId.isEmpty() && !selectedMonth.equals("Select Month Value")) { // Apply filter only based on transaction date for (SalesData data : salesList) { String txnDate = data.getTXN_DATE(); try { Date date = dateFormat.parse(txnDate); String month = monthFormat.format(date); if (month.contains(selectedMonth)) { filteredData.add(data); } } catch (ParseException e) { e.printStackTrace(); } } } else { // No filter applied filteredData.addAll(salesList); } return filteredData; }
With this modification, the filterData
method now checks for the default condition of the selectedMonth
and applies the appropriate filter based on the combination of productId
and selectedMonth
. If the default month "Select Month Value" is chosen, the method includes all the sales data without applying any specific filter for the month.
here i also wanted to add a new functionality that when the data button is clicked it return the ascending order of TXN_DATE i.e return the data in ascending order of (days and month) in TXN_DATE
To implement the functionality of sorting the data in ascending order of the transaction date (TXN_DATE), you can modify the filterData
method as follows:
private List<SalesData> filterAndSortData(List<SalesData> salesList, String productId, String selectedMonth) { List<SalesData> filteredData = new ArrayList<>(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); for (SalesData data : salesList) { if ((productId.isEmpty() || String.valueOf(data.getP_ID()).contains(productId)) && (selected.equals("Select Month Value") || isWithinSelectedMonth(data, selectedMonth))) { filteredData.add(data); } } // Sort the filtered data based on the transaction date in ascending order Collections.sort(filteredData, new Comparator<SalesData>() { DateFormat df = new SimpleDateFormat("dd-MM-yyyy"); public int compare(SalesData data1, SalesData data2) { try { Date date1 = df.parse(data1.getTXN_DATE()); Date date2 = df.parse(data2.getTXN_DATE()); return date1.compareTo(date2); } catch (ParseException e) { throw new IllegalArgumentException(e); } } }); return filteredData; } private boolean isWithinSelectedMonth(SalesData data, String selectedMonth) { SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM"); SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); String txnDate = data.getTXN_DATE(); try { Date date = dateFormat.parse(txnDate); String month = monthFormat.format(date); return month.equals(selectedMonth); } catch (ParseException e) { e.printStackTrace(); return false; } }
In the above code, the filterAndSortData
method now filters the sales data based on the product ID and selected month, and then sorts the filtered data in ascending order of the transaction date (TXN_DATE). Additionally, the isWithinSelectedMonth
method is used to check if a transaction falls within the selected month. This approach ensures that the data is both filtered and sorted according to the specified criteria when the data button is clicked.
also in ascending of days and months both
To achieve the functionality of sorting the data in order of both days and months in the transaction date (TXN_DATE), you can modify the filterAndSortData
method as follows:
private List<SalesData> filterAndSortData(List<SalesData> salesList, String productId, String selectedMonth) { List<SalesData> filteredData = new ArrayList<>(); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); for (SalesData data : salesList) { if ((productId.isEmpty() || String.valueOf(data.getP_ID()).contains(productId)) && (selectedMonth.equals("Select Month Value") || isWithinSelectedMonth(data, selectedMonth))) { filteredData.add(data); } } // Sort the filtered data based on the transaction date in ascending order of both days and months Collections.sort(filteredData, new Comparator<SalesData>() { DateFormat df = new SimpleDateFormat("dd-MM-yyyy"); public int compare(SalesData data1, SalesData data2) { try { Date date1 = df.parse(data1.getTXN_DATE()); Date date2 = df.parse(data2.getTXN_DATE()); if (date1.equals(date2)) { return 0; } else if (date1.before(date2)) { return -1; } else { return 1; } } catch (ParseException e) { throw new IllegalArgumentException(e); } } }); return filteredData; } private boolean isWithinSelectedMonth(SalesData data, String selectedMonth) { SimpleDateFormat monthFormat = new SimpleDateFormat("MM"); String txnDate = data.getTXN_DATE(); try { Date date = new SimpleDateFormat("dd-MM-yyyy").parse(txnDate); Calendar cal = Calendar.getInstance(); cal.setTime(date); String month = String.format("%02d", cal.get(Calendar.MONTH) + 1); return month.equals(selectedMonth); } catch (ParseException e) { e.printStackTrace(); return false; } }
With these modifications, the filterAndSortData
method now filters the sales data and then sorts it in ascending order of both days and months in the transaction date (TXN_DATE). The isWithinSelectedMonth
method also ensures that the transaction date falls within the selected month. This update should provide the functionality you're looking for when the data button is clicked.