Win a copy of The Business Blockchain this week in the Cloud forum!
    Bookmark Topic Watch Topic
  • New Topic

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

 
viral thakar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Report post to moderator
i have created listview where each row has one textview and checkbox. when checkbox is checked i want to remove corresponding textview i.e i want to remove like list.remove(position). Let, me know in depth. thanks.
 
Maneesh Godbole
Saloon Keeper
Posts: 11308
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
You can control the visibility of the TextView using View#setVisibility(int visibility) depending on the check box selection
 
viral thakar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hello sir, actually i want to remove from arraylist.remove (position) when corresponding to checkbox is clicked.
 
Maneesh Godbole
Saloon Keeper
Posts: 11308
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
You are not being clear on what you wish to achieve.
viral thakar wrote:I have created listview where each row has one textview and checkbox. when checkbox is checked i want to remove corresponding textview

So do you wish to remove the TextView or the whole row? Also, if the whole row is removed, you cannot bring it back by toggling the check box (because the check box too will be gone) Is this an acceptable scenario?

viral thakar wrote: i want to remove from arraylist.remove (position) when corresponding to checkbox is clicked.

What is this arraylist? Please TellTheDetails
 
viral thakar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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;
}
}



[/img]
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Report post to moderator
Closing this topic, as the discussion is being continued in another topic: http://www.coderanch.com/t/621310/Android/Mobile/checkbox-position-listview-remove-textview
 
    Bookmark Topic Watch Topic
  • New Topic