Reputation: 2167
I have one android fragment that have a listView. Now I want implement onItemClickListener and show a Modal, if the user click on one item of ListView. Then, if I click on one items, I should to see a Modal with another listView. So with my code, if I try to click on one items, I can display the items of the last element of my principal listView, and it is not good.
This is my Fragment:
public class AlertsFragment extends Fragment {
private AlertAdapter pAdapter;
AlertXAgentAdapter agentAdapter;
AlertXReactionAdapter reactionAdapter;
private RecyclerView recyclerView,recyclerAgentView,recyclerReactionView;
private List<Alert> lista= new ArrayList<Alert>();
View v;
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
private static final int ARG_SECTION_NUMBER =6;
@Override
public void onAttach(Context context) {
super.onAttach(context);
((AndroidNavDrawerActivity) context).onSectionAttached(
ARG_SECTION_NUMBER);
}
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
v=inflater.inflate(R.layout.alerts_activity, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
//recupero la lista delle medication
AlertDAO manager = new AlertDAO(this.getContext());
lista = manager.getAlerts();
pAdapter = new AlertAdapter(lista, new AlertAdapter.OnItemClickListener() {
@Override
public void onItemClick(Alert item) {
try{
final Dialog dialog = new Dialog(v.getContext());
LayoutInflater inflater = LayoutInflater.from(v.getContext());
View dialogView = inflater.inflate(R.layout.alert_agent_reaction_modal, null);
//agent
recyclerAgentView = (RecyclerView) dialogView.findViewById(R.id.recycler_agent_view);
//reaction
recyclerReactionView = (RecyclerView) dialogView.findViewById(R.id.recycler_reaction_view);
dialog.setContentView(dialogView);
dialog.setTitle("Agent and Reaction");
//agent
agentAdapter = new AlertXAgentAdapter(item.getListaAgent());
//reaction
reactionAdapter = new AlertXReactionAdapter(item.getListaReaction());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(v.getContext());
recyclerAgentView.setLayoutManager(mLayoutManager);
recyclerAgentView.setItemAnimator(new DefaultItemAnimator());
recyclerAgentView.setAdapter(agentAdapter);
RecyclerView.LayoutManager mLayoutManager2 = new LinearLayoutManager(v.getContext());
recyclerReactionView.setLayoutManager(mLayoutManager2);
recyclerReactionView.setItemAnimator(new DefaultItemAnimator());
recyclerReactionView.setAdapter(reactionAdapter);
dialog.show();
}catch(Exception e){
Log.e("","");
}
}
});
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(v.getContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(pAdapter);
/**
* Inflate the layout for this fragment
*/
return v;
}
}
This is my Adapter
public class AlertAdapter extends RecyclerView.Adapter<AlertAdapter.MyViewHolder> {
private List<Alert> list;
public Alert alert;
public OnItemClickListener listener;
public interface OnItemClickListener {
void onItemClick(Alert item);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView startDate, endDate,description,status;
public MyViewHolder(View view) {
super(view);
startDate = (TextView) view.findViewById(R.id.startDate);
endDate = (TextView) view.findViewById(R.id.endDate);
description = (TextView) view.findViewById(R.id.description);
status = (TextView) view.findViewById(R.id.status);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(listener != null){
listener.onItemClick(alert);
}
}
}
public AlertAdapter(List<Alert> list,OnItemClickListener listener) {
this.list = list;
this.listener=listener;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.alert_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
alert = list.get(position);
holder.startDate.setText(alert.getDateStart()!=null ? alert.getDateStart() : "");
holder.endDate.setText(alert.getDateEnd()!=null ? alert.getDateEnd() : "");
holder.description.setText(alert.getAlertInfo().getDisplayName());
//holder.type.setText(planOfCare.getDateEnd()!=null ? planOfCare.getDateEnd() : "");
holder.status.setText(alert.getAlarmAlertInfo().getDisplayName());
}
@Override
public int getItemCount() {
return list.size();
}
}
Upvotes: 0
Views: 454
Reputation: 205
Your Alert
variable is not getting assigned to proper object as you are assigning it in onBindViewHolder
method. In your ViewHolder's onclick method try using this instead of passing the Alert
variable
listener.onItemClick(list.get(this.getAdapterPosition()));
Upvotes: 1
Reputation: 336
I think you should do something like this
public class AlertsFragment extends Fragment implements OnClickListener {
personRewardAdapter =new PersonRewardAdapter(PersonRewardSelectionActivity.this, Utilities.R_entities);
rewardList.setAdapter(personRewardAdapter);
rewardList.setOnclickListener(getActivity());
}
public void OnClick(){
}
do this in onCreateView. I am doing this in my code and maybe It helps you, so i am sharing to you
Upvotes: 0