I'm trying to delete the items in my ListView by their ID. Currently they are being deleted by the first position on the List. I can have many items in the list.(example) When I try to delete the sixth item from the list the first item is deleted and the sixth remains.
How do I get the List to delete by ID like in this case m1aa or stringl1. My database deletes stringl1 perfectly but it remains in the list unless it's in the top position.
int pos;
long id;
final ListView lv = getListView();
I forgot to add this to my question
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
adapter=new ArrayAdapter<String>(this,
R.layout.singlelistview,listItems);
setListAdapter(adapter);
Sorry
Button button5 = (Button) dialogView.findViewById(R.id.button5);
button5.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView textviewlay1 =(TextView)findViewById(R.id.m1aa);
String stringl1 = textviewlay1.getText().toString();
Double doubl1 = Double.parseDouble(stringl1);
final String str=lv.getItemAtPosition(pos).toString();
Log.i("ListView", "onLongListItemClick string=" + str);
{listItems.remove(pos);
adapter.notifyDataSetChanged();}
controller1.deletename(stringl1);
}});
It seems that you do not set variable pos.It is an int variablae,so it is 0 when you call:
listItems.remove(pos);
And as result first item(that it's index is equal to 0) will be deleted.
This is happening because getView() called for all the items then you click on button to delete row at that time position is set to last row thats why last item is deleted every time. You have to set position as tag to button and on the time of deletion you can get index by this tag. Thanks
int pos;
long id;
final ListView lv = getListView();
Button button5 = (Button) dialogView.findViewById(R.id.button5);
button5.setTag(pos);
button5.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView textviewlay1 =(TextView)findViewById(R.id.m1aa);
String stringl1 = textviewlay1.getText().toString();
Double doubl1 = Double.parseDouble(stringl1);
final String str=lv.getItemAtPosition((Button)v.getTag()).toString();
Log.i("ListView", "onLongListItemClick string=" + str);
{listItems.remove(String.vauleOf((Button)v.getTag()));
adapter.notifyDataSetChanged();}
controller1.deletename(stringl1);
}});
It can also be done through onItemClickListener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
list.remove(item);
adapter.notifyDataSetChanged();
}
});
Related
> I want to change the selected item's checkbox color of listview so that when user selects the item its color gets changed this is my code :
final ListView listView = (ListView)findViewById(R.id.lvcancelorder);
//this is my listview
ArrayAdapter<String> adapter =
new ArrayAdapter<String(this,android.R.layout.simple_list_item_single_choice, countries); //this is the adapter
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) //this is the item click event
{
selectedFromList = (listView.getItemAtPosition(position)).toString();
}}); //this is the selected item from the listview
How can I do this ? Please suggest something.
I know to do this thing List view custom adapter but have no idea to do it.
It is recommended to create your own custom adapter extends from BaseAdapter or ArrayAdapter and on selection of item simply change background color of root layout view.
use following link to get know about how to create a listview with checkboxes.
see here
You can try this:
//Your button to get selected list
getChoice = (Button)findViewById(R.id.getchoice);
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, android.R.layout.simple_list_item_multiple_choice, countries);
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myList.setAdapter(adapter);
getChoice.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
String selected = "";
int cntChoice = myList.getCount();
SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++){
if(sparseBooleanArray.get(i)) {
selected += myList.getItemAtPosition(i).toString() + "\n";
}
}
Toast.makeText(MainActivity.this, selected, Toast.LENGTH_LONG).show();
}});
this is my code; basically, it is a listview with multiple choice that add a number to a score (textview) when its clicked. My problem is that i would like the number be added to the score when the item is selected in the listview, and removed when the item is deselected. With my code each time that you click an item, the number is added to the score.
public class MainActivity extends Activity {
ListView myList;
TextView tv1;
String[] listContent = {
"Add 2 to total",
"Add 3 to total",
"Add 1 to total",
"Add 5 to total",
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList = (ListView) findViewById(R.id.listView1);
tv1 = (TextView) findViewById(R.id.textView);
final int[] counter = {0};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, listContent);
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener() {
private String[] listview_array2 = {"2", "3", "1", "5"};
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (myList.isItemChecked(position)) {
String name = this.listview_array2[position];
int counter2 = Integer.parseInt(name);
counter[0] = counter[0] + counter2;
tv1.setText("Total " + counter[0]);
} else {
}
}
});
}
}
I have thought that maybe changing to setOnItemSelected instead of ItemClicked, but I am not sure.
Hope I have explain myself properly and you can help me.
Thank you in advanced.
Try with OnItemClickListener with a Boolean for selection/deselection.
Hope this will work
You can do that through your code.
Maintain a flag in the list of your values like List list;
Values is a model containing the number and a flag.
If a item is clicked, set the flag true and do your calculations.
If already clicked item is clicked again, you just need to check the flag.
if(list.getposition(position).isItemClickd){
// Decrease that value from total
// list.getposition(position).isItemClickd = false;
} else{
// list.getposition(position).isItemClickd = true;
// total = total+prevValue;
}
I finally made it in a easy way!!!
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (myList.isItemChecked(position)) {
String name = this.listview_array2[position];
int counter2 = Integer.parseInt(name);
counter[0] = counter[0] + counter2;
tv1.setText("Total " + counter[0]);
I add in "else" the same action than in "if", but subtracting the number
I am trying to make app with one text view, one button and one spinner. I make button work showing random values from string array, but i have a lot different string arrays(list1, list2 etc). Now my question is when change spinner position, how to get button get another string array(from spinner) and show it to textview.
How to pass value from spinner when something is selected in spinner to button.
Any help is appreciated.
Here is my code:
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
Button btn;
public String[] myString,myString1;
public static final Random rgenerator = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//textview
final TextView tv = (TextView) findViewById(R.id.textureView);
final Resources res = getResources();
//string
myString = res.getStringArray(R.array.World_War_I);
myString1 = res.getStringArray(R.array.World_War_II);
//button
btn = (Button) findViewById(R.id.buttonxx);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
//i am missing code here, spinner position, and pass spinner position to if statement.
if (==0){
myString = res.getStringArray(R.array.list1);
String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
}
if (==1){
myString1 = res.getStringArray(R.array.list2);
String q1 = myString1[rgenerator.nextInt(myString.length)];
tv.setText(q1);
}
}
});
//drop list
Spinner spinner = (Spinner) findViewById(R.id.spinnerrrr);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.kategorije, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
// when some cat selected
public void onItemSelected(AdapterView<?> parent, View view,
final int pos, long id) {
// An item was selected. You can retrieve the selected item using
parent.getItemAtPosition(pos);
parent.setSelection(0);
parent.getSelectedItemPosition();
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
Use this
// when some cat selected
public void onItemSelected(AdapterView<?> parent, View view,
final int pos, long id) {
// An item was selected. You can retrieve the selected item using
btn.setTag(pos+""); // Passing as string
parent.getItemAtPosition(pos);
parent.setSelection(0);
parent.getSelectedItemPosition();
}
and then
btn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
//i am missing code here, spinner position, and pass spinner position to if statement.
int index = Integer.parseInt(btn.getTag().toString());
if (index==0){
myString = res.getStringArray(R.array.list1);
String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
}
if (index==1){
myString1 = res.getStringArray(R.array.list2);
String q1 = myString1[rgenerator.nextInt(myString.length)];
tv.setText(q1);
}
}
});
So, this might be a simple question, or I may be doing things totally off here, but here's what I have:
public class SetPrefsActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.radiolist);
ArrayList<Hall> listItems = new ArrayList<Hall>();
ArrayAdapter<Hall> ar = new ArrayAdapter<Hall>(this, android.R.layout.simple_list_item_single_choice, listItems);
setListAdapter(ar);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
boolean somethingChecked = false;
int lastChecked;
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
if(somethingChecked){
ListView lv = (ListView) arg0;
TextView tv = (TextView) lv.getChildAt(lastChecked);
CheckedTextView cv = (CheckedTextView) tv;
cv.setChecked(false);
}
ListView lv = (ListView) arg0;
TextView tv = (TextView) lv.getChildAt(arg2);
CheckedTextView cv = (CheckedTextView) tv;
if(!cv.isChecked())
cv.setChecked(true);
lastChecked = arg2;
somethingChecked=true;
}
});
new LoadListTask().execute(); //This loads Items into the List
Button b = (Button) findViewById(R.id.savePrefBtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//IF SOMETHING IS SELECTED,
//THEN UPDATE SHARED PREFERENCES
Intent i = new Intent(SetPrefsActivity.this, SomeOtherActivity.class);
startActivity(i);
}
}
});
}
//other stuff to fill the arrayAdapter
}
What I want to do, is:
When someone clicks the button, it gets information from the listview and updates a shared preference according to the radio option that's selected.
What I'm having trouble with is getting the index of the currently selected item. What is the best way to retrieve that information? Am I implementing the single choice list completely wrong?
Thank You!
arg2 is the position in your list.
It looks like you are doing some extra (unnecessary) processing. arg1 is your row view, and since the android.R.layout.simple_list_item_single_choice layout contains only a CheckedTextView, you can use that directly without having to look for it.
CheckedTextView cv = (CheckedTextView) arg1;
I have this code i am used to pass an array list to another page and show it as a listview. When the list shows up, i want to be able to check an item and remove it at "button click" which will modify the array.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oppout);
final ListView lv2 = (ListView) findViewById (R.id.custom_list_view);
lv2.setClickable(true);
lv2.setAdapter(new ArrayAdapter<String>(Oppout.this,
android.R.layout.simple_list_item_checked,
Entername.playerList));
lv2.setOnItemClickListener (
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView adapterView,
View view,int arg2, long arg3) {
int selectedPosition = adapterView.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "mu"+ selectedPosition,
Toast.LENGTH_SHORT).show();
}
});
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// on click call the adapterview and delete string at selected position.
// This is my problem, am not getting how to call the adapter and deleted
// the selected item/position
int selectedPosition = adapterView.getSelectedItemPosition();
adapterView.remove(player.SelectedPosition);
Intent myIntent = new Intent (view.getContext(), Callacab.class);
startActivityForResult(myIntent, 0);
}
});
}}
contacts.remove(index); //the arraylist you gave it to your adapter
arrayadapter.remove(index); // this is your adapter that you give it to the listview
arrayadapter.notifyDataSetChanged();
//you can delete from your arraylist or your adapter and then notifyDataSetChanged(); to make the effect happen in your listview....it better to delete from arraylist and its indexs will be the arg2 in your listview item listener