I have some trouble with layout in my list activity.
My list contain separators and text rows
SetupActivity extend ListActivity
private MyCustomAdapter mAdapter;
TextView selection;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mAdapter = new MyCustomAdapter();
mAdapter.addItem("Help/FAQ");
mAdapter.addSeparatorItem("Connection to Server");
// mAdapter.addItem("Connection");
// mAdapter.addItem("Network");
// mAdapter.addItem("config");
// mAdapter.addItem("User");
// mAdapter.addItem("pass");
// mAdapter.addItem("Email");
// mAdapter.addItem("PlatForm");
mAdapter.addSeparatorItem("Consumption");
// mAdapter.addItem("100%");
mAdapter.addSeparatorItem("Map");
// mAdapter.addItem("Map rotation");
// mAdapter.addItem("auto Zoom");
// mAdapter.addItem("Measure Units");
// mAdapter.addItem("Show Heading");
// mAdapter.addItem("Compass North");*/
mAdapter.addFooterItem(getString(R.string.setup_note_map));
mAdapter.addSeparatorItem("Support");
mAdapter.addItem("About");
/*
* mAdapter.addItem("Contact Us"); mAdapter.addItem("Tutorial");
* mAdapter.addItem("Setup Wizard");
*/
mAdapter.addSeparatorItem("Blogs");
mAdapter.addFooterItem(getString(R.string.setup_note_blogs));
setListAdapter(mAdapter);
// selection = (TextView) findViewById(R.id.text);
}
public void onListItemClick(ListView parent, View view, int position,
long id) {
parent.getChildAt(position).setBackgroundColor(position);
if (position == 0) {
Intent myIntent = new Intent(SetupActivity.this,
WebviewHandlerActivity.class);
myIntent.putExtra("ressource", "help");
SetupActivity.this.startActivity(myIntent);
} else if (position == 6) {
Intent myIntent = new Intent(SetupActivity.this,
AboutActivity.class);
SetupActivity.this.startActivity(myIntent);
}
}
// Adapter Class
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 2;
private static final int TYPE_SEPARATOR = 0;
private static final int TYPE_FOOTER = 1;
private static final int TYPE_MAX_COUNT = TYPE_ITEM + 1;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
private TreeSet<Integer> mFooterSet = new TreeSet<Integer>();
public MyCustomAdapter() {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
public void addFooterItem(final String item) {
mData.add(item);
// save separator position
mFooterSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
if (mSeparatorsSet.contains(position))
return TYPE_SEPARATOR;
else if (mFooterSet.contains(position))
return TYPE_FOOTER;
return TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public String getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1, null);
holder.textView = (TextView) convertView
.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView) convertView
.findViewById(R.id.textSeparator);
break;
case TYPE_FOOTER:
convertView = mInflater.inflate(R.layout.footer, null);
holder.textView = (TextView) convertView
.findViewById(R.id.note);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
my xml item1 & item2 contain a LinearLayout with a TextView inside
and my footer.xml only a textView
My problem is when i click on a row it doesn't get orange to say that i clicked on it except my footer...(the one i don't want)
So i figure out it's because it's not in a LinearLayout so i tried to put off the LinearLayout of item1.xml but i can't compile anymore.
Does someone can explain to me how to get my row with the click Animation and not on my footers ?
Cheers
You are not getting clicked color because you have set a background color `
setBackgroundColor
You need to set a statelistDrawable as background
Edit: You need to set a drwable something like this
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_img" android:state_pressed="true"/>
<item android:drawable="#drawable/default_img"/>
</selector>
Related
I have a ListView in AlertDialog which contains more button on each row & it will popup a list with delete option. When I click on delete option, its deleting the last item. In CustomAdaptor I tried a notifyDataSetChanged() after deleting item from ArrayList. Here it always deletes the last item irrespective of item position.
CustomAdaptor Code
public class CustomAdapter extends BaseAdapter {
Context context;
List<String> lrowItems ;
ArrayList<String> listItems;
String className;
CalHelper calHelper;
Settings settings;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, ArrayList<String> listItems) {
this.context = applicationContext;
this.listItems = listItems;
this.className = context.getClass().getSimpleName();
this.calHelper = new CalHelper(applicationContext);
this.settings = new Settings(applicationContext);
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
if(listItems != null) {
return listItems.size();
}else{
return 0;
}
}
#Override
public Object getItem(int position) {
return listItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
//With following overrides Prevent listview item will not mix randomly
//and convertView provided is of the appropriate type.
#Override
public int getViewTypeCount() {
if(getCount() > 0){ //for no records in returning view
return getCount();
}else{
return super.getViewTypeCount();
}
}
#Override
public int getItemViewType(int position) {
return position;
}
/* private view holder class for holding calculation history*/
private class HistoryViewHolder {
TextView more;
TextView expression;
TextView result;
TextView shortNote;
TextView expTime;
}
/* private view holder class for holding GST calculations history*/
private class GstHistoryViewHolder {
TextView amount;
TextView gst;
TextView total;
TextView expTime;
ImageView share;
}
/* private view holder class */
private class BMIViewHolder {
TextView bmiScore;
TextView bmiDate;
TextView bmiWeight;
}
#Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//final int pos = position;
if(className.equals("MainActivity")) {
final HistoryViewHolder holder = new HistoryViewHolder();
final String expString = listItems.get(position);
final String expre = expString.substring(0, expString.indexOf(":"));
final String expreTime = expString.substring(expString.indexOf(":") + 1, expString.length());
final String result = expre.substring(expre.indexOf("=")+1);
String timePattern = calHelper.calHistoryTimePattern(expreTime);
SimpleDateFormat localDateFormat = new SimpleDateFormat(timePattern);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview_row, null);
holder.more = (TextView) convertView
.findViewById(R.id.more);
holder.expression = (TextView) convertView.findViewById(R.id.expression);
holder.result = (TextView) convertView.findViewById(R.id.result);
holder.shortNote = (TextView) convertView.findViewById(R.id.shortNote);
holder.expTime = (TextView) convertView.findViewById(R.id.expTime);
holder.expression.setText(expre.substring(0, expre.indexOf("=")));
holder.result.setText(result);
String time = localDateFormat.format(new Date(expreTime));
holder.expTime.setText(time);
holder.shortNote.setText(settings.getHistoryNote(context,expString));
convertView.setTag(holder);
final TextView btnMore = (TextView) holder.more;
btnMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showPopup(v, position, expString, expre, expreTime, holder.shortNote);
}
});
}
}
return convertView;
}
public void showPopup(View v, final int position, final String expString, final String expression, final String expTime, final TextView shortNote) {
final PopupMenu popup = new PopupMenu(context, v);
//truncate title with ellipsis for more characters
final String noteTitle = expression.length()>10? expression.substring(0, 10) + "..." : expression;
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.history_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Intent intent = new Intent(context, MainActivity.class);
int itenId = item.getItemId();
switch (itenId){
case R.id.mnuInsertExpression:
//some code here
break;
case R.id.mnuHisNote:
//some code here
break;
case R.id.mnuCopyHisRecord:
calHelper.copyToClipboard(expression);
break;
case R.id.mnuShareHisRecord:
calHelper.shareText(expression);
break;
case R.id.mnuDeleteHisRecord: //This is where I am deleting item
System.out.println("Pos:"+position);
listItems.remove(position);
notifyDataSetChanged();
Collections.reverse(listItems); //sort descending by time again
settings.clearSharedPre(context,expString); //remove note attached to this expression from ShredPref
settings.saveArrayList(context, listItems, "EXP_HISTORY");
intent.putExtra("HISTORY_RECORD_DELETED", true);
Toast.makeText(context,"Record removed from history", Toast.LENGTH_SHORT).show();
context.startActivity(intent);
break;
}
return true;
}
});
popup.show();
}
Any help is greatly appreciated.
Try this getView():
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
HistoryViewHolder holder;
if(className.equals("MainActivity")) {
final String expString = listItems.get(position);
final String expre = expString.substring(0, expString.indexOf(":"));
final String expreTime = expString.substring(expString.indexOf(":") + 1, expString.length());
final String result = expre.substring(expre.indexOf("=")+1);
String timePattern = calHelper.calHistoryTimePattern(expreTime);
SimpleDateFormat localDateFormat = new SimpleDateFormat(timePattern);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.listview_row, null);
holder = new HistoryViewHolder();
holder.more = (TextView) convertView.findViewById(R.id.more);
holder.expression = (TextView) convertView.findViewById(R.id.expression);
holder.result = (TextView) convertView.findViewById(R.id.result);
holder.shortNote = (TextView) convertView.findViewById(R.id.shortNote);
holder.expTime = (TextView) convertView.findViewById(R.id.expTime);
holder.more.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (int) v.getTag();
showPopup(v, pos, expString, expre, expreTime, holder.shortNote);
}
});
}else{
holder = (HistoryViewHolder)convertView.getTag();
}
holder.expression.setText(expre.substring(0, expre.indexOf("=")));
holder.result.setText(result);
String time = localDateFormat.format(new Date(expreTime));
holder.expTime.setText(time);
holder.shortNote.setText(settings.getHistoryNote(context, expString));
holder.more.setTag(position);
convertView.setTag(holder);
}
return convertView;
}
Hope that helps!
Its because you are using the position of the getView parameter, that will always be whatever the last index is at the time you go to use it.
Instead you need to add that position to the view, possibly in your convertView is what I usually do (the tag can be anything you want), do convertView.setTag(position) in your getView method as you are creating each items view, and then in your delete method within showpopup you can do (int)view.getTag() to get the position to remove from your dataset.
You should add the postion to the button using setTag() and get the position when passing it to showPopup() using getTag(). You can do the following.
// ....
final TextView btnMore = (TextView) holder.more;
btnMore.setTag(position); // here add the position to the button using setTag().
btnMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// here you can get position your button using v.getTag().
int pos = (int) v.getTag();
showPopup(v, pos, expString, expre, expreTime, holder.shortNote);
}
});
I am new to android and working on web view demo, I have implemented it. But the problem with me is when I check any check box and scrolling the list view , The checked position changes.
I have tried some links from stack over flow with no luck, SO I request someone can help me to resolve this issue, please
My adapter is as below,
adapter
public class FilterAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public FilterAdapter(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public String getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.raw_filter, null);
holder.textView = (TextView) convertView.findViewById(R.id.tv_filter);
holder.chk_filter = (CheckBox) convertView.findViewById(R.id.chk_filter);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.raw_headr, null);
holder.textView = (TextView) convertView.findViewById(R.id.tv_hdr);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
class ViewHolder {
public TextView textView;
public CheckBox chk_filter;
}
add clickListener in your getView() method, it will fix your set checked box in the entire listView like this:
checkBoxSelected.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
instrumentClickListener.onCheckBoxClick(instrumentDTO, checkBoxSelected.isChecked(), position);
Log.d(TAG, "checked instruments is check: " + checkBoxSelected.isChecked() + " \ninstrument: " + position);
}
});
I have an Adapter class that implements ListView with headers.
//Adapter Class
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private static final int TYPE_GRAY_SEPARATOR = 2;
private TreeSet<Integer> mGraySeparatorsSet = new TreeSet<Integer>();
private ArrayList<ContentWrapper> mData = new ArrayList<ContentWrapper>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
}
public void addItem(ContentWrapper value) {
mData.add(value);
notifyDataSetChanged();
}
public void addSeparatorItem(ContentWrapper value) {
mData.add(value);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
public ContentWrapper getItem(int position) {
return mData.get(position);
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public long getItemId(int position) {
Log.v("getItemId Position", ""+position);
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.white, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.black, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
} holder.textView.setText(mData.get(position).getItem());
if (type == TYPE_ITEM) {
holder.textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder x = new AlertDialog.Builder(getActivity());
x.setIcon(R.drawable.ic_launcher)
.setTitle(mData.get(position).getItem())
.setMessage(mData.get(position).getItemDescription())
.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg,
int arg1) {
}
});
AlertDialog a = x.create();
a.show();
}
return null;
}
});
} else {
holder.textView.setOnClickListener(null);
}
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
In this I truly understand the implementation of getView method (ViewHolder,convertView,etc).
The above code runs smoothy but I do not understand that why we are using the methods
addItem
addSeparatorItem
getItemViewType
getViewTypeCount
getCount
getItemId
Can Anyone explain me the scenario clearly !
Thanks in advance..
These all are the call back methods. You can have the clear definition of these methods in below mentioned link.
http://developer.android.com/reference/android/widget/BaseAdapter.html
Let me give you an example of getItem()
Whenever new listview row is created, adapte willl get item details from this call back method.
Similarly whenever list is created, getCount() is called.
Suppose you have 12 items in listview. But you pass 10 in getCount(). it will show only ten items in list view.
I have implemented a ListView with headers from sqlite in a separate project which is like this:
When I implemented onClick() in a position of Slider Menu, It is displayed in Slider Menu rather than in the background View
onCreate() code snippet
if(position == 3)
{
mAdapter = new MyCustomAdapter();
mAdapter.addSeparatorItem(q.get(0).getA_name());
mAdapter.addItem(q.get(0).getAS_name());
for (int i = 1; i < 460; i++) {
if (!(q.get(i).getA_name().trim().equals(q.get(i-1).getA_name().trim()))) {
mAdapter.addSeparatorItem(q.get(i).getA_name());
c++;
}
mAdapter.addItem(q.get(i).getAS_name());
}
setListAdapter(mAdapter);
toggleMenu(arg1);
}
Adapter class
//Adapter Class
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public String getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.activity_main1, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
holder.textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder nointernetconnection = new AlertDialog.Builder(
temp);
nointernetconnection
.setIcon(R.drawable.ic_launcher)
.setTitle(q.get(position-1).getAS_name())
.setMessage(q.get(position-1).getDesc_art())
.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg,
int arg1) {
}
});
AlertDialog a = nointernetconnection.create();
a.show();
}
});
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.activity_main2, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
So, MyCustomAdapter class is dispalying the list in slider menu. But this list should not be displayed in slider menu rather it should be displayed behind slider menu. I hope the problem is clear. Please tell my mistake.
The setlistadapter method you are calling is being called for the slider rather than your list view on the activity/fragment in the background.
do a ListView list = (ListView) getactivity().findViewById(R.id.yourListViewName) or something like this, and then set the list adapter.
I tried make list view has TextView and CheckBox. It is worked fine but when select CheckBox in row another CheckBox in another row selected too.
Example: if I checked first row and scroll down I found another row selected too.
This my Adapter Code
public class ReadersrListAdapter extends BaseAdapter{
private static final int TYPE_SEPARATOR = 0;
private static final int TYPE_ITEM = 1;
private static final int TYPE_MAX_COUNT = 2;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater mInflater=null;
public ReadersrListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
mInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return !data.get(position).get(UtiliShare.KEY_TITLE).startsWith("-");
}
#Override
public int getItemViewType(int position) {
return data.get(position).get(UtiliShare.KEY_TITLE).startsWith("-") ? TYPE_SEPARATOR : TYPE_ITEM;
}
#Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
ViewHolder2 holder2 = null;
int type = getItemViewType(position);
if (convertView == null) {
//holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.reader_list_item, null);
holder.textView = (TextView)convertView.findViewById(R.id.readerTitle);
holder.textView.setTypeface(UtiliShare.getTf());
convertView.setTag(holder);
break;
case TYPE_SEPARATOR:
holder2 = new ViewHolder2();
convertView = mInflater.inflate(R.layout.reader_list_devider, null);
holder2.textView = (TextView)convertView.findViewById(R.id.readerTitle);
holder2.textView.setTypeface(UtiliShare.getTf());
convertView.setTag(holder2);
break;
}
//convertView.setTag(holder);
} else {
if(type==TYPE_ITEM) holder = (ViewHolder)convertView.getTag();
else holder2 = (ViewHolder2)convertView.getTag();
}
HashMap<String, String> curdata = new HashMap<String, String>();
curdata = data.get(position);
String txt = curdata.get(UtiliShare.KEY_TITLE);
if(type == TYPE_SEPARATOR){
txt = txt.replace("-", "");
holder2.textView.setText(txt);
}else{
//if(position <= 100) System.out.println("getView " + position + " " + convertView + " type = " + type);
holder.star = (CheckBox) convertView.findViewById(R.id.btn_Fav);
holder.star.setOnCheckedChangeListener(((ReadersListActivity) this.activity).mStarCheckedChanceChangeListener);
holder.textView.setText(txt);
}
//holder.textView.setText(txt);
return convertView;
}
private static class ViewHolder {
public CheckBox star;
public TextView textView;
}
private static class ViewHolder2 {
public TextView textView;
}
}
And this OnCheckedChangeListener in activity
public OnCheckedChangeListener mStarCheckedChanceChangeListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Cyril: Not implemented yet!
final int position = list.getPositionForView(buttonView);
Toast.makeText(ReadersListActivity.this, ""+position, Toast.LENGTH_SHORT).show();
}
};
How Can I fix this???
Thanks.
As you may know the layouts are reused as you scroll, so you need to track whether each row should be checked yourself. Let's add a new member to your Adapter:
private SparseBooleanArray checked = new SparseBooleanArray();
And a new method:
public void toggleCheck(int position) {
boolean state = expanded.get(position, false);
expanded.put(!state);
}
Now call our new method in onCheckedChanged():
final int position = list.getPositionForView(buttonView);
adapter.toggleCheck(position);
Lastly let's tweak getView() to change the checked state (and a few other things):
// Don't create a new HashMap that you'll never use
//HashMap<String, String> curdata = new HashMap<String, String>();
HashMap<String, String> curdata = data.get(position);
String txt = curdata.get(UtiliShare.KEY_TITLE);
if(type == TYPE_SEPARATOR){
txt = txt.replace("-", "");
holder2.textView.setText(txt);
}else{
//if(position <= 100) System.out.println("getView " + position + " " + convertView + " type = " + type);
// These two lines should be in `if(convertView == null)` with the others
holder.star = (CheckBox) convertView.findViewById(R.id.btn_Fav);
holder.star.setOnCheckedChangeListener(((ReadersListActivity) this.activity).mStarCheckedChanceChangeListener);
// Set the appropriate values
holder.textView.setText(txt);
holder.star.setChecked(expanded.get(position, false));
}