I have a listview with custom adapter in listfragment and also set onclicklistner for listview. But Onclicklistner does not work.
Here is my code:
p
As per the Android API Doc:
ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle). To do this, your view hierarchy must contain a ListView object with the id "@android:id/list" (or list if it's in code)
Since you did not post the layout file for the fragment i am not sure what went wrong here.
The following code is how it should be when you are using the default list view of ListFragment. If you are using the ListFragment you should leverage the additional methods available like setListAdapter
and onListItemClick
. You can additionally do the same thing without using a ListFragment (Using just Fragment).
The Fragment Code (Modified your piece of code)
public class BasicFragment extends ListFragment {
MyCustomAdapter adapter;
@Override
public void onCreate(Bundle si) {
super.onCreate(si);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// not sure what you are doing here but data fetch should be asynchronous if it interacts with DB or makes network call
FetchedData DT = FetchedData.StaticDataTransfer();
RecepiesProperties[] AryObjaz = DT.getData();
getdata(AryObjaz);
adapter = new MyCustomAdapter(getActivity(), R.layout.listview_layout, Dataset);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast t = Toast.makeText(getActivity(), "Message",
Toast.LENGTH_SHORT);
t.show();
}
}
Additionally i modified your adapter code to help recycle views, your current code was not using view recycling and was always inflating views.
Adapter code:
public class MyCustomAdapter extends ArrayAdapter {
Context context;
int layoutResourceId;
Recipes data[] = null;
Typeface typeface;
public ImageLoader imageLoader;
private LayoutInflater inflater;
public MyCustomAdapter(Context context, int textViewResourceId, Recipes[] dataset) {
super(context, textViewResourceId, dataset);
this.layoutResourceId = textViewResourceId;
this.context = context;
this.data = dataset;
imageLoader = new ImageLoader(context.getApplicationContext());
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecipesHolder holder = null;
//recycling views
if(null == row){
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecipesHolder();
holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1);
holder.txtTitle = (TextView) row.findViewById(R.id.title);
holder.category = (TextView) row.findViewById(R.id.category);
holder.source = (TextView) row.findViewById(R.id.source);
holder.country = (TextView) row.findViewById(R.id.country);
holder.readytime = (TextView) row.findViewById(R.id.readytime);
holder.tips = (Button) row.findViewById(R.id.tips);
holder.fav = (Button) row.findViewById(R.id.fav);
row.setTag(holder);
}else{
holder = (RecipesHolder)row.getTag();
}
Recipes ap = data[position];
imageLoader.DisplayImage(ap.getIMAGENAME240(), holder.imgIcon);
holder.txtTitle.setText(ap.getNAME());
holder.category.setText(ap.getCATEGORY());
holder.source.setText(ap.getSOURCE());
holder.country.setText(ap.getCOUNTRY());
holder.readytime.setText(ap.getREADYTIME());
return row;
}
static class RecipesHolder {
ImageView imgIcon;
TextView txtTitle;
TextView category;
TextView source;
TextView country;
TextView readytime;
Button tips;
Button fav;
}
}