in a project i'm doing for school, I need to have a list of items (each item is added by the user pressing a button and such).
Each item will hold several objects/views, one of them being a button.
The button is supposed to delete the entire item (list row) created.
the code I wrote is useless, cause i made too many changes after searching google for 4 hours.
I know how to create a listView using my custom arrayadapter, how to add items to the list and how to customize the list's items (custom view). I can not figure out how to make that button delete the item it is in.
If you want to remove the item, following line of code is needed
arrayAdapter.remove(objectListAdapterMadeFrom.get(indexOfItemToBeDeleted));
Also call
arrayAdapter.notifyDataSetChanged()
if somewhere you are using setNotifyOnChange(false);
first of all, thanks for your answers.
what i eventually did was this:
added "int pos;" in main activity.
added a method in main activity:
private OnItemClickListener listener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
pos = position;
}
};
then, inside the button i added this line:
alarmList.remove(pos);
that fixed it.
i still don't know how efficient is my solution, would love to hear your thoughts.
btw, although i continued my project and added some stuff, if you still wish for the code, i'll post it all here.
Related
I have been working on a certain project which involves the use of web service. I have a list of products that will display in UI as Image,Button to Like,Add To List and some other. I have implemented these items in a Custom ListView and added the products in the Adapter class as always. If I select a Like button for a certain image then the button should change to Unlike, the problem that I am facing here is that the button changes for all the items i.e. all the buttons in the list view gets changed to Like which is idiotic.
Question :
How do I pinpoint a certain list item to get changed (or) How do I change a single button in a list ?
I am short of logic to be used here. Any help will be much appreciated.
Without code it is difficult to help specifically, but have you tried using something like,
listview.getChildAt(position);
to reference a particular list item.
Depending on your code, you can modify this:
yourlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
//on click of any item.
Toast.makeText(getApplicationContext(), "Your position"+position, Toast.LENGTH_LONG).show();
//This is just for example to obtain the position of the listview item.
TextView example= (TextView)findViewById(R.id.sometext);
example.setText(String.valueOf(position));
}
});
Hope this gives you some information..:)
I'm building a simple App with a ListView in it. In the Java code, I'm trying to figure out which item(s) is currently selected. From the ListView documentation at http://developer.android.com/reference/android/widget/ListView.html I see there's a method in the ListView class called
public void setSelection (int position)
I looked for a similarly named method named getSelection(), but there is none.
So, first, how do I determine which item in my ListView is currently selected? I already have the object for the ListView, so I just need the method to tells me which item the user clicked on.
ListView myLV = (ListView) findViewByID(R.id.myListView);
Also, can I define a "callback" function which is called whenever an item is selected?
Finally, how can I use the documentation to figure this out? This is a very basic question which I should be able to figure out on my own. I did what seems to be the right thing: I looked at the Official Developer pages for the ListView class, but it didn't provide the answer. How can I answer these (basic) questions for myself?
Try this
mylv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View v, int position, long lng) {
String selectedFromList =(String) (mylv.getItemAtPosition(position));
}
});
I looked for a similarly named method named getSelection(), but there is none. So, first, how do I determine which item in my ListView is currently selected?
The super class AdapterView has getSelectedItem(), along with getSelectedItemId() and others. AbsListView has getSelectedView() too. But these only tend to work when you have used setChoiceMode()
The OnItemClickListener lets you know which it is clicked as it is clicked.
Finally, how can I use the documentation to figure this out?
You were right to start in ListView's documentation, but go to the "Inherited Methods" section and check the super classes as well.
You can get the position by using onitem click listener.by using on item click listener you can get position of selected item in the list.And you can put this position in Toast
I am working on a simple app in which a bar tender can do the beer pay off automatically. The articles (beer, wine, whisky etc) are listed in a database and I use a SimpleCursorAdapter to map all the articles from the database to a ListView. Works perfectly.
So now it displays a list of articles, prices and a textfield where the bartender can input the quantity of the order (ex 10 beer).
Problem: I don't know how I can get those quantities back from the ListView. Does anyone know how to do this?
Thanks in advance
If you want to use the OnItemClickListener of your ListView, you can use the View view Parameter to access Sub-Items of the List View Item. You could replace the TextView in the following example with your EditText. Replace android.R.id.text1 with your R.id.x.
yourListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView testAccess = (TextView)(view.findViewById(android.R.id.text1));
Toast.makeText(getApplicationContext(), testAccess.getText()
, Toast.LENGTH_LONG).show();
//do something
}
});
One potential way to do it is to set a button click listener inside of your Adapter getView() method, upon click that button can pull the value out of the EditText. Since you are inside the getView() method you should still have a reference to the EditText that you need.
I have a main activity that uses a FrameLayout with the screen split into left navigation and right content panels. The left panel is used for navigation to load different layouts into the right panel (called contentLayout) which can have multiple layouts glued together. I have a ListView in the left navigation panel with onItemClickListeners. In their separate onItemClick methods, I use contentLayout.removeAllViews() and then inflate their layouts like so:
private final OnItemClickListener redListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
contentLayout.removeAllViews();
if (myFirstClassLayout == null) {
myFirstClassLayout = FirstClassLayout.getLayout(
layoutInflater,
MyActivity.this,
resources,
new ArrayList<String>(),
new ArrayList<Desc>()
);
}
CommonClass.updateListeners();
if (FirstClassLayout != null) contentLayout.addView(myFirstClassLayout);
}
};
private final OnItemClickListener blueListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
contentLayout.removeAllViews();
if (mySecondClassLayout== null) {
mySecondClassLayout = SecondClassLayout.getLayout(
layoutInflater,
MyActivity.this,
resources,
new ArrayList<String>(),
textList,
new ArrayList<Desc>(),
hash
);
}
CommonClass.updateListeners();
if (SecondClassLayout != null) contentLayout.addView(mySecondClassLayout);
}
};
In both the FirstClassLayout and SecondClassLayout, I inflate a ThirdClassLayout which contains a dynamic ListView (let's call it commonListView) that makes use of an ArrayAdapter<String>. The ListView is a listChoiceIndicatorSingle style view, meaning that it shows a radio-button-like icon to the right of each list item.
When the app starts up, the right content panel is empty. If I trigger the redListener or the blueListener, it will display the commonListView in the right content panel with none of the list items selected. If I trigger red first, then trigger blue, then make a selection in the commonListView, the change is reflected back to the FirstClassLayout which is great! However, when I continue by selecting something else in the commonListView from that FirstClassLayout, and then trigger the blueListener to go to the SecondClassLayout, the commonListView in the SecondClassLayout does not reflect the selection I just made. Instead, it is stuck on item I selected when I was last on the SecondClassLayout.
This happens if I perform the testing in the reverse order, so depending on which listener I trigger last, that last listener and layout will have proper UI synching control over both layouts. The one I trigger first seems to be able to only control itself. I need for both listeners to be able to display the exact same ListView selected item on both Layouts at any given time.
The synching of the commonListView from last layout to first layout is achieved through CommonClass.updateListeners() which does:
adapter.clear();
for (String title: statusSet) adapter.add(title);
adapter.notifyDataSetChanged();
commonListView.setItemChecked(positionOfItemChecked, true);
Log.i(TAG, "position checked = " + positionOfItemChecked);
adapter.notifyDataSetChanged();
Since this works from last layout triggered to first, I figure this must be related to a UI refresh problem, but I've searched everywhere for a good solution and tried all suggestions. The Log.i statement prints out correctly in the LogCat for first or last layout triggered, so I know it is hitting the code properly. I just don't understand why the first one is unable to control the last one. I've tried:
commonListView.invalidate();
((BaseAdapter) commonListView.getAdapter()).notifyDataSetChanged();
and AsyncTask and several others, but nothing seemed to work on refreshing the first layout so that it matches the second. Has anyone encountered such an issue? Does anyone know how to fix or get around this?
See if contentLayout.requestLayout() after adding the views fixes your problem.
I figured out what was causing the 2nd one to take hold over the 1st. I inherited a lot of this code and so I'm not familiar with all the parts. Several other pieces were involved. I finally found the portion of code where the original author used a custom AdapterWrapper that implements a custom Listener. The ThirdClassLayout was only creating a new ArrayAdapter and a new AdapterWrapper once by checking to see if the static ArrayAdapter was null. Removing this check allowed both adapter objects to be created new for both layouts. With their own Listeners created, both layouts now respond.
I'm now developing an application that uses a ListView with a
CheckedTextView on every item that managed by an ArrayAdapter to
support multiple chooses. The contents in my ListView are dynamic, that
means, can be changed during runtime. Now I try to use
ListView.getCheckedItemPositions() to get all checked items, Because I want
to save all the checked positions and auto-check them when user go back to
this page again. So I need to save checked results for every page.
For the first page everything works fine as expected. But when user goes to
another page and make some chooses, the result array that ListView returned
contains some positions that are never checked. I don't why ListView has
this strange behavior. Even for a page that in fact no checked happens but
ListView gives me a result that indicates there's one item has been checked.
could anyone who can teach me how to get the position of CheckedTextView
in its OnClickListener callback?
example code is appreciate.
Thanks in advance...
The listview recycles its views so when you go to a different page and then return to the previous page, the listview recalls the getView() function for its views. To make sure that the order of the checked views are not mixed up, create an arraylist that contains the check state of all the views before initializing the adapter. Then pass the arraylist as an argument for the adapter's constructor. There, in the getView() function, set the checked state of each checkable textview based on the arraylist. Then, return to the activity class and override the onItemClick() event. Using the view that is given to you when the function is called, do the following to get the checkable textview and set its checked state:
listView1.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View selectedView, int position , long id)
{
CheckedTextView tv = (CheckedTextView)selectedView.findViewById(R.id.textview);
if (tv.isChecked())
{
tv.setChecked(false);
checkStatesOfViews.get(position) = false;
}
else
{
tv.setChecked(true);
checkStatesOfViews.get(position) = true;
}
}
});