Фрагмент состояния:
public class StatesFragment extends Fragment implements MyCustomAdaptorState.OnItemClickListener {
private TextView tvcases_india, tvdeath_india, tvrecoverey_india;
EditText serachstr_india;
RecyclerView recyclerView;
public static List<StatesModel> statesModelList = new ArrayList<>();
private StatesModel statesModel;
private MyCustomAdaptorState myCustomAdaptorState;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_states, container, false);
//Active Calls
tvcases_india = inflate.findViewById(R.id.india_covid_cases);
tvdeath_india = inflate.findViewById(R.id.india_covid_death);
tvrecoverey_india = inflate.findViewById(R.id.india_covid_recovered);
serachstr_india = inflate.findViewById(R.id.edtext_search_bar_india_states);
recyclerView = inflate.findViewById(R.id.covid_india_state);
//Volley Call 1
getDataIndia();
//Volley Call 2
getDataState();
//Search Bar
serachstr_india.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
myCustomAdaptorState.getFilter().filter(charSequence);
myCustomAdaptorState.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
return inflate;
}
private void getDataIndia() {
RequestQueue queue = Volley.newRequestQueue(getActivity());
String url = "https://corona.lmao.ninja/v2/countries/india";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject object = new JSONObject(response.toString());
tvcases_india.setText(object.getString("cases"));
tvdeath_india.setText(object.getString("deaths"));
tvrecoverey_india.setText(object.getString("recovered"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error Response", error.toString());
}
});
queue.add(request);
}
private void getDataState() {
RequestQueue queue = Volley.newRequestQueue(getActivity());
String Url = "http://covid19-india-adhikansh.herokuapp.com/states";
StringRequest stringRequest = new StringRequest(Request.Method.GET, Url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("state");
for (int i = 0; i < array.length(); i++) {
statesModel = new StatesModel();
JSONObject object = array.getJSONObject(i);
statesModel.setStateName(object.getString("name"));
statesModelList.add(statesModel);
}
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
myCustomAdaptorState = new MyCustomAdaptorState(getActivity(), statesModelList);
recyclerView.setAdapter(myCustomAdaptorState);
myCustomAdaptorState.setOnItemClickListener(StatesFragment.this);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("onResponse", error.toString());
}
});
queue.add(stringRequest);
}
@Override
public void onClickListener(int position) {
Intent intent = new Intent(getActivity(),DetailStateActivity.class);
intent.putExtra("position",position);
startActivity(intent);
}
}
CustomAdaptorState. java:
public class MyCustomAdaptorState extends RecyclerView.Adapter<MyCustomAdaptorState.ViewHolder> {
LayoutInflater layoutInflater;
private static List<StatesModel> statesModelList ;
private List<StatesModel> statesModelListFilter;
private OnItemClickListener mListener;
public interface OnItemClickListener{
void onClickListener(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
public MyCustomAdaptorState(Context ctx ,List<StatesModel> statesModelList){
this.layoutInflater = LayoutInflater.from(ctx);
this.statesModelList = statesModelList;
this.statesModelListFilter = statesModelList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.custom_list_item_states,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.stateName.setText(statesModelListFilter.get(position).getStateName());
}
@Override
public int getItemCount() {
return statesModelListFilter.size();
}
public Filter getFilter(){
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults filterResults = new FilterResults();
if(charSequence == null || charSequence.length()==0){
filterResults.count = statesModelList.size();
filterResults.values = statesModelList;
}else {
List<StatesModel> statesModelsResult = new ArrayList<>();
String searchStr = charSequence.toString().toLowerCase();
for(StatesModel statesItemModel : statesModelList){
if (statesItemModel.getStateName().toLowerCase().contains(searchStr)){
statesModelsResult.add(statesItemModel);
}
filterResults.count = statesModelsResult.size();
filterResults.values = statesModelsResult;
}
}
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
statesModelListFilter = (List<StatesModel>)filterResults.values;
StatesFragment.statesModelList = (List<StatesModel>)filterResults.values;
notifyDataSetChanged();
}
};
return filter;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView stateName;
public ViewHolder(@NonNull View itemView) {
super(itemView);
stateName = itemView.findViewById(R.id.tv_india_state_name);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mListener != null){
int position = getAdapterPosition();
if(position!=RecyclerView.NO_POSITION){
mListener.onClickListener(position);
}
}
}
});
}
}
StateModel. java
public class StatesModel {
private String stateName,stateCases,stateDeath,stateRecovered;
public StatesModel() {
}
public StatesModel(String stateName, String stateCases, String stateDeath, String stateRecovered) {
this.stateName = stateName;
this.stateCases = stateCases;
this.stateDeath = stateDeath;
this.stateRecovered = stateRecovered;
}
public String getStateName() {
return stateName;
}
public void setStateName(String stateName) {
this.stateName = stateName;
}
public String getStateCases() {
return stateCases;
}
public void setStateCases(String stateCases) {
this.stateCases = stateCases;
}
public String getStateDeath() {
return stateDeath;
}
public void setStateDeath(String stateDeath) {
this.stateDeath = stateDeath;
}
public String getStateRecovered() {
return stateRecovered;
}
public void setStateRecovered(String stateRecovered) {
this.stateRecovered = stateRecovered;
}
}
DetailStateActitvity.java
public class DetailStateActivity extends AppCompatActivity {
int position;
private TextView stateNameIndia,stateCases,stateDeath,stateRecovered;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_state);
stateNameIndia = (TextView)findViewById(R.id.detail_state);
stateCases = (TextView)findViewById(R.id.no_cases_state);
stateRecovered = (TextView)findViewById(R.id.no_recovered_state);
stateDeath = (TextView)findViewById(R.id.no_death_state);
Intent intent = getIntent();
position = intent.getIntExtra("position",0);
stateNameIndia.setText(StatesFragment.statesModelList.get(position).getStateName());
stateCases.setText(StatesFragment.statesModelList.get(position).getStateCases());
stateRecovered.setText(StatesFragment.statesModelList.get(position).getStateRecovered());
stateDeath.setText(StatesFragment.statesModelList.get(position).getStateDeath());
}
}
fragment_state. xml:
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardBackgroundColor="#fcf2d8"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
app:cardCornerRadius="15dp"
app:cardElevation="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.061"
tools:layout_editor_absoluteX="0dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_medium"
android:text="India COVID-19 Report"
android:textSize="20sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_medium"
android:text="Cases"
android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0"
android:textColor="@color/TotalCases"
android:id="@+id/india_covid_cases"
android:textSize="18sp"
android:textAlignment="textEnd"
android:textStyle="bold"
android:fontFamily="@font/poppins_bold"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_medium"
android:text="Death"
android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0"
android:textColor="@color/TotalDeath"
android:id="@+id/india_covid_death"
android:textSize="18sp"
android:textAlignment="textEnd"
android:textStyle="bold"
android:fontFamily="@font/poppins_bold"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_medium"
android:text="Recovered"
android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0"
android:textColor="@color/TotalRecovery"
android:id="@+id/india_covid_recovered"
android:textSize="18sp"
android:textAlignment="textEnd"
android:textStyle="bold"
android:fontFamily="@font/poppins_bold"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="530dp"
app:cardBackgroundColor="#fcf2d8"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
app:cardCornerRadius="15dp"
app:cardElevation="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.061"
tools:layout_editor_absoluteX="0dp">
<EditText
android:id="@+id/edtext_search_bar_india_states"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="15dp"
android:background="@drawable/edittext_design"
android:drawableLeft="@drawable/ic_search_black_24dp"
android:paddingLeft="10dp"
android:hint="Search Here..."
android:fontFamily="@font/poppins_medium"
android:singleLine="true"
android:drawablePadding="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.009"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="13dp" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="66dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:id="@+id/covid_india_state"
android:layout_below="@id/edtext_search_bar_india_states"
tools:listitem="@layout/custom_list_item_states"
tools:ignore="MissingConstraints">
</androidx.recyclerview.widget.RecyclerView>
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.LinearLayoutCompat>
custom_list_item_state.xml:
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_margin="5dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="80dp"
android:layout_height="60dp"
android:id="@+id/imageview_india"
android:src="@drawable/indiaflag"
android:padding="5dp"
android:layout_margin="5dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_india_state_name"
android:layout_gravity="center_vertical"
android:fontFamily="@font/poppins_medium"
android:text="State Name"
android:textSize="20sp"
android:paddingLeft="15dp"/>
</androidx.appcompat.widget.LinearLayoutCompat>
activity_detail_state.xml:
<androidx.appcompat.widget.LinearLayoutCompat 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:orientation="vertical"
tools:context=".DetailActivity">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="80dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
app:cardElevation="20dp"
app:cardBackgroundColor="#fcf2d8"
app:cardCornerRadius="15dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_medium"
android:text="States"
android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/detail_state"
android:textSize="18sp"
android:textAlignment="textEnd"
android:textStyle="bold"
android:fontFamily="@font/poppins_bold"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_medium"
android:text="Cases"
android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/no_cases_state"
android:textSize="18sp"
android:textAlignment="textEnd"
android:textStyle="bold"
android:fontFamily="@font/poppins_bold"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="102dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_medium"
android:text="Recovered"
android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/no_recovered_state"
android:textSize="18sp"
android:textAlignment="textEnd"
android:textStyle="bold"
android:fontFamily="@font/poppins_bold"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="149dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/poppins_medium"
android:text="Death"
android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/no_death_state"
android:textSize="18sp"
android:textAlignment="textEnd"
android:textStyle="bold"
android:fontFamily="@font/poppins_bold"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.LinearLayoutCompat>
пожалуйста, помогите мне получить все результаты и отобразить их в activity_detail_state. Я получаю только название штата, а не остальные детали. Пожалуйста, помогите мне завершить мой проект. Извините, что не предоставил изображение проблемы. но когда вы запускаете код, вы понимаете, в чем проблема.