i'm trying to develop an android app. i used Custom listview for array.
i have 4 buttons like that :
But i want to that : When Yellow button clicked listview's style :
When Button 2 clicked listview's style : :
My codes below :
CustomTextview xml:
android:background="#drawable/lstx_1"
android:id="#+id/ls_txt"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="40dp"/>
CustomAdapter.java
public class customlist extends ArrayAdapter<String> {
private final Activity context;
private final String[] hikayeler;
private Typeface tf;
public customlist(Activity context, String[] hikayeler) {
super(context,R.layout.lsv_txt, hikayeler);
this.context = context;
this.hikayeler = hikayeler;
this.tf = Typeface.createFromAsset(context.getAssets(), "poetsen.ttf");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.lsv_txt, null, true);
TextView ls_txt = (TextView) rowView.findViewById(R.id.ls_txt);
ls_txt.setText(hikayeler[position]);
ls_txt.setTypeface(tf);
GradientDrawable gdBaslik = new GradientDrawable();
gdBaslik.setColor(Color.parseColor("#cf96ac"));
gdBaslik.setCornerRadius(12);
gdBaslik.setStroke(2, Color.parseColor("#cf96ac"));
ls_txt.setBackground(gdBaslik);
return rowView;} }
The sample code is as follows:
private ListView mListView;
private customlist mcustomAdapter;
private boolean mStyle = false;
public void showColor(View v) {
if (mListView == null) {
mListView = (ListView) findViewById(R.id.lv_test);
mcustomAdapter = new customlist(this, new String[] { "aaaa", "bbbbb", "ccccc" });
mListView.setAdapter(mcustomAdapter);
} else {
GradientDrawable gdBaslik = new GradientDrawable();
if (mStyle) {
gdBaslik.setColor(Color.parseColor("#cf96ac"));
gdBaslik.setCornerRadius(12);
gdBaslik.setStroke(2, Color.parseColor("#cf96ac"));
} else {
gdBaslik.setColor(Color.parseColor("#849232"));
gdBaslik.setCornerRadius(12);
gdBaslik.setStroke(2, Color.parseColor("#849232"));
}
mStyle = !mStyle;
mcustomAdapter.setGradientDrawable(gdBaslik);
}
}
public class customlist extends ArrayAdapter<String> {
private final Activity context;
private final String[] hikayeler;
private GradientDrawable gdBaslik;
private Typeface tf;
public customlist(Activity context, String[] hikayeler) {
super(context, R.layout.lsv_txt, hikayeler);
this.context = context;
this.hikayeler = hikayeler;
this.tf = Typeface.createFromAsset(context.getAssets(), "poetsen.ttf");
}
public void setGradientDrawable(GradientDrawable drawable) {
this.gdBaslik = drawable;
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.lsv_txt, null, true);
TextView ls_txt = (TextView) rowView.findViewById(R.id.ls_txt);
ls_txt.setText(hikayeler[position]);
ls_txt.setTypeface(tf);
ls_txt.setBackground(gdBaslik);
return rowView;
}
}
Note: "showColor(View v)" is a Button click event.I hope it can help you.
Related
I have 3 classes relating to my checkbox section of my app in Android studio, atm the check box loads, but when selecting and deselecting the value doesn't save when I go bak into it from the main menu. any help would great!!!
public class WatchList extends AppCompatActivity {
ArrayList dataModels;
ListView listView;
private WatchListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("watchlist", "created watchlist activity");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_watch_list);
listView = (ListView) findViewById(R.id.listview2);
dataModels = new ArrayList();
dataModels.add(new WatchListClass(R.drawable.kookaburra,"Kookaburra","Albury", false));
dataModels.add(new WatchListClass(R.drawable.cockatoo, "Cockatoo" , "Bathurst", true));
dataModels.add(new WatchListClass(R.drawable.emu,"Emu", "Echuca", true));
dataModels.add(new WatchListClass(R.drawable.magpie, "Magpie", "Sydney", true));
adapter = new WatchListAdapter(dataModels, getApplicationContext());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
WatchListClass dataModel= (WatchListClass) dataModels.get(position);
dataModel.checked = !dataModel.checked;
adapter.notifyDataSetChanged();
}
});
}
}
public class WatchListAdapter extends ArrayAdapter {
private ArrayList dataSet;
Context mContext;
private static class ViewHolder {
TextView birdWatchName, birdWatchLocation;
ImageView birdWatchImage;
CheckBox checkBox;
}
public WatchListAdapter(ArrayList data, Context context) {
super(context, R.layout.watch_list, data);
this.dataSet = data;
this.mContext = context;
}
#Override
public int getCount() {
return dataSet.size();
}
#Override
public WatchListClass getItem(int position) {
return (WatchListClass) dataSet.get(position);
}
#Override
public View getView(int position, View convertView, #NonNull ViewGroup parent) {
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.watch_list, parent, false);
viewHolder.birdWatchImage = (ImageView) convertView.findViewById(R.id.birdWatchImage);
viewHolder.birdWatchName = (TextView) convertView.findViewById(R.id.birdWatchName);
viewHolder.birdWatchLocation = (TextView) convertView.findViewById(R.id.birdWatchLocation);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
WatchListClass item = getItem(position);
viewHolder.birdWatchImage.setImageResource(item.birdWatchImage);
viewHolder.birdWatchName.setText(item.birdWatchName);
viewHolder.birdWatchLocation.setText(item.birdWatchLocation);
viewHolder.checkBox.setChecked(item.checked);
return result;
}
}
public class WatchListClass {
public String birdWatchName, birdWatchLocation;
int birdWatchImage;
boolean checked;
WatchListClass(int birdWatchImage, String birdWatchName,String birdWatchLocation, boolean checked) {
this.birdWatchName = birdWatchName;
this.birdWatchLocation = birdWatchLocation;
this.birdWatchImage = birdWatchImage;
this.checked = checked;
}
}
u can user shared preferences for check Box
try this one
How to save checkbox value with shared preferences?
you can use local database in app to store check box values and retrieve it back.
I have a ListView that displays a list of TextViews. I want each TextView to be displayed in their appropriate typeface. The names of the fonts which appear in the ListView as part of the fonts String[] array are spelled the same as when the Typeface is created.
ListView fontsListView = (ListView) this.view.findViewById(R.id.MenuLayout);
final String[] fonts = new String[] {
"Aclonica",
"Amino-Regular",
};
ArrayList<String> fontsList = new ArrayList<String>();
fontsList.addAll( Arrays.asList(fonts) );
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.fonts_simple_row, fontsList);
final TextView[] textViewArray = new TextView[0];
for( int i = 0; i <= fonts.length - 1; i++) {
textViewArray[i].setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/" + fonts[i] + ".ttf"));
}
fontsListView.setAdapter(listAdapter);
And the XML:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:id="#+id/rowTextView"
android:textSize="20sp"
android:gravity="center_horizontal"
android:padding="10dp"
android:typeface="sans"
android:fontFamily="sans-serif-light" />
Why can't I get each TextView in the ListView its proper Typface?
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.fonts_simple_row,value) {
#Override
public View getView(int position, View convertView,ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(R.id.rowTextView);
textView.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/" + fonts[position] + ".ttf"));
return view;
}
};
fontsListView.setAdapter(arrayAdapter);
You should use a custom array adapter and set font on get view method.
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
String s = values[position];
if (s.equals("Arial")) {
// set textView font to arial
} else if (s.equals("Roboto")){
// set textView font to roboto
}
return rowView;
}
}
Create Typeface only one time and use it. Avoid creating typeface in getview method.it will create every time you scroll if you are not maintaining reusability.
Use ViewHolder pattern for better performance in listview
Below i have try to given both things. I have given explanation in comment also. Please check it if it may help you.
public class AdapterFontViewHolder extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
Typeface typefaceArial, typefaceRoboto;
private Typeface[] fonts;
public AdapterFontViewHolder(Context context, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
// Create Typeface only once and use it.. Path you can change as per your directory
fonts = new Typeface[]{
Typeface.createFromAsset(context.getAssets(), "fonts/Aclonica.ttf"),
Typeface.createFromAsset(context.getAssets(), "fonts/Amino-Regular.ttf"),
};
}
public class ViewHolder {
TextView textView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vHolder;
if (convertView == null) {
// Create Instnce of view if its null & store object in ViewHolder (a class)
vHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.rowlayout, parent, false);
vHolder.textView = (TextView) convertView.findViewById(R.id.label);
//Set ViewHolder instance in convertview in a tag as a object
convertView.setTag(vHolder);
} else {
// reuse as already converview is instansiated & it holds ViewHolder instance in tag
vHolder = (ViewHolder) convertView.getTag();
}
vHolder.textView.setText(values[position]);
//Here From Fonts array take Typeface
vHolder.textView.setTypeface(fonts[position]);
return convertView;
}
}
Set Adapter
ListView fontsListView = (ListView) this.view.findViewById(R.id.MenuLayout);
AdapterFontViewHolder listAdapter = new AdapterFontViewHolder(getActivity(), R.layout.fonts_simple_row, fontsList);
fontsListView.setAdapter(listAdapter);
I have a ListView that displays a list of TextViews. I want each TextView to be displayed in their appropriate typeface. The names of the fonts which appear in the ListView as part of the fonts String[] array are spelled the same as when the Typeface is created.
ListView fontsListView = (ListView) this.view.findViewById(R.id.MenuLayout);
final String[] fonts = new String[] {
"Aclonica",
"Amino-Regular",
};
ArrayList<String> fontsList = new ArrayList<String>();
fontsList.addAll( Arrays.asList(fonts) );
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.fonts_simple_row, fontsList);
final TextView[] textViewArray = new TextView[0];
for( int i = 0; i <= fonts.length - 1; i++) {
textViewArray[i].setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/" + fonts[i] + ".ttf"));
}
fontsListView.setAdapter(listAdapter);
And the XML:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:id="#+id/rowTextView"
android:textSize="20sp"
android:gravity="center_horizontal"
android:padding="10dp"
android:typeface="sans"
android:fontFamily="sans-serif-light" />
Why can't I get each TextView in the ListView its proper Typface?
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.fonts_simple_row,value) {
#Override
public View getView(int position, View convertView,ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(R.id.rowTextView);
textView.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/" + fonts[position] + ".ttf"));
return view;
}
};
fontsListView.setAdapter(arrayAdapter);
You should use a custom array adapter and set font on get view method.
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
String s = values[position];
if (s.equals("Arial")) {
// set textView font to arial
} else if (s.equals("Roboto")){
// set textView font to roboto
}
return rowView;
}
}
Create Typeface only one time and use it. Avoid creating typeface in getview method.it will create every time you scroll if you are not maintaining reusability.
Use ViewHolder pattern for better performance in listview
Below i have try to given both things. I have given explanation in comment also. Please check it if it may help you.
public class AdapterFontViewHolder extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
Typeface typefaceArial, typefaceRoboto;
private Typeface[] fonts;
public AdapterFontViewHolder(Context context, String[] values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
// Create Typeface only once and use it.. Path you can change as per your directory
fonts = new Typeface[]{
Typeface.createFromAsset(context.getAssets(), "fonts/Aclonica.ttf"),
Typeface.createFromAsset(context.getAssets(), "fonts/Amino-Regular.ttf"),
};
}
public class ViewHolder {
TextView textView;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vHolder;
if (convertView == null) {
// Create Instnce of view if its null & store object in ViewHolder (a class)
vHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.rowlayout, parent, false);
vHolder.textView = (TextView) convertView.findViewById(R.id.label);
//Set ViewHolder instance in convertview in a tag as a object
convertView.setTag(vHolder);
} else {
// reuse as already converview is instansiated & it holds ViewHolder instance in tag
vHolder = (ViewHolder) convertView.getTag();
}
vHolder.textView.setText(values[position]);
//Here From Fonts array take Typeface
vHolder.textView.setTypeface(fonts[position]);
return convertView;
}
}
Set Adapter
ListView fontsListView = (ListView) this.view.findViewById(R.id.MenuLayout);
AdapterFontViewHolder listAdapter = new AdapterFontViewHolder(getActivity(), R.layout.fonts_simple_row, fontsList);
fontsListView.setAdapter(listAdapter);
I have a special class, a custom adapter for my ListView and I need to get some data from another Activity. But my implementation of the method GetIntent()GetExtras() isn't working. What is wrong?
Here is my custom adapter code:
public class CustomAdapter extends ArrayAdapter<String> {
int myColor,myWidth;
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// return super.getView(position, convertView, parent);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.ColorTextButton);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageViewIcon);
TextView textView1 = (TextView) rowView.findViewById(R.id.HelpButton);
textView.setText(values[position]);
String s = values[position];
System.out.println(s);
if (s.equals("Monday")) {
imageView.setImageResource(R.drawable.arrow2);
textView.setBackgroundColor(Color.YELLOW);
} else if (s.equals("Wednesday")) {
imageView.setImageResource(R.drawable.arrow2);
textView1.setBackgroundColor(Color.GREEN);
} else if (s.equals("Friday")) {
imageView.setImageResource(R.drawable.arrow2);
} else {
imageView.setImageResource(R.drawable.arrow);
}
return rowView;
}
}
If you want the data to be accessable From a few activities you should think about use a singleton data Class. so in stead of trying to get pass the data from your adapter to a second activity both the adaper and the Activity use the same method to get the data class
public class DataProvider {
private static DataProvider instance;
public static DataProvider getInstance()
{
if(null == instance){instance = new DataProvider();}
return instnace;
}
public String[] getObjects(){
return this.myStringArray;
}
// add more methods in here to retrieve and count your data as you need
}
then in your Activity you can
// somewhere
myStingArrayInMyActivity = DataProvider.getInstance().getObjects()
also in you adapter
you can do
public class CustomAdapter extends ArrayAdapter<String> {
int myColor,myWidth;
private final Context context;
private final String[] values;
public CustomAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// your code
textView.setText(DataProvider.getInstance.getObjects[position]);
String s = DataProvider.getInstance.getObjects[position];
/// your code
return rowView;
}
}
I have a row which is represented by an image, some text and a CheckBoxes. Whenever I'm trying to use the OnItemClickListener for the rows, the event won't fire up when clicking the CheckBoxes.
I also tried checkbox.onCheckedChangedListener but it gives me Null Pointer at findViewByID. I checked, the ID I am looking for is alright, no typos in there.
I'd like to make usage of this OnItemClickListener so later on I can play with the checkboxes. Any ideas?
Code:
ADAPTER:
public class FilterAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> mFilters;
private ArrayList<Integer> mPictures;
private Typeface Bebas, DroidSans;
public FilterAdapter(Context context, ArrayList<String> filters, ArrayList<Integer> pictures) {
this.mContext = context;
this.mFilters = filters;
this.mPictures = pictures;
}
#Override
public int getCount() {
return mFilters.size();
}
#Override
public Object getItem(int position) {
return mFilters.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.category_filter_item, null);
}
DroidSans = Typeface.createFromAsset(mContext.getAssets(), "fonts/DroidSans.ttf");
ImageView filter_img = (ImageView) convertView.findViewById(R.id.category_picture);
TextView filter_category = (TextView) convertView.findViewById(R.id.filter_category);
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.check_box);
filter_category.setTypeface(DroidSans);
filter_category.setText(mFilters.get(position));
filter_img.setBackgroundResource(mPictures.get(position));
return convertView;
}
}
Class where using the Adapter:
public class CheckinFilters extends Fragment {
private ListView mListView;
private FilterAdapter filterAdapter;
private ArrayList<String> l = new ArrayList<String>();
private ArrayList<Integer> drawables = new ArrayList<Integer>();
private Typeface Bebas;
private ArrayList<Integer> checkboxes = new ArrayList<Integer>();
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
public CheckinFilters() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_checkin_filters, container, false);
l.add("Chill");
l.add("Eat");
l.add("Explore");
l.add("Move");
l.add("Party");
l.add("Whatever");
drawables.add(R.drawable.category_chill);
drawables.add(R.drawable.category_eat);
drawables.add(R.drawable.category_explore);
drawables.add(R.drawable.category_move);
drawables.add(R.drawable.category_party);
drawables.add(R.drawable.category_whatever);
mListView = (ListView) view.findViewById(R.id.filter_list);
filterAdapter = new FilterAdapter(view.getContext(), l, drawables);
mListView.setAdapter(filterAdapter);
sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
Bebas = Typeface.createFromAsset(view.getContext().getAssets(), "fonts/BebasNeue.otf");
TextView mainHeader = (TextView) view.findViewById(R.id.filters_text);
mainHeader.setTypeface(Bebas);
SearchView filter_categories = (SearchView) view.findViewById(R.id.filter_categories);
int searchImgID = getResources().getIdentifier("android:id/search_button", null, null);
ImageView searchViewHint = (ImageView) filter_categories.findViewById(searchImgID);
searchViewHint.setImageResource(R.drawable.ab_icon_search);
int searchPlateID = filter_categories.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
View searchPlateView = filter_categories.findViewById(searchPlateID);
if (searchPlateView != null) {
searchPlateView.setBackgroundResource(R.drawable.search_location_shape);
}
int searchViewID = filter_categories.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) filter_categories.findViewById(searchViewID);
textView.setTextColor(Color.GRAY);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.check_box);
return view;
}
private void save() {
sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.putInt("first", checkboxes.size());
for (int i = 0; i < checkboxes.size(); i++) {
editor.remove("Status_" + i);
editor.putInt("Status_" + i, checkboxes.get(i));
}
editor.commit();
}
private void load() {
sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
int size = sharedPreferences.getInt("first", 0);
for (int i = 0; i < size; i++) {
checkboxes.add(sharedPreferences.getInt("Status_" + i, 0));
}
}
#Override
public void onPause() {
super.onPause();
save();
}
#Override
public void onResume() {
super.onResume();
load();
}
}
Layout I'm inflating for Adapter:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ECF0F1">
<ImageView
android:id="#+id/category_picture"
android:layout_width="70dp"
android:layout_height="60dp"
android:background="#drawable/sm_profile"/>
<TextView
android:id="#+id/filter_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/category_picture"
android:padding="10dp"
android:text="Party"
android:textColor="#color/enloop_dark_gray"
android:textSize="18dp"/>
<CheckBox
android:id="#+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:padding="10dp"/>
</RelativeLayout>
</RelativeLayout>
Instead of using OnItemClickListener in CheckinFilters.java, you should add that code in getView() method of FilterAdapter. You are actually adding checkbox for every row but the id for all checkbox is same, so you can't apply customized code for every checkbox.
To be able to make use of all checkboxes you can put checkbox code in getView() method because it also contains an argument called position. So you can just get the position and apply OnItemClickListener to every single checkbox. That way it will work.
public class FilterAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> mFilters;
private ArrayList<Integer> mPictures;
private Typeface Bebas, DroidSans;
public FilterAdapter(Context context, ArrayList<String> filters, ArrayList<Integer> pictures) {
this.mContext = context;
this.mFilters = filters;
this.mPictures = pictures;
}
#Override
public int getCount() {
return mFilters.size();
}
#Override
public Object getItem(int position) {
return mFilters.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.category_filter_item, null);
}
DroidSans = Typeface.createFromAsset(mContext.getAssets(), "fonts/DroidSans.ttf");
ImageView filter_img = (ImageView) convertView.findViewById(R.id.category_picture);
TextView filter_category = (TextView) convertView.findViewById(R.id.filter_category);
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.check_box);
filter_category.setTypeface(DroidSans);
filter_category.setText(mFilters.get(position));
filter_img.setBackgroundResource(mPictures.get(position));
if(position == 0) {
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
else {
}
}
});
}
//Similarly add OnClickListener to other checkboxes.
return convertView;
}
}