Extracting user input from ListView - android

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.

Related

Deleting an item from a listView

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.

Pinpoint List View Item in Android

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..:)

"Synchronizing" two spinners

I have two spinners that I want to be "tied" to one another in a mutually-exclusive sense: If you select an item in one, that item's text turns red and appears at the top, while the other one goes back to displaying the initial ("title") selection (if another item was previously selected), and its text turns white.
This is all done via the onItemSelected listeners:
sectionSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
issueSpin.setSelection(0);
((TextView) issueSpin.getChildAt(0)).setTextColor(Color.parseColor("#FFFFFF"));
((TextView) arg1).setTextColor(Color.parseColor("#E3170D"));
....
and vice versa for the "issue spinner". My problem is that, if I'm going from one spinner to the other and I select the top item, the onItemSelectedListener doesn't register because the item being selected is already selected.
I've been told that this is not possible. Or, rather, I've been told that it is impossible for an onItemSelected listener to fire on an item that is already selected. While I realize this is technically true, this problem seems relatively simple and I'm sure there must be a workaround to produce the desired effect.
I have a few questions regarding some that I'm pondering:
Is there any way to set all items in a spinner as unselected, while still displaying one of them?
Could I utilize a different type of event (i.e. 'setOnTouchListener', 'setOnClickListener', etc), presumably on the top item, in conjunction with the onItemSelectedListener?
Should I utilize a different type of event by itself, perhaps on the Views inflated in the spinner themselves, without the onItemSelectedListener?
Can you help me find a better strategy than those alluded to in the bulletpoints above?
Instead of using a Spinner, use a Button that visually resembles a non-active Spinner. When the button is clicked, show a custom Dialog containing a ListView. You can use the setSelection method and/or the position in the Adapter's getView to show which item is currently selected. The ListView's OnItemClickListener will get notified that something got clicked regardless of whether the item was selected or not.
When you find which item was clicked, hide the Dialog and notify the button-containing Activity of this, so that it can update both Buttons if needed.
I have implemented something really similar to this using DialogFragments and a DialogFragmentParent and it works fine.
Have you consider something like below,
sectionSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
updateView("sectionSpin");
}
}
And for other one
issueSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int position, long arg3) {
updateView("issueSpin");
}
}
and now
private void updateView(String spinner){
if (spinner.equals(sectionSpin)){
//update spinner per requirement
}
else{
//update spinner per requirement
}
}
You can also consider some additional variable or parameters to keep track if the item selected for first time or not.
So, you could make the first item in the spinners not valid selections like "[Choose one]" or "[Select Title]" forcing the listener to fire every time.
This would have the added benefit that you will know user did select something before the next step.
Of course, this would depend on desired application flow.

add onClick listener on Android ListView With SimpleAdapter

I didn't found the solution with the search :(
I'm programming an application for reading RSS feeds.
I'm using a listview for putting a screen title and description of every feed into 2 textview. I realized that with a SimpleAdapter.
In my project I have 2 classes: MainActivity and AsynkTask.
In mainActivity I execute the asyncTask that reads RSS and puts them into the adapter and the listview
Now, how can I add a click listener on the listview for opening every feed into the browser?
I hope that I've explained correctly the problem and I'm sorry for my very bad english!
Thanks to all.
PS: if you need some of my code i will post it.
In the onCreate method of your MainActivity, if it's a ListActivity, you need to get a reference to your list view like so:
ListView lv = getListView();
Then add a click listener to it like so:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Open the browser here
}
On your list view, you need to use setOnItemClickListener

how to access ListView with CheckedTextView?

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;
}
}
});

Categories

Resources