• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

How can i get checkbox position into listview to remove corresponding textview

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello sir, I have Arraylist into bean class@Line 1 Below. I have mainActivity class @ Line 2 Below where i set data to listview using myAdapter(CustomAdapter class). each row has one textview and checkbox. i want to do when checkbox is checked i want to obtain that checkbox position so i can remove corresponding textview so, i used below l1.remove(i) it' s work find but what happen i have to click on listview item to remove the item but want to remove iwhen checkbox is checked or clicked i want to remove corresponding item. thanks.


Line 1>>> public class Bean {




static ArrayList<String> getList(){

ArrayList<String> l1=new ArrayList<String>();

l1.add("Laptop");
l1.add("Desktop");
l1.add("Pendrive");

return l1;
}
}
//////////////////////////////////////////////



Line 2>> public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


final ArrayList<String> l1=Bean.getList();



final ListView l2=(ListView)findViewById(R.id.listView1);
Button b1=(Button)findViewById(R.id.button1);


final myAdapter1 adapter = new myAdapter1(this, l1);
l2.setAdapter(adapter);




b1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

SparseBooleanArray boolArray=l2.getCheckedItemPositions();

for(int i=0;i<l1.size();i++)
{
if(boolArray.valueAt(i)==true)
{
l1.remove(i);
}
}
adapter.notifyDataSetChanged();


}
});



}


}

3>> public class myAdapter1 extends BaseAdapter
{



ArrayList<String> listItem;

Activity activity;




public myAdapter1(Activity activity, ArrayList<String> l1) {
// TODO Auto-generated constructor stub
listItem=l1;
this.activity=activity;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return listItem.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

public class ViewHolder
{
TextView txtViewItem;
CheckBox chk;
}

@Override
public View getView(final int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub

final ViewHolder item;
LayoutInflater inflater = activity.getLayoutInflater();

if(view==null)
{
view = inflater.inflate(R.layout.activity_main, null);
item = new ViewHolder();

item.txtViewItem = (TextView) view.findViewById(R.id.textView1);
item.chk=(CheckBox)view.findViewById(R.id.checkBox1);
view.setTag(item);
}
else
{
item = (ViewHolder) view.getTag();
}

item.txtViewItem.setText(listItem.get(position));



return view;
}
}
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So here is a question for you. If you had a CheckBox that wasn't in a ListView how would you determine when it was checked? What does that API for CheckBox say?
 
viral thakar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check following code when i checked to checkbox only one or multiple but none of textview corresponding to checkbox is removed. how can i achieve to remove textview when corresponding checkbox is checked. thanks.

1>>


public class Bean {


boolean [] selected=new boolean[3];
static ArrayList<String> getList(){

ArrayList<String> l1=new ArrayList<String>();

l1.add("Laptop");
l1.add("Desktop");
l1.add("Pendrive");

return l1;
}
}

2>>>>


public class MainActivity extends Activity {

Bean be=new Bean();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


final ArrayList<String> l1=Bean.getList();



final ListView l2=(ListView)findViewById(R.id.listView1);
Button b1=(Button)findViewById(R.id.button1);


final myAdapter1 adapter = new myAdapter1(this, l1);
l2.setAdapter(adapter);




b1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//TODO Auto-generated method stub

//boolArray=l2.getCheckedItemPositions();

for(int i=0;i<l1.size();i++)
{
if(be.selected[i]==true)
{
l1.remove(i);
}
}
adapter.notifyDataSetChanged();


}
});



}

}


3>>>

class myAdapter1 extends BaseAdapter
{

Bean be=new Bean();

ArrayList<String> listItem;

Activity activity;




public myAdapter1(Activity activity, ArrayList<String> l1) {
//TODO Auto-generated constructor stub
listItem=l1;
this.activity=activity;
}

@Override
public int getCount() {
//TODO Auto-generated method stub
return listItem.size();
}

@Override
public Object getItem(int position) {
//TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
//TODO Auto-generated method stub
return 0;
}

public class ViewHolder
{
TextView txtViewItem;
CheckBox chk;
}

@Override
public View getView( final int position, View view, ViewGroup parent) {
//TODO Auto-generated method stub

final ViewHolder item;
LayoutInflater inflater = activity.getLayoutInflater();

if(view==null)
{
view = inflater.inflate(R.layout.addlist, null);
item = new ViewHolder();

item.txtViewItem = (TextView) view.findViewById(R.id.textView1);
item.chk=(CheckBox)view.findViewById(R.id.checkBox1);
view.setTag(item);
}
else
{
item = (ViewHolder) view.getTag();
}

item.txtViewItem.setText(listItem.get(position));

item.chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub

if(item.chk.isChecked())
{
if(be.selected[position]==true){

be.selected[position]=false;
}
}
else{
be.selected[position]=true;

}
}
});





return view;
}
}
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code of any length. You can edit your posts to add them.

Also, this seems to be a duplicate or continuation of this question you asked yesterday. Please keep discussions within the same thread.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

viral thakar wrote: check following code when i checked to checkbox only one or multiple but none of textview corresponding to checkbox is removed. how can i achieve to remove textview when corresponding checkbox is checked. thanks.


Like Ulf said, it is hard to read your code, I don't want to expend the effort to try to figure it out. So I asked last time if you knew how to check if a checkbox is checked. I assume that you show that in the code you posted. You also know how to remove items from the adapter, because you do that with the button. So did you try to remove the checked items from the adapter when you get the checkbox event?
 
viral thakar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello sir, i have also tried into my custom adapter class to remove item from list when it's checked but what happen when i checked multiple item it's give me exception that size is 2 index 2 arrayindex out of bound exception so, i can only remove single item from list and then referesh the adpater class using method notifydatachenage somthing like this. so, give me perfect guide line or correct solution for that. also in my previous post it's not working i.e it's not removing any item. what wrong it was? thanks.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

viral thakar wrote:so, give me perfect guide line or correct solution for that.


We are NotACodeMill, we don't work by giving you results, nor am I your employee to be bossed around. So no, I won't give you the correct solution but I will help you.

when i checked multiple item it's give me exception that size is 2 index 2 arrayindex out of bound exception so,


Because you are attempting to remove the object based on an index, but once an object is removed, the indexes change. I would suggest you remove the row based on its value. You may have to find the value in list to get its index then remove it using the found index.

Alternatively, each time you remove an item you could refresh the list so each checkbox gets reset with the proper row it works on.

A third solution would be, instead of removing the rows, you hide them. Then remove them after some period of time of inactivity, so the screen refreshes less often.

i can only remove single item from list and then referesh the adpater class using method notifydatachenage somthing like this. also in my previous post it's not working i.e it's not removing any item. what wrong it was?


Well it looks like you know what is wrong, you aren't notifying the adapter (and the view) of the change.
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic