I have a listvew with a castam adapter, which contains a Textview , CheckBox and a button.
When I click on imaginbutton imbutUpd , I want the fragment where the listview is located to be replaced by a new fragment.
BaseAdapter Code

 public class ListViewAdapter extends BaseAdapter { ArrayList<ListBase> listBases; Context context; public ListViewAdapter(Context context, ArrayList<ListBase> araylv){ if(araylv != null){ listBases = araylv; } this.context = context; } @Override public int getCount() { return listBases.size(); } @Override public Object getItem(int number) { return listBases.get(number); } @Override public long getItemId(int arg0) { return arg0; } private DBMethods dbMethods; CheckBox checkDel; ImageButton imbutUpd; BussinesRecrods bussinesRecrods; MainActivity main; int isTag; @Override public View getView(int id, View convertView, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(context); ListBase p = getProduct(id); bussinesRecrods = new BussinesRecrods(); dbMethods = new DBMethods(context); main = new MainActivity(); if (convertView == null) { convertView = inflater.inflate(R.layout.tableinfact, parent, false); } TextView textVNotBook = (TextView) convertView.findViewById(R.id.textVNotBook); TextView textTimeDate = (TextView) convertView.findViewById(R.id.textDateTime); checkDel = (CheckBox) convertView.findViewById(R.id.checkBNotBook); imbutUpd = (ImageButton) convertView.findViewById(R.id.imageButUpdate); textVNotBook.setText(listBases.get(id).textHave); textTimeDate.setText(listBases.get(id).textTime+ " "+listBases.get(id).textDate); checkDel.setOnCheckedChangeListener(myCheckChangeList); checkDel.setTag(id); checkDel.setChecked(p.box); imbutUpd.setOnClickListener(onClickListener); imbutUpd.setTag(id); return convertView; } View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View v) { } }; } 

    1 answer 1

    Fragment change should be made not in the adapter, but in the activity in which the fragment is located.

    Here are two options:

    1. If you have an adapter in the activity class, simply call the following function from the activity:

       void changeFragment(int number) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(idContainer, new MyFragment()); //или ft.replace(idContainer, MyFragment.newInstance(number)); ft.commit(); } 
    2. If you have an adapter in a separate class, call this function through the interface in the adapter class:

       ChangeFragment clickOnItem; interface ChangeFragment{ void changeFragment(int number); } public ListViewAdapter(Context context, ArrayList<ListBase> araylv){ if(araylv != null){ listBases = araylv; } this.context = context; clickOnItem = (ClickOnItem) context; } 

    Call on click clickOnItem.changeFragment(id) , and in activity: MyActivity implements ListViewAdapter.ChangeFragment and the @Override function from option # 1.

    In general, ListView is now considered obsolete and it is recommended to use RecyclerView . It is really simpler, better and more functional.

    • And how to put the listview button handler in the activity class? - LnDivan -
    • do you have an adapter in a separate class or not? - Jarvis_J
    • Adapter in a separate class - LnDivan -
    • then using the interface. For example, like this: .stackoverflow.com / questions / 498412 / . Just initialize the interface variable in the adapter's constructor, and not in onAttach . - Jarvis_J
    • And can you please cite as an example the code already with the initialized interface in the adapter's constructor, and the redefined interface method in activit. - LnDivan -