Android 对JSONArray排序后,自定义列表视图是否未更改?
我正在对JSONArray进行排序,并在自定义列表视图中显示它们,但排序后,数据在自定义列表视图中不会发生更改 以下是我的代码,用于选择要执行的排序:Android 对JSONArray排序后,自定义列表视图是否未更改?,android,json,sorting,listview,collections,Android,Json,Sorting,Listview,Collections,我正在对JSONArray进行排序,并在自定义列表视图中显示它们,但排序后,数据在自定义列表视图中不会发生更改 以下是我的代码,用于选择要执行的排序: fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder=new AlertDialog.Builder(SearchResu
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(SearchResult.this);
builder.setTitle("Sort List Item");
builder.setItems(new CharSequence[]{"By Name", "By Father Name","By EPIC","By House Number"}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
switch (i)
{
case 0:
Toast.makeText(getApplicationContext(), "By Name", Toast.LENGTH_SHORT).show();
sortJsonTechnique="1";
if (myData)
{
sortByName(sortdata,sortJsonTechnique);
}
break;
case 1:
Toast.makeText(getApplicationContext(), "By Father Name", Toast.LENGTH_SHORT).show();
sortJsonTechnique="2";
if (myData)
{
sortByName(sortdata,sortJsonTechnique);
}
break;
case 2:
Toast.makeText(getApplicationContext(), "By EPIC", Toast.LENGTH_SHORT).show();
sortJsonTechnique="3";
if (myData)
{
sortByName(sortdata,sortJsonTechnique);
}
break;
case 3:
Toast.makeText(getApplicationContext(), "By House Number", Toast.LENGTH_SHORT).show();
sortJsonTechnique="4";
if (myData)
{
sortByName(sortdata,sortJsonTechnique);
}
break;
default:
break;
}
}
});
builder.show();
}
});
在fab按钮中进行选择后,单击i sort json数组。
下面是解析json数组的代码:
private void sortByName(String mysortJson,String sortType)
{
List<JSONObject> jsonObjects=new ArrayList<JSONObject>();
try {
JSONObject object=new JSONObject(mysortJson);
JSONArray jsonArray=object.getJSONArray("Data");
for (int i=0;i<jsonArray.length();i++)
{
jsonObjects.add(jsonArray.getJSONObject(i));
}
if (sortType.equals("1"))
{
Collections.sort(jsonObjects, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject a, JSONObject b) {
String val1=new String();
String val2=new String();
try {
val1=(String) a.get("Name");
// Log.e("Value1",""+val1);
val2=(String) b.get("Name");
// Log.e("Value2",""+val2);
} catch (JSONException e) {
e.printStackTrace();
}
return val1.compareTo(val2);
}
});
for (int j=0;j<jsonObjects.size();j++)
{
jsonArray.put(jsonObjects.get(j));
}
voter_id=new String[jsonArray.length()];
boothname_id=new String[jsonArray.length()];
voter_name=new String[jsonArray.length()];
voter_f_m_h_name=new String[jsonArray.length()];
voter_epic=new String[jsonArray.length()];
voter_h_no=new String[jsonArray.length()];
voter_mobile=new String[jsonArray.length()];
voter_gender=new String[jsonArray.length()];
voter_age=new String[jsonArray.length()];
get_voter_dob=new String[jsonArray.length()];
get_marriage_anniv=new String[jsonArray.length()];
get_voter_caste_id=new String[jsonArray.length()];
get_voter_status_id=new String[jsonArray.length()];
get_voter_social_status_id=new String[jsonArray.length()];
for (int c=0;c<jsonArray.length();c++)
{
JSONObject fetchVoter = jsonArray.getJSONObject(c);
voter_id[c] = fetchVoter.getString(KEY_VOTER_ID);
boothname_id[c] = fetchVoter.getString(KEY_BOOTHNAME_ID);
voter_name[c] = fetchVoter.getString(KEY_SEARCH_NAME);
voter_f_m_h_name[c] = fetchVoter.getString(KEY_SEARCH_F_H_M_NAME);
voter_epic[c] = fetchVoter.getString(KEY_SEARCH_EPIC_NAME);
voter_h_no[c] = fetchVoter.getString(KEY_SEARCH_HOUSE_NUMBER);
voter_mobile[c]=fetchVoter.getString(KEY_SEARCH_MOBILE);
voter_gender[c]=fetchVoter.getString(KEY_SEARCH_GENDER);
voter_age[c]=fetchVoter.getString(KEY_SEARCH_AGE);
get_voter_dob[c]=fetchVoter.getString(KEY_SEARCH_DOB);
get_marriage_anniv[c]=fetchVoter.getString(KEY_MARRIEGE_ANNIV);
get_voter_caste_id[c]=fetchVoter.getString(KEY_GET_CASTE_ID);
get_voter_status_id[c]=fetchVoter.getString(KEY_GET_STATUS_ID);
get_voter_social_status_id[c]=fetchVoter.getString(KEY_GET_SOCIAL_STATUS);
}
CustomSearchList adapter=new CustomSearchList(this,ParseJsonData.voter_id,ParseJsonData.boothname_id,ParseJsonData.voter_name,ParseJsonData.voter_f_m_h_name,ParseJsonData.voter_epic,ParseJsonData.voter_h_no,ParseJsonData.voter_mobile,ParseJsonData.voter_age,ParseJsonData.voter_gender,ParseJsonData.get_marriage_anniv,ParseJsonData.get_voter_dob,ParseJsonData.get_voter_caste_id,ParseJsonData.get_voter_status_id,ParseJsonData.get_voter_social_status_id);
searchlist.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
else if (sortType.equals("2"))
{
Collections.sort(jsonObjects, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject a, JSONObject b) {
String val1=new String();
String val2=new String();
try {
val1=(String) a.get("FName");
// Log.e("Value1",""+val1);
val2=(String) b.get("FName");
// Log.e("Value2",""+val2);
} catch (JSONException e) {
e.printStackTrace();
}
return val1.compareTo(val2);
}
});
for (int j=0;j<jsonObjects.size();j++)
{
jsonArray.put(jsonObjects.get(j));
}
voter_id=new String[jsonArray.length()];
boothname_id=new String[jsonArray.length()];
voter_name=new String[jsonArray.length()];
voter_f_m_h_name=new String[jsonArray.length()];
voter_epic=new String[jsonArray.length()];
voter_h_no=new String[jsonArray.length()];
voter_mobile=new String[jsonArray.length()];
voter_gender=new String[jsonArray.length()];
voter_age=new String[jsonArray.length()];
get_voter_dob=new String[jsonArray.length()];
get_marriage_anniv=new String[jsonArray.length()];
get_voter_caste_id=new String[jsonArray.length()];
get_voter_status_id=new String[jsonArray.length()];
get_voter_social_status_id=new String[jsonArray.length()];
for (int c=0;c<jsonArray.length();c++)
{
JSONObject fetchVoter = jsonArray.getJSONObject(c);
voter_id[c] = fetchVoter.getString(KEY_VOTER_ID);
boothname_id[c] = fetchVoter.getString(KEY_BOOTHNAME_ID);
voter_name[c] = fetchVoter.getString(KEY_SEARCH_NAME);
voter_f_m_h_name[c] = fetchVoter.getString(KEY_SEARCH_F_H_M_NAME);
voter_epic[c] = fetchVoter.getString(KEY_SEARCH_EPIC_NAME);
voter_h_no[c] = fetchVoter.getString(KEY_SEARCH_HOUSE_NUMBER);
voter_mobile[c]=fetchVoter.getString(KEY_SEARCH_MOBILE);
voter_gender[c]=fetchVoter.getString(KEY_SEARCH_GENDER);
voter_age[c]=fetchVoter.getString(KEY_SEARCH_AGE);
get_voter_dob[c]=fetchVoter.getString(KEY_SEARCH_DOB);
get_marriage_anniv[c]=fetchVoter.getString(KEY_MARRIEGE_ANNIV);
get_voter_caste_id[c]=fetchVoter.getString(KEY_GET_CASTE_ID);
get_voter_status_id[c]=fetchVoter.getString(KEY_GET_STATUS_ID);
get_voter_social_status_id[c]=fetchVoter.getString(KEY_GET_SOCIAL_STATUS);
}
CustomSearchList adapter=new CustomSearchList(this,ParseJsonData.voter_id,ParseJsonData.boothname_id,ParseJsonData.voter_name,ParseJsonData.voter_f_m_h_name,ParseJsonData.voter_epic,ParseJsonData.voter_h_no,ParseJsonData.voter_mobile,ParseJsonData.voter_age,ParseJsonData.voter_gender,ParseJsonData.get_marriage_anniv,ParseJsonData.get_voter_dob,ParseJsonData.get_voter_caste_id,ParseJsonData.get_voter_status_id,ParseJsonData.get_voter_social_status_id);
searchlist.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
else if (sortType.equals("3"))
{
Collections.sort(jsonObjects, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject a, JSONObject b) {
String val1=new String();
String val2=new String();
try {
val1=(String) a.get("EPIC");
// Log.e("Value1",""+val1);
val2=(String) b.get("EPIC");
// Log.e("Value2",""+val2);
} catch (JSONException e) {
e.printStackTrace();
}
return val1.compareTo(val2);
}
});
for (int j=0;j<jsonObjects.size();j++)
{
jsonArray.put(jsonObjects.get(j));
}
voter_id=new String[jsonArray.length()];
boothname_id=new String[jsonArray.length()];
voter_name=new String[jsonArray.length()];
voter_f_m_h_name=new String[jsonArray.length()];
voter_epic=new String[jsonArray.length()];
voter_h_no=new String[jsonArray.length()];
voter_mobile=new String[jsonArray.length()];
voter_gender=new String[jsonArray.length()];
voter_age=new String[jsonArray.length()];
get_voter_dob=new String[jsonArray.length()];
get_marriage_anniv=new String[jsonArray.length()];
get_voter_caste_id=new String[jsonArray.length()];
get_voter_status_id=new String[jsonArray.length()];
get_voter_social_status_id=new String[jsonArray.length()];
for (int c=0;c<jsonArray.length();c++)
{
JSONObject fetchVoter = jsonArray.getJSONObject(c);
voter_id[c] = fetchVoter.getString(KEY_VOTER_ID);
boothname_id[c] = fetchVoter.getString(KEY_BOOTHNAME_ID);
voter_name[c] = fetchVoter.getString(KEY_SEARCH_NAME);
voter_f_m_h_name[c] = fetchVoter.getString(KEY_SEARCH_F_H_M_NAME);
voter_epic[c] = fetchVoter.getString(KEY_SEARCH_EPIC_NAME);
voter_h_no[c] = fetchVoter.getString(KEY_SEARCH_HOUSE_NUMBER);
voter_mobile[c]=fetchVoter.getString(KEY_SEARCH_MOBILE);
voter_gender[c]=fetchVoter.getString(KEY_SEARCH_GENDER);
voter_age[c]=fetchVoter.getString(KEY_SEARCH_AGE);
get_voter_dob[c]=fetchVoter.getString(KEY_SEARCH_DOB);
get_marriage_anniv[c]=fetchVoter.getString(KEY_MARRIEGE_ANNIV);
get_voter_caste_id[c]=fetchVoter.getString(KEY_GET_CASTE_ID);
get_voter_status_id[c]=fetchVoter.getString(KEY_GET_STATUS_ID);
get_voter_social_status_id[c]=fetchVoter.getString(KEY_GET_SOCIAL_STATUS);
}
CustomSearchList adapter=new CustomSearchList(this,ParseJsonData.voter_id,ParseJsonData.boothname_id,ParseJsonData.voter_name,ParseJsonData.voter_f_m_h_name,ParseJsonData.voter_epic,ParseJsonData.voter_h_no,ParseJsonData.voter_mobile,ParseJsonData.voter_age,ParseJsonData.voter_gender,ParseJsonData.get_marriage_anniv,ParseJsonData.get_voter_dob,ParseJsonData.get_voter_caste_id,ParseJsonData.get_voter_status_id,ParseJsonData.get_voter_social_status_id);
searchlist.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
else if (sortType.equals(4))
{
Collections.sort(jsonObjects, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject a, JSONObject b) {
String val1=new String();
String val2=new String();
try {
val1=(String) a.get("HouseNo");
// Log.e("Value1",""+val1);
val2=(String) b.get("HouseNo");
// Log.e("Value2",""+val2);
} catch (JSONException e) {
e.printStackTrace();
}
return val1.compareTo(val2);
}
});
for (int j=0;j<jsonObjects.size();j++)
{
jsonArray.put(jsonObjects.get(j));
}
voter_id=new String[jsonArray.length()];
boothname_id=new String[jsonArray.length()];
voter_name=new String[jsonArray.length()];
voter_f_m_h_name=new String[jsonArray.length()];
voter_epic=new String[jsonArray.length()];
voter_h_no=new String[jsonArray.length()];
voter_mobile=new String[jsonArray.length()];
voter_gender=new String[jsonArray.length()];
voter_age=new String[jsonArray.length()];
get_voter_dob=new String[jsonArray.length()];
get_marriage_anniv=new String[jsonArray.length()];
get_voter_caste_id=new String[jsonArray.length()];
get_voter_status_id=new String[jsonArray.length()];
get_voter_social_status_id=new String[jsonArray.length()];
for (int c=0;c<jsonArray.length();c++)
{
JSONObject fetchVoter = jsonArray.getJSONObject(c);
voter_id[c] = fetchVoter.getString(KEY_VOTER_ID);
boothname_id[c] = fetchVoter.getString(KEY_BOOTHNAME_ID);
voter_name[c] = fetchVoter.getString(KEY_SEARCH_NAME);
voter_f_m_h_name[c] = fetchVoter.getString(KEY_SEARCH_F_H_M_NAME);
voter_epic[c] = fetchVoter.getString(KEY_SEARCH_EPIC_NAME);
voter_h_no[c] = fetchVoter.getString(KEY_SEARCH_HOUSE_NUMBER);
voter_mobile[c]=fetchVoter.getString(KEY_SEARCH_MOBILE);
voter_gender[c]=fetchVoter.getString(KEY_SEARCH_GENDER);
voter_age[c]=fetchVoter.getString(KEY_SEARCH_AGE);
get_voter_dob[c]=fetchVoter.getString(KEY_SEARCH_DOB);
get_marriage_anniv[c]=fetchVoter.getString(KEY_MARRIEGE_ANNIV);
get_voter_caste_id[c]=fetchVoter.getString(KEY_GET_CASTE_ID);
get_voter_status_id[c]=fetchVoter.getString(KEY_GET_STATUS_ID);
get_voter_social_status_id[c]=fetchVoter.getString(KEY_GET_SOCIAL_STATUS);
}
CustomSearchList adapter=new CustomSearchList(this,ParseJsonData.voter_id,ParseJsonData.boothname_id,ParseJsonData.voter_name,ParseJsonData.voter_f_m_h_name,ParseJsonData.voter_epic,ParseJsonData.voter_h_no,ParseJsonData.voter_mobile,ParseJsonData.voter_age,ParseJsonData.voter_gender,ParseJsonData.get_marriage_anniv,ParseJsonData.get_voter_dob,ParseJsonData.get_voter_caste_id,ParseJsonData.get_voter_status_id,ParseJsonData.get_voter_social_status_id);
searchlist.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
// Log.e("Length Array",""+jsonArray.length());
// Log.e("Length List",""+jsonObjects.size());
} catch (JSONException e) {
e.printStackTrace();
}
}
private void sortByName(字符串mysortJson,字符串sortType)
{
List jsonObjects=newarraylist();
试一试{
JSONObject对象=新的JSONObject(mysortJson);
JSONArray JSONArray=object.getJSONArray(“数据”);
对于(inti=0;i,在将数据设置为Listview之前,应该使用java中的comparator类对数据进行排序
您可以创建一个模型类,其中包含所需的所有变量(名称、booth_id等),并将每个对象存储到ArrayList中
Collections.sort(ArrayListObject,new NameComparator());
范例
class NameComparator implements Comparator{
public int compare(Object o1,Object o2){
YourClassName s1=(YourClassName )o1;
YourClassName s2=(YourClassName )o2;
return s1.name.compareTo(s2.name);
}
}
在将数据设置为Listview之前,应该使用java中的comparator类对数据进行排序
您可以创建一个模型类,其中包含所需的所有变量(名称、booth_id等),并将每个对象存储到ArrayList中
Collections.sort(ArrayListObject,new NameComparator());
范例
class NameComparator implements Comparator{
public int compare(Object o1,Object o2){
YourClassName s1=(YourClassName )o1;
YourClassName s2=(YourClassName )o2;
return s1.name.compareTo(s2.name);
}
}
不要为字段创建一个字符串数组的数据对象,而是为JsonArray的每个元素创建一个对象。
为JsonArray中的对象创建列表。
在自定义适配器中传递该列表。
将listview适配器设置为自定义适配器
现在,只要您想按任何字段对数据进行排序,就可以使用Java collections comparator,您可以编写自己的自定义comparator
之后,只需在适配器上调用notifyDataSetChanged()
编辑-
您可以创建模型搜索项,如下所示
class SearchItem {
private String voterId;
private String boothNameId;
private String searchName;
private String houseNumber;
private String gender;
public void setVoterId(String voterId) {
this.voterId = voterId;
}
public String getVoterId() {
return voterId;
}
......................(Add as many fields you want and their corresponding getter and setter methods)
}
您的Json解析类可以这样修改(您必须填充SearchItem类的所有字段)
class ParseJsonData {
public static ArrayList<SearchList> parseSearchresult(String jsonResponse) {
try {
JSONObject searchData = new JSONObject(jsonResponse);
JSONArray searchArray=searchData.getJSONArray(SEARCH_VOTER_ARRAY);
ArrayList<SearchItem> searchList = new ArrayList<SearchItem>();
for(int index = 0; index < searchArray.length(); index++) {
JSONObject searchItem = searchArray.get(index);
SearchItem item = new SearchItem();
item.setVoterId(searchItem.optString(KEY_VOTER_ID, null);
item.setBoothNameId(searchItem.optString(KEY_BOOTHNAME_ID, null);
item.setSearchName(searchItem.optString(KEY_SEARCH_NAME, null);
item.setHouseNumber(searchItem.optString(KEY_SEARCH_HOUSE_NUMBER, null);
item.setGender(searchItem.optString(KEY_SEARCH_GENDER, null);
searchList.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
return searchList;
}
}
类ParseJsonData{
公共静态ArrayList parseSearchresult(字符串jsonResponse){
试一试{
JSONObject searchData=新的JSONObject(jsonResponse);
JSONArray searchArray=searchData.getJSONArray(SEARCH\u VOTER\u ARRAY);
ArrayList searchList=新建ArrayList();
对于(int index=0;index
现在,您可以创建自己的自定义适配器,如下所示
class CustomSearchList extends BaseAdapter {
ArrayList<SearchItem> mSearchList;
Context mContext;
public CustomSearchList(Context context, ArrayList<SearchItem> searchList) {
mContext = context;
mSearchList = searchList;
}
..........................(Implement methods for BaseAdapter or other methods you want to write)
}
class CustomSearchList扩展了BaseAdapter{
ArrayList-mSearchList;
语境;
公共CustomSearchList(上下文,ArrayList searchList){
mContext=上下文;
mSearchList=搜索列表;
}
……………(实现BaseAdapter的方法或要编写的其他方法)
}
这里searchList作为引用传递,所以该列表中的任何更新以及对适配器的notify调用都将反映更改
可以这样修改listview类。parseSearchList()方法返回所有searchlist的arraylist
private void getList(String json) {
ArrayList<SearchItem> searchList = ParseJsonData.parseSearchresult(json);
CustomSearchList adapter = new CustomSearchList(this, searchList);
searchlistView.setAdapter(adapter);
mBtn.setOnClickListener(this, new OnClickListener() {
@Override
public void onClick(....) {
//Suppose when some button click happens for sorting
Collections.sort(searchList, <yourCustomComparator>);
notifyDataSetChanged();
}
});
}
private void getList(字符串json){
ArrayList searchList=ParseJsonData.parseSearchresult(json);
CustomSearchList adapter=新的CustomSearchList(此为searchList);
searchlistView.setAdapter(适配器);
mBtn.setOnClickListener(这是新的OnClickListener(){
@凌驾
公共void onClick(..){
//假设某个按钮点击进行排序
集合。排序(搜索列表,);
notifyDataSetChanged();
}
});
}
最好将searchList设置为成员变量而不是本地变量。而不是为字段创建一个带有字符串数组的数据对象。为JsonArray的每个元素创建一个对象。
为JsonArray中的对象创建列表。
在自定义适配器中传递该列表。
将listview适配器设置为自定义适配器
现在,只要您想按任何字段对数据进行排序,就可以使用Java collections comparator,您可以编写自己的自定义comparator
之后,只需在适配器上调用notifyDataSetChanged()
编辑-
您可以创建模型搜索项,如下所示
class SearchItem {
private String voterId;
private String boothNameId;
private String searchName;
private String houseNumber;
private String gender;
public void setVoterId(String voterId) {
this.voterId = voterId;
}
public String getVoterId() {
return voterId;
}
......................(Add as many fields you want and their corresponding getter and setter methods)
}
您的Json解析类可以这样修改(您必须填充SearchItem类的所有字段)
class ParseJsonData {
public static ArrayList<SearchList> parseSearchresult(String jsonResponse) {
try {
JSONObject searchData = new JSONObject(jsonResponse);
JSONArray searchArray=searchData.getJSONArray(SEARCH_VOTER_ARRAY);
ArrayList<SearchItem> searchList = new ArrayList<SearchItem>();
for(int index = 0; index < searchArray.length(); index++) {
JSONObject searchItem = searchArray.get(index);
SearchItem item = new SearchItem();
item.setVoterId(searchItem.optString(KEY_VOTER_ID, null);
item.setBoothNameId(searchItem.optString(KEY_BOOTHNAME_ID, null);
item.setSearchName(searchItem.optString(KEY_SEARCH_NAME, null);
item.setHouseNumber(searchItem.optString(KEY_SEARCH_HOUSE_NUMBER, null);
item.setGender(searchItem.optString(KEY_SEARCH_GENDER, null);
searchList.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
return searchList;
}
}
类ParseJsonData{
公共静态ArrayList parseSearchresult(字符串jsonResponse){
试一试{
JSONObject searchData=新的JSONObject(jsonResponse);
JSONArray searchArray=searchData.getJSONArray(SEARCH\u VOTER\u ARRAY);
ArrayList searchList=新建ArrayList();
对于(int index=0;index
现在,您可以创建自己的自定义适配器,如下所示
class CustomSearchList extends BaseAdapter {
ArrayList<SearchItem> mSearchList;
Context mContext;
public CustomSearchList(Context context, ArrayList<SearchItem> searchList) {
mContext = context;
mSearchList = searchList;
}
..........................(Implement methods for BaseAdapter or other methods you want to write)
}
class CustomSearchList扩展了BaseAdapter{
ArrayList-mSearchList;
语境;
公共Cu