public class AddCourse extends Activity implements OnClickListener {
private Spinner addCourse;
String addedcoursevalue;
TextView AddcourseButton;
StringBuffer sb;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addcourse);
addCourse = (Spinner) findViewById(R.id.spinner1);
AddcourseButton = (TextView) findViewById(R.id.AddcourseButton);
AddcourseButton.setOnClickListener(this);
final String[] coursearray = getResources().getStringArray(
R.array.Course_arrays);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.spinner_item, coursearray);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
addCourse.setAdapter(dataAdapter);
addCourse.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
addedcoursevalue = addCourse.getSelectedItem().toString();
sb = new StringBuffer();
sb.append(addedcoursevalue);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(AddCourse.this, sb, 1000).show();
}
}
This is my code i am able to display value in spinner i want what ever i selected that should store in buffer that i want to Print on Button click in toast please check where am doing mistake please help
i want what ever i selected that should store in buffer that i want to
Print on Button click
No need to store Spinner selected value in Buffer inside onItemSelected method. you can directly access selected value in onClick method by calling addCourse.getSelectedItem() :
sb = new StringBuffer();
addCourse.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
addedcoursevalue = addCourse.getSelectedItem().toString();
sb.append(addedcoursevalue);
}
Related
I am trying to make a listView search for Android.I am using SimpleCursorAdapter on onTextChanged
i have found a error adapter cannot be resolved or is not a field.how can resolve this error .I am using SimpleCursorAdapter .I am not able to use adapter.getFilter().filter(cs);
ListView lv;
Cursor cursur1;
EditText search ;
int textlength=0;
ArrayAdapter<String> adapter1;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
search = (EditText) findViewById(R.id.inputSearch);
final String [] from={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone._ID};
int [] m={android.R.id.text1,android.R.id.text2};
lv=getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
cursur1=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
startManagingCursor(cursur1);
#SuppressWarnings("deprecation")
SimpleCursorAdapter adapter=new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursur1, from,m);
setListAdapter(adapter);
search.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
MainActivity.this.adapter.getFilters().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
#Override
public long getSelectedItemId() {
// TODO Auto-generated method stub
return super.getSelectedItemId();
}
#Override
public int getSelectedItemPosition() {
// TODO Auto-generated method stub
return super.getSelectedItemPosition();
}
}
Try this.. inside add Text change listener
YourAdapterOrWhateverAdapter adapter = (YourAdapterOrWhateverAdapter) listView.getAdapter();
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged()
you can use AutoCompleteTextView, e.g -
private static final String[] DATE_RANGE = new String[] { "1 Day",
"2 Days", "3 Days", "6 Months", "1 Year", "Blank" };
AutoCompleteTextView dateRangeTV = (AutoCompleteTextView)findViewById(R.id.tv_date_range);
dateRangeTV.setThreshold(0);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.default_date_range_list, DATE_RANGE);
dateRangeTV.setAdapter(adapter);
In Touch event, you could see the dropdownlist :
dateRangeTV.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
dateRangeTV.showDropDown();
return false;
}
});
Type any text, It'll show your matching text from the list.
For more see the tutorial of the AutoCompleteTextView
I have a spinner in my code and I want to get position of element in the array when selected. Here is my code which is running perfectly. In selection I am storing the string value of element but I also want the position count of element
public class MainActivity extends Activity implements OnItemSelectedListener {
final Context context = this;
private Button button;
private String selection;
private String[] states = new String[]{
"Gujrat","Jammu and Kashmir","Kerala","Karnataka","Lakshadweep","Maharashtra","Manipur","Mizoram",
"Nagaland","New Delhi","Rajasthan","Tami Nadu","West Bengal"
};
ArrayList<String> categoryList = new ArrayList<String>();
private static final String TAG = "MyActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int currentMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
//make it fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//fix portrait orientation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.menu);
Button dialogButton = (Button) dialog.findViewById(R.id.btncross);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Spinner spin = (Spinner)dialog.findViewById(R.id.spinState);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, states);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter_state);
spin.setOnItemSelectedListener(MainActivity.this);
}
});
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView tv = (TextView)arg1;
selection = tv.getText().toString();
Log.v(TAG, "index=" + selection);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
In the code:
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
TextView tv = (TextView)arg1;
selection = tv.getText().toString();
Log.v(TAG, "index=" + selection);
}
arg2 is the position of spinner
int arg2 of `onItemSelected()` is the position of selected items.
So you can use it like
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selection = states[arg2];
Log.v(TAG, "index=" + arg2);
Log.v(TAG, "selction=" + selection);
}
spin.getSelectedItemPosition();
Here in your setOnItemSelectedListener(new OnItemSelectedListener()
public abstract void onItemSelected (AdapterView<?> parent, View view, int position, long id)
Parameters
parent The AdapterView where the selection happened
view The view within the AdapterView that was clicked
position The position of the view in the adapter
id The row id of the item that is selected
int arg2 to get Selected Item Id
Update only this method,
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Log.v(TAG, "index=" + spin.getItemIdAtPosition(int2));
}
How to get value in second spinner and third spinner after selected first spinner and calculate [to create a simple converter]
here the code, there is no error in my code but this coding not run perfectly, please help me guys..
public class MainActivity extends Activity{
private Spinner firstSpinner, secondSpinner, thirdSpinner;
private Button convert;
EditText input, output;
Double awal, numtemp;
String num, hasil;
ArrayList<String> ChooseType = new ArrayList<String>();
ArrayList<String> ChooseSuhu = new ArrayList<String>();
ArrayList<String> ChooseBerat = new ArrayList<String>();
ArrayList<String> ChoosePanjang = new ArrayList<String>();
ArrayList<String> ChooseData = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstSpinner = (Spinner) findViewById(R.id.ChooseType);
secondSpinner = (Spinner) findViewById(R.id.ChooseType1);
input = (EditText) findViewById(R.id.editText);
thirdSpinner = (Spinner) findViewById(R.id.ChooseType2);
output = (EditText) findViewById(R.id.editText1);
convert = (Button) findViewById(R.id.konBut);
fillarray();
firstSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line, ChooseType));
firstSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,long arg3) {
// TODO Auto-generated method stub
if (pos == 1) {
secondSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChooseSuhu));
thirdSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChooseSuhu));
}
/*else if(pos == 2){
secondSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChooseBerat));
thirdSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChooseBerat));
}
else if(pos == 3){
secondSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChoosePanjang));
thirdSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChoosePanjang));
}
else if(pos == 4){
secondSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChooseData));
thirdSpinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, ChooseData));
}*/
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
convert.setOnClickListener(new convert());
}
public void onClickListener(){
secondSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
if(arg2 == 0){
num = input.getText().toString();
awal = Double.parseDouble(num);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
thirdSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
numtemp = awal;
hasil = numtemp.toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
private class convert implements OnClickListener {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
output.setText(hasil);
}
}
private void fillarray() {
// TODO Auto-generated method stub
ChooseType.add("Choose");
ChooseType.add("Suhu");
ChooseType.add("Berat");
ChooseType.add("Panjang");
ChooseType.add("Data");
ChooseSuhu.add("celcius");
ChooseSuhu.add("reamur");
ChooseSuhu.add("fahrenheit");
ChooseSuhu.add("kelvin");
ChooseBerat.add("pon");
ChooseBerat.add("ton");
ChooseBerat.add("miligram");
ChooseBerat.add("gram");
ChooseBerat.add("kilogram");
ChooseBerat.add("oz");
ChoosePanjang.add("centimeter");
ChoosePanjang.add("feet");
ChoosePanjang.add("inche");
ChoosePanjang.add("kilometer");
ChoosePanjang.add("meter");
ChoosePanjang.add("mile");
ChoosePanjang.add("centimeter");
ChooseData.add("bit");
ChooseData.add("byte");
ChooseData.add("kilobit");
ChooseData.add("kilobyte");
ChooseData.add("megabit");
ChooseData.add("megabyte");
ChooseData.add("gigabit");
ChooseData.add("gigabyte");
ChooseData.add("terabit");
ChooseData.add("terabyte");
}
}
Try this
OtherSpinner.getSelectedItem().toString();
I am new to Android programming and I have a ListView when filtered always return me the first item in the list, so how do I fix this?
For instance, my list contains A.A, A.B, A.C, B.C, B.D.
When I want to search the list for things starting with B, I will get B.C, B.D but when I click B.C, it returns me A.A and B.D, it returns me A.B
public class PatientList extends Activity{
PatientDB patientDB;
Context myContext;
ListView lv_patients;
EditText et_patients;
ArrayAdapter<String> adapter;
ArrayList<HashMap<String, String>> patientList;
static String value;
String[] patientarray = new String[]{"initials"};
ArrayList<String> patientArrayList = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_patient_list);
myContext = this;
patientDB = new PatientDB(myContext);
patientDB.open();
Cursor patientCursor;
patientCursor = patientDB.retrieveAllEntriesCursor();
if(patientCursor!=null && patientCursor.getCount()>0)
{
String patientInitials;
patientCursor.moveToFirst();
do
{
patientCursor.getString(patientDB.COLUMN_KEY_ID); // + " " +
patientInitials = patientCursor.getString(patientDB.COLUMN_INITIALS_ID);
Log.i("FromCursor", patientInitials);
patientArrayList.add(patientInitials);
} while (patientCursor.moveToNext());
}
lv_patients = (ListView) findViewById(R.id.lv_patients);
et_patients = (EditText) findViewById(R.id.et_patients);
lv_patients.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
String a = patientArrayList.get(position);
Toast.makeText(PatientList.this, a + " selected", Toast.LENGTH_SHORT).show();
Intent returnIntent = new Intent(PatientList.this, PatientInfo.class);
returnIntent.putExtra("value", a);
setResult(RESULT_OK, returnIntent);
finish();
}
});
//Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_patients, R.id.patient_name, patientArrayList);
lv_patients.setAdapter(adapter);
//Enabling search filter
et_patients.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
PatientList.this.adapter.getFilter().filter(cs);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
}
Because you filter your adapter to have B.C in position 0 and B.D in position 1. But in your implementation of your onClickListener, you use the listview's position to get directly from the source ArrayList.
String a = patientArrayList.get(position);
Instead to get it from the filtered adapter:
String a = parent.getAdapter().getItem(position);
or
String a = parent.getItemAtPosition(position);
I have used a List and an Arrayadapter to put strings in a Listview. Now I want to remove the selected Item. I have tried the following code, but it's not working well. How can I fix this?
Here's my code:
TextView t1;
String[] temp;
mylist = (ListView) findViewById(R.id.list);
final List<String> wordList = Arrays.asList(temp);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
wordList);
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int index = mylist.getSelectedItemPosition();
int count=mylist.getCount();
for(int i=0;i<=count;i++) {
if (index >= 0) {
t1.setText(wordList.remove(index));
}
adapter.notifyDataSetChanged();
}
}
});
Here's more source code:
public class Edit extends Activity {
SharedPreferences sharedpref;
ListView mylist;
String[] temp;
String name,
ArrayAdapter<String> adapter;
Button save, delete, cancel;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
mylist = (ListView) findViewById(R.id.list);
cancel = (Button) findViewById(R.id.cancel1);
save = (Button) findViewById(R.id.save1);
delete = (Button) findViewById(R.id.delete1);
t1=(TextView)findViewById(R.id.textView1);
sharedpref = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
name = sharedpref.getString("Visible_selected", "");
String delimiter = "\n";
temp = name.split(delimiter);
mylist.setItemsCanFocus(true);
mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
final List<String> wordList = Arrays.asList(temp);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, wordList);
mylist.setAdapter(adapter);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mylist.setOnItemClickListener(new OnItemClickListener() {
private String getSelectedItemOfList;
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
getSelectedItemOfList = sortedList.get(arg2).getStr_movieParam();
}
});
cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
Here's my updates code:
enter code here
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
adapter.remove(getSelectedItemOfList);
adapter.notifyDataSetChanged();
}
});
mylist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
SparseBooleanArray checkedPositions =mylist.getCheckedItemPositions();
if(checkedPositions.get(arg2)==true){
getSelectedItemOfList = (String) mylist.getItemAtPosition(arg2);
t1.append(getSelectedItemOfList.toString()+"\n");}
}
});
Try to implement the onitemclickListener and get the item id and delete the item clicked in the arrayadapter and next call adapter.notifyDataSetChanged();
Try this...
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3) {
String value = (String) adapter.getItemAtPosition(position);
adapter.remove(value);
adapter.notifyDataSetChanged();
}
});
I think you should use OnItemClickListener() for the listview so that when you select/click any list item you will get name of selected item then you can perform deletion operation.
Try this.
mylist.OnItemClickListener(listenerOflistView);
private OnItemClickListener listenerOflistView = new OnItemClickListener() {
private String getSelectedItemOfList;
public void onItemClick(AdapterView<?> view, View view1, int pos,
long arg3) {
// TODO Auto-generated method stub
getSelectedItemOfList = mylist.get(pos).toString(); // here you will get selected item name.
}
}
Hope this will help you.
see this code which help you to understand how to remove items in listview.
private ArrayList<String> students = new ArrayList<>();
private ArrayAdapter<String> arrayAdapter;
private ListView myListView;
private int getSelectedIndex = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myListView = (ListView) findViewById(R.id.myListView);
students.add("rahim");
students.add("karim");
students.add("sumon");
students.add("rakib");
students.add("porag");
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, students);
myListView.setAdapter(arrayAdapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "hello " + students.get(position), Toast.LENGTH_LONG).show();
MainActivity.this.getSelectedIndex = position;
for (int i = 0; i < myListView.getChildCount(); i++) {
if(position == i ){
myListView.getChildAt(i).setBackgroundColor(Color.BLUE);
}else{
myListView.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
}
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);//Menu Resource, Menu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:
Toast.makeText(getApplicationContext(), "Item 1 Selected", Toast.LENGTH_LONG).show();
return true;
case R.id.delete:
if(this.getSelectedIndex!=-1){
students.remove(this.getSelectedIndex);
this.getSelectedIndex = -1;
arrayAdapter.notifyDataSetChanged();
myListView.setAdapter(arrayAdapter);
}
return true;
default:
return false;
}
}
I would say the best way to do this (the really best way is a custom adapter) is to keep a copy of your arraylist of items in your class. Then when delete is called remofve the item form your copy of the arraylist and re-init the listview.
First thing i want to tell you that code you have written is wrong...
May be you want the context menu on listview..
if you are getting the position of selected item then you can have
adapter.removeItem(adapter.getItem(position));
adapter.notifyDatasetChanged()
may this help you
replace
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
mylist.setOnItemClickListener(new OnItemClickListener() {
private String getSelectedItemOfList;
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
getSelectedItemOfList = sortedList.get(arg2).getStr_movieParam();
}
});
with
mylist.setOnItemClickListener(new OnItemClickListener() {
private String getSelectedItemOfList;
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
getSelectedItemOfList = sortedList.get(arg2).getStr_movieParam();
}
});
and
delete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
adapter.remove(getSelectedItemOfList );
adapter.notifydatasetChanged
}
});
check this one
Try working with this code in the http://appfulcrum.com/2010/09/12/listview-example-3-simple-multiple-selection-checkboxes/
public class ListTest extends Activity {
String selectedItem;
ArrayAdapter<String> adapter;
ArrayList<String> newList = new ArrayList<String>();
ListView l1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_test);
l1 = (ListView) findViewById(R.id.lsvSign);
newList.add("Android");
newList.add("iOS");
newList.add("Mac");
newList.add("Windows");
newList.add("Linux");;
Collections.sort(newList);
adapter = new new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,newList);
l1.setAdapter(adapter);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
selectedItem = l1.getItemAtPosition(position).toString();
RemoveItem(selectedItem);
}
});
protected void RemoveItem(String item) {
newList.remove(item);
adp2.notifyDataSetChanged();
}
}
}
Try working code: listAdapter.remove(listAdapter.getItem(position));
mainListView.setAdapter( listAdapter );