我最近开始开发几个Android应用程序,并且在本周开始开发的应用程序中,我需要从Web服务中检索JSONObject并根据用户参数对其进行过滤,为此,我创建了一个具有可扩展列表视图的活动,包含复选框。单击时,它将复选框内容保存到数组中并进行相应的过滤。而且过滤效果很好。但是,复选框有些奇怪,当我从一个抽屉的底部检查一个时,从同一抽屉的顶部检查一个,从其他抽屉中又检查了几个。但是,它不会将它们注册为点击,并且不会将过滤器参数数组中添加,而是仅添加我们最初检查的内容。据我所知,自动检查的内容之间没有任何模式。有列表适配器的代码。 CustomExpandableListAdapter.java
package com.divasoft.retivaraporlar;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
static public HashMap<String, ArrayList<String>> filterData = new HashMap<>();
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
filterData.put("Cari Açıklama", new ArrayList<String>());
filterData.put("Döviz", new ArrayList<String>());
filterData.put("Borç", new ArrayList<String>());
filterData.put("Alacak", new ArrayList<String>());
filterData.put("Bakiye", new ArrayList<String>());
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public View getChildView(final int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
CheckBox expandedListViewCheckBox = (CheckBox) convertView
.findViewById(R.id.expandedListItem);
expandedListViewCheckBox.setText(expandedListText);
expandedListViewCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked) {
if(expandableListTitle.get(listPosition).equals("Bakiye")){
switch(buttonView.getText().toString()) {
case "Borçlu":
filterData.get(expandableListTitle.get(listPosition)).add("DEBT");
break;
case "Alacak":
filterData.get(expandableListTitle.get(listPosition)).add("OWE");
break;
default:
filterData.get(expandableListTitle.get(listPosition)).add("ZERO");
break;
}
}else {
filterData.get(expandableListTitle.get(listPosition)).add(buttonView.getText().toString());
}
}else{
filterData.get(expandableListTitle.get(listPosition)).remove(buttonView.getText().toString());
}
}
}
);
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
@Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
并且有该活动的代码:
filter_popup.java
package com.divasoft.retivaraporlar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class filter_popup extends Activity {
public View filter;
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;
public String TAG = "Filter";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter_popup);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
expandableListDetail = getData();
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
filter = findViewById(R.id.filter);
filter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
Intent myIntent = new Intent(filter_popup.this, cariBakiye.class);
myIntent.putExtra("filters", CustomExpandableListAdapter.filterData); //Optional parameters
filter_popup.this.startActivity(myIntent);
finish();
cariBakiye.getInstance().finish();
}
});
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
expandableListTitle.get(groupPosition) + " List Expanded.",
Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
expandableListTitle.get(groupPosition) + " List Collapsed.",
Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
getApplicationContext(),
expandableListTitle.get(groupPosition)
+ " -> "
+ expandableListDetail.get(
expandableListTitle.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT
).show();
return false;
}
});
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*0.8), (int)(height*0.65));
}
public static HashMap<String, List<String>> getData() {
HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
try {
List<String> cariAciklama = new ArrayList<>();
for (int i = 0; i<((JSONArray)cariBakiye.getCariData().get("DATA")).length(); i++){
if(cariAciklama.indexOf(((JSONObject)((JSONArray)cariBakiye.getCariData().get("DATA")).get(i)).get("current_description").toString())==-1) {
cariAciklama.add(((JSONObject)((JSONArray)cariBakiye.getCariData().get("DATA")).get(i)).get("current_description").toString());
}
}
List<String> doviz = new ArrayList<>();
for (int i = 0; i<((JSONArray)cariBakiye.getCariData().get("DATA")).length(); i++){
if(doviz.indexOf(((JSONObject)((JSONArray)cariBakiye.getCariData().get("DATA")).get(i)).get("currency_code").toString())==-1) {
doviz.add(((JSONObject) ((JSONArray) cariBakiye.getCariData().get("DATA")).get(i)).get("currency_code").toString());
}
}
List<String> alacak = new ArrayList<>();
for (int i = 0; i<((JSONArray)cariBakiye.getCariData().get("DATA")).length(); i++){
if(alacak.indexOf(((JSONObject)((JSONArray)cariBakiye.getCariData().get("DATA")).get(i)).get("total_owe").toString())==-1) {
alacak.add(((JSONObject) ((JSONArray) cariBakiye.getCariData().get("DATA")).get(i)).get("total_owe").toString());
}
}
List<String> borc = new ArrayList<>();
for (int i = 0; i<((JSONArray)cariBakiye.getCariData().get("DATA")).length(); i++){
if(borc.indexOf(((JSONObject)((JSONArray)cariBakiye.getCariData().get("DATA")).get(i)).get("total_debt").toString())==-1) {
borc.add(((JSONObject) ((JSONArray) cariBakiye.getCariData().get("DATA")).get(i)).get("total_debt").toString());
}
}
List<String> bakiye = new ArrayList<>();
for (int i = 0; i<((JSONArray)cariBakiye.getCariData().get("DATA")).length(); i++){
switch(((JSONObject) ((JSONArray) cariBakiye.getCariData().get("DATA")).get(i)).get("balance_type").toString()) {
case "DEBT":
if(bakiye.indexOf("Borçlu")==-1){
bakiye.add("Borçlu");
}
break;
case "OWE":
if(bakiye.indexOf("Alacak")==-1){
bakiye.add("Alacak");
}
break;
default:
if(bakiye.indexOf("Sıfır")==-1){
bakiye.add("Sıfır");
}
break;
}
}
expandableListDetail.put("Cari Açıklama", cariAciklama);
expandableListDetail.put("Bakiye", bakiye);
expandableListDetail.put("Döviz", doviz);
expandableListDetail.put("Borç", borc);
expandableListDetail.put("Alacak", alacak);
}catch(JSONException e){Log.e("FilterPopup","[ERROR]: "+e.toString());}
return expandableListDetail;
}
}