I have a problem with fragment, what i create:
Here is a custom List
class CustomList extends ArrayAdapter<String> {
private final Activity activity;
private final String[] web;
private final Integer[] imageID;
CustomList(Activity activity, String[] web, Integer[] imageID){
super(activity, R.layout.list_single, web);
this.activity = activity;
this.web = web;
this.imageID = imageID;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView img = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
img.setImageResource(imageID[position]);
return rowView;
}
}
Here is a fragment:
public class BackgroundSelectFragment extends Fragment {
ListView list;
String[] web = {
"Clearing",
"Scroll",
"Desert",
"World",
"Old City",
"Clip Board",
} ;
Integer[] imageId = {
R.drawable.clearing,
R.drawable.scroll,
R.drawable.desert,
R.drawable.world,
R.drawable.city,
R.drawable.clipboard,
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listview_fragment, container, false);
CustomList adapter = new
CustomList(getActivity(), web, imageId);
list = (ListView) container.findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
and the last i try to open a fragment:
backgroundBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.show(backgroundSelectFragment)
.commit();
}
});
Im not got any error, when i click a button nothing appear, where is my fragment? Of course i initialize it in MainActivity OnCreate like:
backgroundSelectFragment = new BackgroundSelectFragment();
Please help. I dont know what i do wrong.