get selected item in a list view - android

I have used a list view in view flipper, I want that when user selected a list item then it should be stay selected when user select other list item or come back to that "Listview" activity.how is it possible?

list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,
View arg1, int position, long arg3)
{
String selectedItem = list.getSelectedItem().toString();
});
Here the selectedItem contains the what you have selected in the list.

try like this
simpleAdapter = new SimpleAdapter(this, docList, R.layout.your_layout,
new String[] { "UserName", "Status"}, new int[] {
R.id.username, R.id.status});
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
registerForContextMenu(listView);
listView.showContextMenu();
tv = (TextView) arg1.findViewById(R.id.username);
getListName = tv.getText().toString();
tv1 = (TextView) arg1.findViewById(R.id.status);
getFriendUserId = tv1.getText().toString();
}
});

Related

Android ListView has some items exactly same, how distinguish which one is clicked?

I have a ListView in my Android app, which it has some item with exactly same
name. How can distinguish which one is clicked? Is there any solution?
For example, I have retrieved these rows from database:
How do I send their ID and receive back their ID by selecting the name in ListView?
(I just want to display item's name)
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// use the position to distinguish the view clicked
});
}
With the position is:
plansList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int intPosition = position;
String clickedValue = plansList.getItemAtPosition(intPosition).toString();
}
});
String[] locations = {"russia", "ukraine", "brazil"};
locationList = new ArrayList<String>(Arrays.asList(locations));
search_listview_result = (ListView)findViewById(R.id.search_listview_result);
searchResultListViewAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, android.R.id.text1, locationList);
search_listview_result.setAdapter(searchResultListViewAdapter);
search_listview_result.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SharedPreferences.Editor editor = getSharedPreferences("SelectedLocation", MODE_PRIVATE).edit();
editor.putString("Name", locationList.get(position));
editor.putString("Lat", String.valueOf(locationLatList.get(position)));
editor.putString("Long", String.valueOf(locationLongList.get(position)));
editor.commit();
finish();
}
});

Spinner only saving first word to string

I am trying to get the spinner to display the item which i have selected. But it is only displaying the first word even if i choose the ones below. Here is the code i am using
ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
getApplicationContext(), R.layout.spinner_item, R.id.textView1, al);
spFacilityType.setAdapter(aa1);
spFacilityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
int index = arg0.getSelectedItemPosition();
position = index;
}
});
final String Strspinner = spFacility.getItemAtPosition(position).toString();
Use onItemSelected instead of onNothingSelected.
do the code in onItemSelected()..
String s= spFacilityType.getSelectedItem().toString();
now s will show the selected item

search item position

I am developing an Android application in which I am using a listview and search bar. When I click on any searched item it will show starting items, not the clicked one.
Can anyone help me how to get the position of selected item?
You can use this for getting position of your selected Item
YourList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SelectedId = YourList.getItemIdAtPosition(arg2);}});
- Use getItemIdAtPosition() method.
Eg:
ListView lv = (ListView) findViewById(R.id.listView_ProductList);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
i = new Intent(ProductActivity.this, ProductDetails.class);
// i.putExtra("keyAapo", arg2 );
i.putExtra("keyAapo", (int) lv.getItemIdAtPosition(arg2));
startActivity(i);
}
});
First, while clicking the listview item,
get the position of that item in listview
Pass the strings/values to that intent if any.
Intent for navigating to next class as below:
YourListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor listItem = (Cursor)<YourListView>.getItemAtPosition(position);
String lID = listItem.getString(1);
Intent i= new Intent(<ClassName>.this, <Navigate to Class>.class)
i.putExtra("lID", lID); // key-value pair
startActivity(i);
}});

Android list view the single value of a row

is possible get the value of a single row.
In partycular i wont get in a string the value of id in my row of mt list with adapter.
In each row there are value id and label
Thaks a lot and sorry for my english
ListView list;
list = (ListView) findViewById(R.id.listView1);
mSchedule = new SimpleAdapter(getApplicationContext(), result ,R.layout.row_meteo,
new String[]{"id", "label"}, new int[] {R.id.textView1,R.id.textView2});
list.setAdapter(mSchedule);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String idPlace = mSchedule.getItem(position).toString();
Toast.makeText(getApplicationContext()," Cliccato sul luogo :"+idPlace,Toast.LENGTH_LONG).show();
}
}
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view.findViewById(R.id.textView1); //<---------
String str = tv.getText();
}
Replace
String idPlace = mSchedule.getItem(position).toString();
with
String idPlace =((TextView)view.findViewById(R.id.textView1)).getText().toString();

How to get text from AutoCompleteTextView?

I have an AutoCompleteTextView in my app which works. I have successfully created an onClickItemListener. The question is how to grab the text the user selected.
And this is the thing: I have an ArrayList with words being passed to the Adapter to search for suggestions. As the user types a word the suggestions list gets shorter (in rows on the UI side) so when i want to get the word from the ArrayList at the index the user selected i get the wrong word because the indexes doesn't match.
How can I get the text (String) the user chose without having to mess with the index?
Here's my code:
public class AutocompleteActivity extends BaseActivity {
private DBManager m_db;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete);
m_db = new DBManager(this);
final ArrayList<String> words = m_db.selectAllWords();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, words);
AutoCompleteTextView tv = (AutoCompleteTextView)findViewById(R.id.autocomplete);
tv.setThreshold(1);
tv.setAdapter(adapter);
tv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("SELECTED TEXT WAS------->", words.get(arg2));
}
});
}
}
Yeah... unfortunately the name of the parameters on the onItemClick method you must implement are not so self-descriptive but here is an example with the names of what they are:
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
String selection = (String)parent.getItemAtPosition(position);
//TODO Do something with the selected text
}
});
parent The AdapterView where the click happened.
view The view within
the AdapterView that was clicked (this will be a view provided by the
adapter)
position The position of the view in the adapter
id The row id of the item that was clicked.
For more info see: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html
arg0 being your AdapterView and arg2 the position.
Have you tried:
arg0.getItemAtPosition(arg2);
I think what you are looking for is this.
String s = this.mCountry.getEditableText().toString();
Where mCountry is the AutoCompleteTextView.
this.mCountry = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
ArrayAdapter<String> adapterCountry = new ArrayAdapter<String>(this, R.layout.list_item, countries);
this.mCountry.setAdapter(adapterCountry);
mCountry is the list of countries, and I wanted to save the country selected in SharedPreferences.
Hope this helps.
Easiest of all
For Getting text of the selected suggestion in AutoCompleteTextView use this
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("========>>", autoCompleteTextView.getText().toString());
}
});
try this:
txtPurpose.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Purpose selected = (Purpose) arg0.getAdapter().getItem(arg2);
txtPurpose.setTag(selected);
}
});
One another of getting text of suggestion selected in AutoCompleteTextView is
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
TextView txtvw=(TextView) view;
String str=txtvw.getText().toString();
int index = contactNames.indexOf(str);
}
Here is the code that will solve the problem.
private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
int index = (int) view.getTag();
Object item = parent.getItemAtPosition(index);
if (item instanceof SearchItemShareConnectionDAO) {
SearchItemShareConnectionDAO dao = (SearchItemShareConnectionDAO) item;
}
}
};
SetTag(Dao.getPosition) in getView() method of adapter.
To get the text of the displayed item selected by the user
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String selectedItemText = arg0.getItemAtPosition(arg2);
Log.i("myTag", "SELECTED TEXT WAS["+selectedItemText+"]);
}
Following best practices, please use some form of descriptive nomenclature for your variables, your code will make more sense:
public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int selectedItemIndexIn, long id)
There is also a way to get item outside the onItemClick:
int index = tv.getListSelection();
if (index != ListView.INVALID_POSITION) {
Object item = tv.getAdapter().getItem(index);
}
beware that int getListSelection() may return ListView.INVALID_POSITION if there is no dropdown or if there is no selection.
For me, but on setOnItemSelectedListener():
arg0.getSelectedItem();
was enough. The same should work on setOnItemClickListener() I guess.
Kotlin version
autoCompleteId.setOnItemClickListener { parent, view, position, id ->
val selectedItem = parent.getItemAtPosition(position).toString()
Log.d("SELECTED ITEM", selectedItem )
}
Output:
D/CLASSROOM: selectedItem

Categories

Resources