Answer the question
In order to leave comments, you need to log in
How to sort data in ListView?
There is the following code that receives json and parses it, then the data is passed to the ListView through the SimpleAdapter. Three arrays are passed to the sheet: name, number, and skills. You need to sort by name. How can I do that?
private class GetJson extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d(LOG_TAG, "Json data is downloading...");
Toast.makeText(getApplicationContext(),
"Json data is downloading...", Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(Void... voids) {
HttpHandler httpHandler = new HttpHandler();
String jsonString = httpHandler.makeServiceCall(URL_MOCKY);
Log.d(LOG_TAG, "Response from url: " + jsonString);
if (jsonString != null){
try{
JSONObject jsonObject = new JSONObject(jsonString);
//json объект company
final JSONObject jsonObjectCompany = new JSONObject(jsonObject.getString(COMPANY));
//массив competences из объекта company
JSONArray jsonArrayCompetences = jsonObjectCompany.getJSONArray(COMPANY_COMPETENCES);
final StringBuilder stringBuilder = new StringBuilder();
//создание строки из массива competences
for (int i = 0; i < jsonArrayCompetences.length(); i++) {
stringBuilder.append(jsonArrayCompetences.get(i).toString());
if (i != jsonArrayCompetences.length() - 1){
stringBuilder.append(", ");
} else
stringBuilder.append(";");
}
//вывод данных в TextView (name, age and competences)
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
String name = "Name: " + jsonObjectCompany.getString(COMPANY_NAME);
String age = "Age: " + jsonObjectCompany.getString(COMPANY_AGE);
String competences = "Competences: " + stringBuilder.toString();
textViewCompName.setText(name);
textViewCompAge.setText(age);
textViewCompCompetences.setText(competences);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
//массив employees из объекта company
JSONArray jsonArrayEmployees = jsonObjectCompany.getJSONArray(COMPANY_EMPLOYEES);
/*
Создание объектов из элементов массива employees и
получение его данных: имени, номера телефона и навыков.
Навыки представляют массив, поэтому переводятся в строку
*/
for (int i = 0; i < jsonArrayEmployees.length(); i++) {
JSONObject jsonObjectEmployees = jsonArrayEmployees.getJSONObject(i);
JSONArray jsonArraySkills = jsonObjectEmployees.getJSONArray(EMPLOYEES_SKILLS);
StringBuilder stringBuilderSkills = new StringBuilder();
for (int j = 0; j < jsonArraySkills.length(); j++) {
stringBuilderSkills.append(jsonArraySkills.get(j));
if (j != jsonArraySkills.length() - 1){
stringBuilderSkills.append(", ");
}
}
String name = jsonObjectEmployees.getString(EMPLOYEES_NAME);
String phone = jsonObjectEmployees.getString(EMPLOYEES_PHONE);
String skills = stringBuilderSkills.toString();
//добавление данных в Map
Map<String, String> map = new HashMap<>();
map.put(ATTRIB_NAME, name);
map.put(ATTRIB_PHONE, phone);
map.put(ATTRIB_SKILLS, skills);
//добавление Map в лист
arrayList.add(map);
}
//создание from и to массивов для SimpleAdapter
from = new String[] {ATTRIB_NAME, ATTRIB_PHONE, ATTRIB_SKILLS};
to = new int[] {R.id.textViewName, R.id.textViewPhone, R.id.textViewSkills};
} catch (final Exception e){
Log.e(LOG_TAG, "Parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(LOG_TAG, "Couldn't get json from server");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
ListAdapter listAdapter = new SimpleAdapter(getApplicationContext(),
arrayList,
R.layout.listview_item,
from,
to);
listViewData.setAdapter(listAdapter);
Toast.makeText(getApplicationContext(), "Upload finished", Toast.LENGTH_SHORT).show();
}
}
Answer the question
In order to leave comments, you need to log in
Yes, it is sorted before being returned to the adapter, and if you need to dynamically change the position of cells - inside the adapter, change the position of objects in the data source for cells and do notifyDataSetChanged();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question