Get the text of listview Android - 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!

Related

Check all of adapter elements in ListView

I have CustomAdapter which I am using for populating ListView with some data.
Each element in ListView has two variables. For each listview (in onItemClick method) I must check this variables and If they are the same - do some code and If they are different - do another code, for example Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
So I have tried this:
private List<SomeItem> items = new ArrayList();
//items were created
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for(int i=0; i<=items.size(); i++) {
SomeItem item = items.get(position);
String tmpCI = item.getFirstVariable();
String tmpPCI = item.getecondVariable();
if (!tmpCI.equals(tmpPCI)) {
//some code
} else {
Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
}
}
}
});
But all of my listview elements have values of the first element in those two variables.
So how can I do something like item.next(); for validating all of items in listview?
UPD:
Sorry, I will provide more information about what I am doing after checking variables of listview items for understanding my issue.
I have one more adapter:
SomeAnotherAdapter adapterPr = new SomeAnotherAdapter(this, R.layout.list_tem_another, itemsAnother);
and one more listview:
listViewAnother.setAdapter(adapterPr);
First of all I understood, that first variable should be from first listview and the second variable from another listview.
In this listViewAnother I have many items, which has some "id". For example 1st, 5th and 20th elements have id 90 and other elements have id 100.
We can say, that items from the first listview also have "id".
So I must check if(first variable = second variable) and then show in listViewAnother only items that have id which equals ID from clicked item in listView.
I tried: adapterPr.remove(item2); but then I understood, that I need all of items because I can go back to listView and press another item which will need those removed elements.
Now, hope I provided full information and you will be able to help me improve my code.
Do you need to perform the check on every element of the adapter when you click on one element of the adapter? If not, you don't need a loop. If you do, your loop should be iterating over the original list, and does not need adapter position at all.
In general when using adapters and lists, you should use the adapter's position and the adapter's data set to perform any tasks. It's not good practice to use the adapter position to get an item from the original list.
Simply set one onItemClickListener which gets the corresponding item from the adapter, and do what you need to from there:
private List<SomeItem> items = new ArrayList();
//items were created
SomeAdapter adapter = new SomeAdapter(this, R.layout.list_item, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SomeItem item = adapter.getItem(position);
String tmpCI = item.getFirstVariable();
String tmpPCI = item.getecondVariable();
if (!tmpCI.equals(tmpPCI)) {
//some code
} else {
Toast.makeText(EPG.this, "Variables are different", Toast.LENGTH_SHORT).show();
}
}
});

Listen for array adapter onClick

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

Android:Checked Listview Item Goes at Last

My ListView looks like this:
[CheckBox] [TextView]
My question is, how can I change the item position when CheckBox is checked? I mean to say, if the user checked any item from ListView, then the checked item goes to the end, so its position changes from current to last.
Thanks in Advance.
Use Custom adapter for listView. here is an example. Now from your ListActivity class
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, Object> map = (HashMap<String, Object>)
lv.getItemAtPosition(position);
// get the item Position from here and make the nacessary changes
// and update the listview again.
}
Without posting any code, this will have to be a brief overview of the pseudo steps you need to take
You simply need to update the ordering of your data set being used by your adapter (usually an arraylist or array of objects), and then called
notifyDataSetChanged()
on your adapter.
So in your case, you want to take the element at the position that was clicked, and put it at the end of your arrayList of objects.
If you post some code, you may get more detailed answers however
Here is the step you can follow. I have not tested it but you can try this.
// your value array you are binding with listview
private final String[] values;
// put default value to "n". size will be the same as for values array.
private final String[] valuesChecked;
onClick of Checkbox Listview
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get checkbox is checked or not
if it is checked then{
valuesChecked[position]="y";
}else{
valuesChecked[position]="n";
}
// short your values array by first "n" and after "y".
// call your adapter.notifyDataSetChanged();
}
Good Luck :)

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

Pass custom list item content to new activity when list item is clicked from a listview

First of all I want to know if this is possible. Essentially I have a listview that is being populated programatically with a custom list item view and a custom adapter. This works like a charm but the problem is that I have to sort this list in a specific way once it is populated. The values that are being put into the list are stored in String arrays. The problem is that when I sort the listview it changes the index of where I thought the values would be stored. I want to start a new activity with certain values found in the list view. Is this possible? I could take a string from the list item and can look it up in my arrays storing the values when that item is clicked. I can then start a new intent with extras so that I can pass this new data to the new activity. So, is it possible to extract a string from a list item?
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3){
Object o=lv.getItemAtPosition(position);
UtilityClass u=(UtilityClass)o;
String s=u.getMyData();
Intent edit = new Intent(context, AnotherActivity.class);
edit.putExtra("your_identifier", Integer.toString(position));
startActivity(intent);
}});
If u have implemented onItemClickListner / onItemSelectListner then you can get a callback onItemClicked()/ onItemSelected() from there using the Adapter get the item from selected position. and the same can be sent to another activity using a bundle/extra

Categories

Resources