I have a list view whose adapter is in a different class in a different package. Now I have to get the data from database then I set Adapter for my list view using this data. So I have created an ArryList and pass this in the constructor of Adapter while seeting it for the list view. But the problem is that the data is repeating. eg.- there are 12 distinct Strings in the arraylist but what I get is -first five elements in order and after that the same five are being repeated. The count of data is always right but the position will always be 0,1,2,3,4 . I can not understand what the problem is . here is the code -
public class CheckboxAdapter extends BaseAdapter{
LayoutInflater inflater ;
ArrayList<String> mData = new ArrayList<String>();
//constructor for lesion adapter
public CheckboxAdapter (Context context, ArrayList<String> data){
inflater = LayoutInflater.from(context);
mData = data;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
Log.v("pos", ""+position);
convertView = inflater.inflate(R.layout.e_lesion_liststyle, null);
final CheckBox cb = (CheckBox) convertView.findViewById(R.id.disease_lesion_checkbox);
cb.setText(mData.get(position));
cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
cb.setButtonDrawable(R.drawable.check_box_1);
}
});
}//end of if condition
return convertView;
}
try this.. just write all code out of the condition expect inflate code..
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
Log.v("pos", ""+position);
convertView = inflater.inflate(R.layout.e_lesion_liststyle, null);
} //end of if condition
final CheckBox cb = (CheckBox) convertView.findViewById(R.id.disease_lesion_checkbox);
cb.setText(mData.get(position));
cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
cb.setButtonDrawable(R.drawable.check_box_1);
}
});
return convertView;
}
Related
I have list view with check box and text view. When i checked check box, change the text in same row. This way i will check all the check boxes in list view and change the text in every row. Can any one explain with sample code?
public class DialogAdapter extends BaseAdapter{
//private LayoutInflater inflater = null;
ArrayList<String> timeList = new ArrayList<String>();
Context context;
public DialogAdapter(Context context, ArrayList<String> timeList) {
// TODO Auto-generated constructor stub
this.context = context;
this.timeList = timeList;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
//return timings.size();
return timeList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return timeList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//String timing = "10-30,14-08,16-30,19-00";
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.dialog_row_list,null);
//timeListView.setVisibility(convertView.VISIBLE);
TextView txt_time=(TextView) convertView.findViewById(R.id.text_times);
txt_time.setText(timeList.get(position).toString());
EditText comments=(EditText)convertView.findViewById(R.id.editText_comment);
final TextView txt_Action=(TextView) convertView.findViewById(R.id.textView_act);
CheckBox cBox= (CheckBox) convertView.findViewById(R.id.checkBox_action);
cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#SuppressLint("ResourceAsColor")
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int getPosition = (Integer) buttonView.getTag();
timeList.get(getPosition).setSelected(buttonView.isChecked());
Toast.makeText(context, "position"+getPosition, 0).show();
if(isChecked){
txt_Action.setText("finished");
txt_Action.setTextColor(R.color.Green);
}
else{
txt_Action.setText("UnChecked");
txt_Action.setTextColor(R.color.Red);
}
}
});
return convertView;
}
}
this is my code please check n give solution how to change listview text when check checkbox in listview???
in the getView method of your ListView adapter:
if(yourCheckBox.isChecked)
yourTextView.setText("text if checkBox is checked.");
else
yourTextView.setText("text if checkBox is not checked.");
There is view before checkboxes and texviews that is listview ,if u want to access textviews from listview please make sure you are accessing from listview one item layout
Like
TextView txtvw=findViewByID(ur_layout_of_list_item).findViewByID(ur_textview);
after that changes will be done.
First I will suggest you to use ViewHolder pattern for listview adapter so that you don't need to do findViewById() everytime you scroll your list this will prevent your listview from performance slow down and also I would prefer to use setOnClickListener() over CheckedChangeListener().
Below is the sample code you can try
public class CustomAdapter extends BaseAdapter {
private Context context;
public static ArrayList<Item> modelArrayList;
public CustomAdapter(Context context, ArrayList<Item> modelArrayList){
this.context = context;
this.modelArrayList = modelArrayList;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
return modelArrayList.size();
}
#Override
public Object getItem(int position) {
return modelArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.layout_list_view_row_items, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Item currentItem = (Item) getItem(position);
if(currentItem.isChecked())
viewHolder.tvName.setText("Checked);
else
viewHolder.tvName.setText("Unchecked);
viewHolder.checkBox.setChecked(currentItem.isChecked());
viewHolder.checkBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final boolean checked = currentItem.isChecked();
if(checked){
viewHolder.checkBox.setChecked(!checked);
viewHolder.tvName.setText("Unchecked");
currentItem.setChecked(!checked);
}else{
viewHolder.checkBox.setChecked(checked);
viewHolder.tvName.setText("Checked");
currentItem.setChecked(checked);
}
}
});
return convertView;
}
private class ViewHolder {
protected CheckBox checkBox;
private TextView tvName;
public ViewHolder(View view) {
checkBox = (CheckBox)view.findViewById(R.id.checkbox);
tvName = (TextView) view.findViewById(R.id.textView);
}
}
}
Here is my adapter class:-
public class CustomAdapter extends BaseAdapter {
Context c;
CustomAdapter(Context c)
{
this.c=c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int pos=position;
LayoutInflater inflater=LayoutInflater.from(c);
View v=inflater.inflate(R.layout.layout_list_item, parent, false);
ImageButton image_button=v.findViewById(R.id.imagebutton);
image_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(c, "Image Button clicked:" + pos, Toast.LENGTH_SHORT).show();
}
});
return v;
}
}
How do I make all imagebuttons clickable? I tried searching for answers and as per an answer given here: how to make an imageview clickable in an listview I tried but only my first row button is clickable. Please help.
In your custom adapter constructor
LayoutInflater mInflater;
CustomAdapter(Context c)
{
mInflater = LayoutInflater.from(c);
// initialize inflater in the constructor.
// need not initialize everytime getView is called.
}
Use a View Holder
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
static class ViewHolder
{
ImageButton ib;
}
#Override
public View getView(int position, View item, ViewGroup parent) {
ViewHolder holder;
if(item == null){
item = mInflater.inflate(R.layout.elementos_lista_temas, null);
holder = new ViewHolder();
holder.ib = (ImageButton) item.findViewById(R.id.imagebutton);
item.setTag(holder);
}
else{
holder = (ViewHolder)item.getTag();
}
return item;
}
Then in your activity class
ListView lv = (ListView) findViewById(R.id.listview);
CustomAdapter cus = new CustomAdapter(this);
lv.setAdapter(cus);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Toast.makeText(ActivityName.this, "Image Button clicked:" + itemPosition, Toast.LENGTH_SHORT).show();
}
});
As told by "the World of Listview-2010" see below link show how to use viewholder class with custom listview in android http://impressive-artworx.de/2011/list-all-installed-apps/ Hope this helps you.
My Custom ListView Adapter class like this.
I want to get the index number of an item once an item has been pressed(onclick). How can I get the index number and pass it to a int.. The code where I'll get the index number is after the onCreate method. Please help me!
// On holder.txtstormName_Nice button click i want to get selected item index.
public class ListViewCustomAlerts extends BaseAdapter {
ArrayList<HurricaneBeanClass> itemList;
public Activity context;
public LayoutInflater inflater;
public ListViewCustomAlerts(Activity context,ArrayList<HurricaneBeanClass> arraylist_List)
{
super();
this.context = context;
this.itemList = arraylist_List;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount()
{
return itemList.size();
}
#Override
public Object getItem(int position)
{
return itemList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
p =position;
final ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.hurricane_row, null);
holder.txtstormName_Nice=(TextView)convertView.findViewById(R.id.hurricaneNameRowTextView);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
HurricaneBeanClass bean = (HurricaneBeanClass) itemList.get(position);
holder.txtstormName_Nice.setText(bean.getStormName_Nice());
holder.txtstormName_Nice.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//here i want item index.................
HurricaneBeanClass bean = (HurricaneBeanClass) itemList.get(position);
HurricaneActivity.curlat=bean.getCurentlat();
HurricaneActivity.curlon=bean.getCurentlon();
...
}
});
return convertView;
}
}
public static class ViewHolder
{
TextView txtstormName_Nice;
}
List View Item Position is already coming in argument list.
public View getView(int position, View convertView, ViewGroup parent) {
If you want de id of element you can write in getView:
getItemId(position)
In getView() method you can get it
public View getView(final int position, View convertView, ViewGroup parent) {
.........
.....
System.out.println("Index:"+position);
}
So, here position is your index
Below I have written my code actually when I was scrolling my listview the checkbox takes its original position, any help by code will be appreciated.
cp.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
cp.setChecked(true);
cx.setChecked(false);
ck.setChecked(false);
int a=1;
String c=getListView().getItemAtPosition(position).toString();
updatetable(c, "FULL");
//Toast.makeText(getApplicationContext(), c+"full", Toast.LENGTH_LONG).show();
// Toast.makeText(getApplicationContext(), "year", Toast.LENGTH_LONG).show();
}
});
cx.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
cx.setChecked(true);
cp.setChecked(false);
ck.setChecked(false);
int a=1;
String c=getListView().getItemAtPosition(position).toString();
updatetable(c, "CRITICAL");
//Toast.makeText(getApplicationContext(), c+"ok", Toast.LENGTH_LONG).show();
}
});
ck.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ck.setOnCheckedChangeListener(null);
// TODO Auto-generated method stub
ck.setChecked(true);
cp.setChecked(false);
cx.setChecked(false);
int a=1;
String c=getListView().getItemAtPosition(position).toString();
updatetable(c, "FINISH");
//Toast.makeText(getApplicationContext(), c+"fineshed", Toast.LENGTH_LONG).show();
}
});
Your question is not clear, but I think I got the problem.
I think this is not a matter of CheckBox. I think you're doing something wrong with your adapter.
public class MyAdapter extends BaseAdapter {
private List<Something> mList;
private Context mContext;
public MyAdapter(Context context, List<Something> list) {
mContext = context;
mList = list;
}
#Override
public int getCount() {
return mList.getSize();
}
#Override
public Object getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_layout, null);
}
CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
/* Here you should set your checkbox state */
}
}
I think your problem is convertView.
When you scroll down your ListView the popped up items are not destroyed, but recycled. The recycled item is given you back as convertView. convertView is not cleaned, hence if the CheckBox in the popped out item was checked, the CheckBox in convertView is checked.
You have to maintain a List<Boolean> states where you save the state of the CheckBox of all your items. Then:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_layout, null);
}
CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
checkBox.setChecked(states.get(position));
}
I haven't your code, so the example above is as general as possible. If you let us see the code we can help you.
Given a listview with many data fields per row, how can we separate only one field (say an id) from the row withonClickListener()
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object dataRow = listView.getItemAtPosition(position);
Log.e("hi, this is the full row of data, i just want 1 of the fields", dataRow);
}
This can be achieved by custom listview by extending BaseAdapter to a class.. In the class you can
Inside oncreate :
listview.setAdapter(new newListview(this); //listview is the object of ListView in xml file
outside onCreate and newListview is the innerclass for the activity
class newListview extends BaseAdapter{
private Context context;
public PrayerList(Context context) {
this.context=context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;// here return size of listview
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View view, ViewGroup viewgroup) {
ViewHolder holder = null;
LayoutInflater inflater = LayoutInflater.from(context);
if (view == null)
{
view = inflater.inflate(R.layout.mydatalistview, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) view.findViewById(R.id.title);
holder.txtViewDescription = (TextView) view.findViewById(R.id.description);
holder.arrowImage=(ImageView)view.findViewById(R.id.arrow_imageview);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
//here you can use onclick of particular item of listview
}
}
class ViewHolder{
TextView txtViewTitle;
TextView txtViewDescription;
ImageView arrowImage;
}