Question

I am fetching search results from my server and displaying them in a listview. Firstly, I need to change this into a scrollable dropdown. The values in the dropdown are in the format: SchoolA ; Russia I need to capture this text on click and split it into SchoolA and Russia. The code is as follows in activity page:

private void populateResults(String response) {

List<String> resultsFromServer = parseResponse(response);

ListView resultsList = (ListView) findViewById(R.id.schoolssListView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, resultsFromServer); 
resultsList.setAdapter(arrayAdapter); 

addListenerOnClick(); // this is the method in which i need the above mentioned operations}

Now, the code in the xml page for listview is as below:

<ListView
    android:id="@+id/schoolssListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>
Était-ce utile?

La solution

Try this

listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {

   // Your code here , position points to your requirement index

}

});

Onclick you can get the position of the item ,Get the String from the arrayList of that position so you will get Eg: StringA;Russia Then split it with seperator ; and use it

UPDATE

String currentString = "StringA;Russia";

String[] separated = CurrentString.split(";");
separated[0]; // it will contain StringA
separated[1]; // it will contain russia

Alternate

StringTokenizer tokens = new StringTokenizer(CurrentString, ":");

String first = tokens.nextToken();// this will contain "StringA"
String second = tokens.nextToken();

Autres conseils

here is one example to implement Custom List View and overriding onItemclick on listview in android

Hope this will help.You can ask if you have any further queries.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top