Position of particular item in the listview - android

I have a listview with a lot of variables (about 1000). How can I get the position of a particular string variable (eg "Washington")?
That is, I want to say how knowing the name of the variable (name of city) to get its position in the listview.

Depends on the ListAdapter that you're using. If you have an ArrayAdapter and a simple array of strings the code would be something like:
ArrayAdapter arrayAdapter = (ArrayAdapter) listView.getAdapter();
int position = arrayAdapter.getPosition("Washington");

Related

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!

Listview sorting by priority wise

I had a list view containing 5 arrays, 4 arrays are static arrays and 1 array is dynamic array coming from the server.
My question is how can i sort the entire list view using 1 array(dynamic)
My Adapter is like this
Adapter adapter=new Contacts_Adapter(class.this, Contacts_Names_Array, Contacts_Numbers_Array, Contacts_Images_Array, Contacts_Ids_Array, Chat_List_Array);
in the above code contain 5 arrays
Static Arrays = Contacts_Names_Array, Contacts_Numbers_Array, Contacts_Images_Array, Contacts_Ids_Array
Dynamic Array : Chat_List_Array
Based on the dynamic array i want sort the list view
Any suggestion.....?
If you merge all those arrays into one list of POJO objects whose members are contactName, contactNumber,..., contactId then you can use Collections.sort to sort the list.
Merging is simply creating a class like this
class Bla{
String contactName, contactNumber;
int contactId;
}
which you fill with the values from your arrays. And then put together the results in one list. If you use Adapter then you have likely done this as Adapter only corresponds to one list of objects.
To sort based on id, you define a Comparator
public class BlaIdComparator<Bla> implements Comparator<Bla> {
public int compare(Bla bla1, Bla bla2){
return bla1.contactId - bla2.contactId;
//this depends on your sorting order, ascending or descending
}
}
then use it on your list like this
Collections.sort(list, new BlaIdComparator());
then refresh your Adapter for the change to take effect
adapter.notifyDatasetChanged();

What does RHS of this code statement do?

ListView listView = (ListView) findViewById(R.id.listview);
It would be understandable for me if it was something like
ListView listView = new ListView()
But I don't understand what the RHS of ListView listView = (ListView) findViewById(R.id.listview) means; I know that LHS creates a reference variable named listView which will contain the reference to an object of ListView.
To the best of my understanding, is it retrieving a view by findViewById() and parsing to a ListView object (how can an object of one type be even parsed into an object of another type) , and then assigning a reference to that ListView object in listView reference variable?
Thank you in advance.
R.id.listview
here in one of your xml layouts you name a list as "listview"
android assigns id to every name you allot. id are stored in R java file
it would be like
public static final int listview=0x7f050002;
even you could directly use this int value in place of R.id.listview
findViewById(R.id.listview);
this will tell your activity to find a view (whose id is stored as R.id.listview)
(listview)
you are casting your view as a LISTVIEW object
and assigning it to the
listView
object of class ListView

Splitting a string and putting it inside a listview

I asked a question before about splitting string but maybe it wasn't clear enough.
I made a simple activity which has an example to what my problem is.
I have a message and it's a long one coming from a server.
I need to split this message and put it inside a listview, I'll show you my code.
public class Page1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity6);
String message = "0---12,,,2013-02-12 08:04,,,this is a test,,,0---11,,,2013-02-12 08:05,,,and this is why it is damaged,,,0---10,,,2013-02-12 08:06,,,what comes from select data randomly";
String[] variables = message.split(",");
ListView listView1 = (ListView) findViewById(R.id.listView12);
String[] items = { variables.toString() };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
}
}
Now let's say that the split is commas ", " so it will be
0---12 ------->ID1
2013-02-12 08:04 ------------>date1
this is a test ----------->subject1
0---11 ------->ID2
2013-02-12 -8:05 ------------>date2
and this is why it is damaged ----------->subject2
And so on, now what I can't do is that I want to put these strings in a loop and write them to a listview such that the subject1 should be in item1 and date1 should be in subitem1 like this
Subject1
Date1
------
Subject2
Date2
------
This is how the listview should look like
Can anyone help me with this please?
You would need to create a custom ArrayAdapter to populate a ListView from your objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs, in it you will override the getView method and use the position parameter you receive for the corrent row View to indicate the row index.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter
Please note that ArrayAdaper is designed for items containing only one single TextView. From the docs:
A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView
Consider subclassing ArrayAdapter (docs) and override its getView method.

How do I set spinner value to string

I have a spinner set up like this:
ArrayAdapter<String> states = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.stateabbrev));
states.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(states);
As you can see, the source is an array.xml file.
I want to know how to populate it if I know the array value. For instance, I am retrieving information from my database and the user is from "KY" so I have a string "KY" and I want the spinner selection to be on "KY"
at first we should get position of "KY"
int position = states.getPosition("KY");
after that, select in spinner with position
state.setSelection(position);

Categories

Resources