delete the checked item in listview

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

delete the checked item in listview

Postby gsk » Tue Nov 11, 2008 3:23 pm

Hi ....
Iam the new to android... I am trying to developing a sample project like todolist application.

i want to display my listview with a check box and i write the code like below.
I created a new activity which extnds new ListActivity and also i write the code for check boxes in same listactivity by taking an inner class (which extnds arrayadapter).

and then I got the exact view like what i wanted .... like below


i added the menu buttons like insert and delete...

My Problem is:
when i press insert ibutton inthe menu i can able to add note successfully to the listview( ie. visibled as checkbox + textview)
but....when i pressed the delete button it should be delete checked items in the list
but ... i couldn't get the item which i checked in that listview.

package mypack.login;

import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.AdapterView;
import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;

public class MyList2 extends ListActivity implements AdapterView.OnItemClickListener{

private static final int MENU_ITEM_INSERT = Menu.FIRST;

private static final int MENU_ITEM_DELETE = Menu.FIRST+1;


Task tl=new Task();

private CheckBoxAdapter mCheckBoxAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.todolist);

mCheckBoxAdapter = new CheckBoxAdapter(this,R.layout.list_item_checkbox, tl.arraylistitems);

setListAdapter(mCheckBoxAdapter);

final ListView listView = getListView();

listView.setItemsCanFocus(false);
listView.setTextFilterEnabled(true);


}

public void onItemClick(AdapterView parent, View view, int position, long id)
{

mCheckBoxAdapter.toggle(position);

}

private static class CheckBoxAdapter extends ArrayAdapter<String>implements CompoundButton.OnCheckedChangeListener
{

private SparseBooleanArray mCheckStates;
// public SparseBooleanArray checked;

public CheckBoxAdapter(Context context, int resource, List objects)
{
super(context, resource, objects);
mCheckStates = new SparseBooleanArray(objects.size());

}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final CheckBox view = (CheckBox) super.getView(position, convertView, parent);
view.setTag(position);
view.setChecked(mCheckStates.get(position, false));
view.setOnCheckedChangeListener(this);
return view;
}

public boolean isChecked(int position) {

return mCheckStates.get(position, false);

}

public void setChecked(int position, boolean isChecked) {

mCheckStates.put(position, isChecked);

notifyDataSetChanged();
}

public void toggle(int position) {

setChecked(position, !isChecked(position));

}

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

mCheckStates.put((Integer) buttonView.getTag(), isChecked);


}


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

// This is our one standard application action -- inserting a
// new note into the list.
menu.add(0, MENU_ITEM_INSERT, 0, R.string.menu_insert);

final boolean haveItems=getListAdapter().getCount() > 0;

if(haveItems){
menu.add(0, MENU_ITEM_DELETE , 0, R.string.menu_delete);
}


return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ITEM_INSERT:
// Launch activity to insert a new item
startActivity(new Intent(this, Task.class));
return true;

case MENU_ITEM_DELETE:

?********************************************************??????????????????
?????????????????????????????



return true;
}

return super.onOptionsItemSelected(item);
}



}


please help me ....(try to post some code if possible)

thanks in advance
gsk
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Nov 05, 2008 11:12 am

Top

Postby MrSnowflake » Tue Nov 11, 2008 3:31 pm

This is NOT a tutorial!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests