Listen for array adapter onClick - android

I have a MultiAutoCompleteTextView and when you click on an item in my list of AutoComplete words, it is inserting the word I want correctly, however, when I click on the word I want to insert, I want to move my cursor to a different position than the end of the edittext's text. How can I set a listener for my adapter? My code is as follows:
setContentView(R.layout.activity_main);
String[] suggestions = getResources().getStringArray(R.array.list_of_suggestions);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,suggestions);
MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView)findViewById(R.id.code_text);
textView.addTextChangedListener(mTextEditorWatcher);
textView.setAdapter(adapter);
textView.setTokenizer(new SColonTokenizer());
I can't set it in my text changed listener as this is called every time the user enters a character. How can I listen for an onClick on my array adapter? I know it is possible to do this using a ListView, but my suggestions are handled by the adapter and not making use of a listView to do this.
After clicking, I want to move the cursor inside the parenthesis, I know how to do this using
textView.setSelection(textView.getText().length());
just I am unsure how to call this after clicking on an array adapter item

You can use OnItemClickListener like
textView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
textView.setSelection(textView.getText().length());
}
});

Related

Using simple_list_item_checked, wherever I click it's getting checked

I want to be able to click on every item on my list, and then move to another activity (this I know how to do)
And also when I click (exactly on the checkbox) then it should be clicked, because now, wherever I click on the item line, the checkbox is getting clicked.
I also want, once I click on any of the checkboxes, to get a count, because I want to put a progress bar, to show how far my students went in my book in the app,
String[] items = getResources().getStringArray(R.array.chapters);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,android.R.id.text1, items);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
CheckedTextView check = (CheckedTextView)view;
check.setChecked(!check.isChecked());
}
});
You are now checking the checkbox on the listView item click, if you want to do it on the checkbox click you should make your custom adapter instead of working with the default adapter.
Try to add checkbox in separate view of list or list item. For ex. if you have RelativeLayout then you can add your checkbox toLeftOf your listview that way your checkbox won't get clicked as you click on list item.
So I guess it's not possible to make it get clicked when only clicking on it,
So I will try to make separate check boxes, as you suggested,
Any code idea for how to get all the check boxes values (count) so I can make a g

How can i set click listener on individual item in a listitem?

I have a listview binded to an arrayadapter of strings... How do i set different clicklistener on each item in the list view
Register an item click listener on your ListView with http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)
In your AdapterView.OnItemClickListener#onItemClick you will get the view and an id.
You can use the id if you have a given order in your arraylist, or you could use View#findViewById and get the content of the view to figure out which item it is.
That means, you would not set a listener per item, rather you would have one listener for all items, and then do different things based on which item it was.
I think you could use it like this.
adapter = new ArrayAdapter(getContext(), R.layout.list_item_forecast, R.id.list_item_forecast_textView, new ArrayList<String>());
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//what ever you want to give on item click
}
});
here by checking the position inside onItemClick you can assign specific tasks.

Get the text of listview Android

I have listview that populated from database.
When I click a row in that listview, it will pass data to another activity with the content of activity depend on the passing data.
This is my code:
private final ArrayList<String> results = new ArrayList<>();
private final AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(ContentPick.this, Content.class);
String text = results.get(position);
i.putExtra(ID_EXTRA, String.valueOf(text));
startActivity(i);
}
};
The activity contain listview also have edittext that have filtering (when I type some text in the edittext it will filter the listview according to the text). The problem is, if I'm click a row in listview after I filtered it, the activity that showing is same result as if I'm not filter the listview.
Example:
When the edittext is empty, the first row of the listview is Cell structure . When I click the Cell structure it will show information about Cell structure.
I click Back, and I type Blood in EditText, it will filter the listview, and the first row of the listview is Blood Cell. When I click the row Blood Cell, it still show information about Cell Structure.
What I'm trying to do is with this code:
String text = results.get(position);
But I think it's the cause of the problem.
Please help me to solve this problem. Thank you very much.
change position in your code.like below
final int position
Use mAdapter.notifyDataSetChanged() to actual notify your dataset position. It might help you to solve problem.
After some trial and error I finally solve the problem.
I make a method displayResult and I set the adapter like this
dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, results);
In the method AdapterView.OnItemClickListener, I change variable text to this
String text = dataAdapter.getItem(position);
And it works!

Get reference to an object within a ListView

Up front: This is my first attempt at an Android app. I'm in that strange place of not knowing what to search for to find the answer to my question.
What I have accomplished is:
Created a custom class myCustomClass with properties of 'title' and 'youTubeUrl'
Created an ArrayList<myCustomClass>
Added multiple elements to ArrayList<myCustomClass>
Created a custom ArrayAdapter and attached it to the the arraylist.
Added an onItemClickListener to the custom ArrayAdapter.
All of that works good. I would like to show the title in the ListView and then when the user clicks the list view item, I'd like to get a reference to the youtubeUrl property.
Here's what I have for the adapter code:
MyListAdapter myListAdapter = new MyListAdapter(this, R.layout.my_list, elements);
myList.setAdapter(myListAdapter);
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
myListAdapter.notifyDataSetChanged();
Thanks for your help.
You can use the position property in onItemClick to go back to your data source and find the relevant item. From there you should be able to retrieve the Url.
As another poster implied, it depends on what you are using in your adapter. Assuming it's MyCustomClass. You can do something like this in your onItemClick method:
MyCustomClass selection = (MyCustomClass) getListView().getItemAtPosition(position);

how does cursor know which item is clicked in listview?

I have a activity displaying call logs in a ListView. The adapter used here to populate listview extends CursorAdapter. Listview is set to onItemClickListener(..). My Question is whenever an item is clicked how does cursor get the data? how does cursor know which position is clicked and need to get data from clicked position only? I have provided a code snippnet.
public class CallLog extends Activity
{
ListView mListView;
Cursor cursor;
//other variables
public void OnCreate()
{
setContentView(R.layout.calllog);
//SQLiteDatabaseInstance db
cursor = db.query(...parameters to get all call logs...);
mListView.setOnItemClickListener(this);
}
public void OnItemClick( // all arguments... )
{
//there is a column name 'NAME' in call log table in database
String name = cursor.getString(cursor.getColumnIndex(Database.NAME))
//here name is obtained of the clicked item.
}
Cursor is a result set. how does the cursor know which item is clicked? What can be the methods implicitly called by cursor that gives it position of clicked item?
If there are any links of similar question then pls provide.
I hope I am able to make you understand the question. Thank you
Try this:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//move cursor to clicked row
cursor.moveToPosition(position);
}
Specifically it is NOT the Cursor that knows who clicked on what. This is actually handled by the Adapter. The adapter is used to group elements together and allow abstraction as such that they can be handled in a uniform way.
Any form of list, always has an adapter, and this is exactly why the adapter works so well. If you look at a Custom Listview with a Custom Adapter, you'll see exactly how this is done.
Example:
http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/
You should use cursor.moveToposition(position) inside function to get to the position of that clicked item. After that you apply this and when you will click on any item, the cursor will be set on that item and then you can use that particular item for your operation.
mListView..setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0,
View view, int position, long id) {
// here position gives the which item is clicked..
}
});
Additionally check this link for ListView ListView and ListActivity
It may help you..

Categories

Resources