Reputation: 41
I have a fragment (i tried also in activity) that have edittext and button and recyclerview I expect to get json data displayed in the recyclerview after clicking the button to search with the text of edittext.
problem is: it works fine but i should press my edittext
here is the button clicklistener code :
searchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hospsAdapter = new hospsAdapter(getContext(), arrayList, HospitalFragment.this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(hospsAdapter);
loadData()
}
}
inside the load data method i call this method after getting json data from volley
public void updateList(ArrayList<HospitalsModel> list){
if(hospsAdapter != null) {
hospsAdapter.updateData(list);
}
}
And here is the adaper code:
public class hospsAdapter extends RecyclerView.Adapter<hospsAdapter.OrdersViewHolder> {
private Context context;
private ItemClickHandler itemClickHandler;
private List<HospitalsModel> orderDetailsList;
HospitalsModel productsModel;
public hospsAdapter(Context context, List<HospitalsModel> orderDetailsList , ItemClickHandler itemClickHandler) {
this.context = context;
this.itemClickHandler = itemClickHandler;
this.orderDetailsList = new ArrayList<>(orderDetailsList);
}
public hospsAdapter() {
}
public interface ItemClickHandler{
public void onItemClick(HospitalsModel productsModel);
}
@NonNull
@Override
public OrdersViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.hosps_list_item, viewGroup, false);
return new OrdersViewHolder(view);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onBindViewHolder(@NonNull OrdersViewHolder ordersViewHolder, int i) {
HospitalsModel productsModel = orderDetailsList.get(i);
Glide.with(context).load(productsModel.getImageUrl()).into(ordersViewHolder.appCompatImageView);
ordersViewHolder.orderTv.setText(productsModel.getName());
ordersViewHolder.orderTv2.setText("العنوان: "+productsModel.getAddress());
ordersViewHolder.orderTv5.setText("متاح الان: نعم"+"");
final int[] flag = {1};
ordersViewHolder.appCompatImageView2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(flag[0] ==1) {
ordersViewHolder.appCompatImageView2.setImageDrawable(context.getDrawable(R.drawable.heart2));
flag[0] = 2;
}else {
ordersViewHolder.appCompatImageView2.setImageDrawable(context.getDrawable(R.drawable.heart));
flag[0] =1;
}
}
});
ordersViewHolder.book.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialPhoneNumber(productsModel.getOpenHo(),context);
}
});
ordersViewHolder.map.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri gmmIntentUri = Uri.parse("geo:0,0?q=" +productsModel.getName());
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(v.getContext().getPackageManager()) != null) {
v.getContext().startActivity(mapIntent);
}
}
});
}
@Override
public int getItemCount() {
return orderDetailsList.size();
}
public void updateData(List<HospitalsModel> movies) {
orderDetailsList.clear();
this.orderDetailsList = movies;
notifyDataSetChanged();
}
public class OrdersViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@BindView(R.id.order_img)
AppCompatImageView appCompatImageView;
@BindView(R.id.fav_img)
AppCompatImageView appCompatImageView2;
@BindView(R.id.order_tv4)
TextView orderTv2;
@BindView(R.id.order_tv)
TextView orderTv;
@BindView(R.id.order_tv5)
TextView orderTv5;
@BindView(R.id.button_more)
Button book;
@BindView(R.id.button_map)
Button map;
public OrdersViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
HospitalsModel productsModel = orderDetailsList.get(position);
itemClickHandler.onItemClick(productsModel);
}
}
@Override
public long getItemId(int position) {
return position;
}
public void dialPhoneNumber(String phoneNumber,Context context) {
if(Build.VERSION.SDK_INT > 22) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
ActivityCompat.requestPermissions((DoctorsActivity)context, new String[]{Manifest.permission.CALL_PHONE}, 101);
return;
}
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phoneNumber));
if (intent.resolveActivity(context.getPackageManager()) != null) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
context.startActivity(intent);
}
}else {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phoneNumber));
if (intent.resolveActivity(context.getPackageManager()) != null) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
context.startActivity(intent);
}
}
}
}
I've tried every thing in every related question but no thing changed so please help and tell me why can this problem happen while android development. Thanks in advance.
Upvotes: 1
Views: 808
Reputation: 41
finally i found a solution for this issue after updating the adapter list create a handler and refresh your layout
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
getActivity().findViewById(R.id.recyclerView).invalidate();
getActivity().findViewById(R.id.recyclerView).requestLayout();
}
},1500);
Upvotes: 1
Reputation: 2278
There are some problems in your code, try to fix them first.
1- Initialize your adapter and layout manager in onCreate
(activity) or onViewCreated
(fragment).
2- Just loadData()
must be called when your button is clicked
3- Your updateList(...)
must be called on your UI thread
4- Try to clear all items in your orderDetailsList
then add new items on it
Anyway there are better solutions to handle such these things and that are MVI or MVVM. You can continue learning Android from here Android Doc
Update Answer :
Your adapter seems good, however apply these two things.
1- Remove List<HospitalsModel> orderDetailsList
argument in your constructor and initialize orderDetailsList
without any initial items.
2- Try to replace updateData(...)
with this one,
public void updateData(List<HospitalsModel> movies) {
orderDetailsList.clear();
orderDetailsList.addAll(movies);
notifyDataSetChanged();
}
Upvotes: 0