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);
}});
Related
I want to create a ListView in an Activity with a few items (say Item1 item2 item3..) and when I click on (say item1) it proceeds to the next Activity to give me a TextView (description text) and the same repeats for item2 and item3. How can I do that?
You can add Listview setOnItemClickListener and get Description of text.
list_view.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2,
long arg3) {
String description=alist.get(arg2);
}
});
Use
list_view.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> adapter, View view,int position, long arg3)
{
String description = datalist.get(position);// datalist is the list of data which is use to create listview
Intent intent = new Intent(FirstAcitivity.this, Activity2.class);
intent.putExtra("description", description);// sending value to next activity
}
});
And in Your Activity2(destination activity after clicking/tapping user move )
String description = getIntent().getExtras().getString("description");// get description/value from activity which called this (Activity1)
Check this code
list_view.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2,
long arg3) {
TextView text = (TextView) view.findviewbyid(text view reference);
String description=text .gettext();
}
});
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();
}
});
I know how can implement listview without using listactivity.But i dont know how get the clicked item from the listview.Please anyone tell me how listen the clicked item from the listview without using listactivity.
Please Follow this Link. Here you can find simple Listview example using Activity.
Using listview.setOnItemClickListener you can get Click on Listview.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id)
{
}
You can implements OnItemClickListener and can write the method like below or just simply use eclipse to implement unimplemented method
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
}
First you have to make instance of list view Clickable like
lv.setClickable(true);
then
use following
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id)
{
}
use ListView.
Then with the listview object invoke
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//do your stuff
}
}
for click on listview in activiyt try this code...
ListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//write you action here
}
});
i am creating an application with a listview and what i want to happen is when you choose an item from the listview the application will automatically move you to the next screen. Is this possible to do?
Thanks
have a look at setOnClickListener() of your ListView. then you can use startActivity() to skip to the next screen/activity.
ListView lv = (ListView)findeViewById(...);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, View v, int index, long arg3) {
// index = the index of the clicked element in the listview
// create an intent and add index to its data
startActivity(intent);
}
});
Have a look at onItemClick and use startActivity() to bring up a new screen.
For listening the list item click events you need to override onItemClickListener method .
See here :
mylist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent=new Intent(getApplicationContext(),NextActivity.class);
//If you want to pass parameters/values to NextActivity use putExtra
intent.putExtra("my_data","my_values");
startActivity(intent);
}
});
try this code..it will helpful for u..
"list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent=new Intent();
intent.setClass(nextScreen.this,profile_details.class);
intent.putExtra("profile", mArray.get(mPosition));
startActivityForResult(intent,0);
}
});"
use this code.....
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