I am trying to make call from my main listview Fragment to another differnt listview fragments by clickig different items on main list view.
For that i have written following code:
My main listview fragment ShopFragment.java: (from this list items i want to call another fragments by clicking on different items)
package fragments.h.safmical;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import safmical.h.R;
/**
* Created by hadvani on 4/13/2017.
*/
public class ShopFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview= inflater.inflate(R.layout.fragment_shop,container,false);
String[] list1={"x","y","z"};
ListView listView= (ListView) rootview.findViewById(R.id.mainlist);
ArrayAdapter<String> listviewadapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,list1);
listView.setAdapter(listviewadapter);
FragmentManager fm =getFragmentManager();
listView.setOnClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
fm.beginTransaction().replace(R.id.content_frame,new ListOneFragment()).commit();
}
if(position==1){
fm.beginTransaction().replace(R.id.content_frame,new ListTwoFragment()).commit();
}
if(position==2){fm.beginTransaction().replace(R.id.content_frame,new ListThreeFragment()).commit();}
}
});
return rootview;
}
}
xml file of main listview fragment fragment_shop.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#e6e6fa">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainlist"/>
</FrameLayout>
ListOneFragment.java:
package fragments.h.safmical;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import safmical.h.R;
/**
* Created by hadvani on 4/14/2017.
*/
public class ListOneFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_listone, container, false);
String[] list1 = {"a", "b", "c"};
ListView listView = (ListView) rootview.findViewById(R.id.list1);
ArrayAdapter<String> listviewadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list1);
listView.setAdapter(listviewadapter);
return rootview;
}
}
fragment_listone.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list1">
</ListView>
</FrameLayout>
Same way i have implemented ListTwoFragment.java , ListThreeFragment.java , fragment_listtwo.xml and fragment_listthree.xml.
but its not working. Gradle build shows following error:
Error:(30, 17) error: no suitable method found for setOnClickListener(<anonymous OnItemClickListener>)
method View.setOnClickListener(OnClickListener) is not applicable
(argument mismatch; <anonymous OnItemClickListener> cannot be converted to OnClickListener)
method AdapterView.setOnClickListener(OnClickListener) is not applicable
(argument mismatch; <anonymous OnItemClickListener> cannot be converted to OnClickListener)
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Should i make any correction or is there any differnet method to do this? Please help me to solve this.
You need to change
listView.setOnClickListener(new AdapterView.OnItemClickListener() {
to
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
What's happening is that it's trying OnClickListener
instead of OnItemClickListener