我正在对JSONArray进行排序并在自定义列表视图中显示它们,但在排序后,数据在自定义列表视图中没有更改。
这是我的fab按钮代码,用于选择要执行的排序:
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按钮中选择后点击我排序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();
}
}
我使用`adapter.notifyDataSetChanged();但我的自定义列表不会影响排序。 请建议我做什么。
答案 0 :(得分:1)
在将数据设置为Listview之前,您应该使用java中的比较器类对数据进行排序。
使用所有变量(name,booth_id等)创建一个Model类,并将每个对象存储到ArrayList中。
SQL Server version Supported compatibility Recommended compatibility
level values level for ConfigMgr
SQL Server 2016 130, 120, 110, 100 130
SQL Server 2014 120, 110, 100 110
例如
Collections.sort(ArrayListObject,new NameComparator());
答案 1 :(得分:1)
而不是为其字段创建一个包含字符串数组的数据对象。为JsonArray的每个元素创建一个对象。 从JsonArray创建对象列表。 将该列表传递到自定义适配器中。 将listview适配器设置为自定义适配器。
现在,只要您想按任何字段对数据进行排序。只需使用Java集合比较器,或者您可以编写自己的自定义比较器。
之后只需在适配器上调用notifyDataSetChanged()。
编辑 -
您可以创建您的模型SearchItem,如下所示
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;
}
}
现在您可以按照以下方式创建自己的自定义适配器
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)
}
此处searchList作为参考传递,因此该列表中的任何更新以及随后对适配器的通知调用都将反映更改。
您的listview类可以像这样修改。 parseSearchList()方法返回所有搜索列表的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();
}
});
}
如果您可以将searchList作为成员变量而不是本地变量,那会更好。
答案 2 :(得分:1)
在我的情况下,我从JSONParser类获得homeList(模型'Home'的ArrayList),所以befor setAdapter
Collections.sort(homeList, new Comparator<Home>() {
public int compare(Home emp1, Home emp2) {
int price1 = emp1.getPrice();
int price2 = emp2.getPrice();
return Integer.compare(price1, price2);
}
});
Adapter adapter = new Adapter(getActivity(),homeList);
listView.setadapter(adapter);
所以这将首先对整个arraylist进行排序,然后我们需要setAdapter。
尝试这个我认为这会对你有帮助。
答案 3 :(得分:1)
是的,我认为你必须使用模型类。这是onPostExecute的示例。
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pd.dismiss();
try {
JSONObject object = new JSONObject(s.toString());
YourModelNameList = new ArrayList<>();
if (object.getString("status").equalsIgnoreCase("true")) {
JSONArray array = object.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject jsonProduct = array.getJSONObject(i);
YourModelName home = new Home();
String special = jsonProduct.getString("special_price");
home.setId(jsonProduct.getString("product_id"));
home.setName(jsonProduct.getString("product_name"));
home.setPrice(jsonProduct.getString("price"));
YourModelArrayList.add(home);
}
}
Collections.sort(YourModelArrayList, new Comparator<YourModelName>() {
public int compare(YourModelName emp1, YourModelName emp2) {
int price1 = emp1.getPrice();
int price2 = emp2.getPrice();
return Integer.compare(price1, price2);
}
});
adapter = new YourModelArrayListAdapter(getActivity(), YourModelArrayList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
试试这个,如果这对你有所帮助。谢谢。
答案 4 :(得分:1)
我对名为ArrayList&gt;的arraylist进行了排序。 addressaray; 使用
将响应数据添加到arraylist{{1}}