从一个活动开始,我打开另一个活动并过滤自定义列表视图的结果。
这是第一个活动:
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func goToSecondVC(_ sender: UIButton) {
// Go from first VC to second VC
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let showSecondVC = storyBoard.instantiateViewController(withIdentifier: "goSecondVC") as! SecondViewController
self.present(showSecondVC, animated:true, completion:nil)
}
}
这是过滤器活动:
public class PersonaFragment extends Fragment {
private PersonaAdapter personaAdapter;
private ListView personeListView;
private ArrayList<Persona> personeList = new ArrayList<Persona>();
private ArrayList<String> listIdClas = new ArrayList<String>();
private final static int INSERT_REQUEST = 1;
private final static int FILTER_REQUEST = 2;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.persona_fragment, null);
setHasOptionsMenu(true);
personaAdapter = new PersonaAdapter(getActivity(), personeList, R.layout.persona_row_layout);
personeListView = view.findViewById(R.id.lvPersone);
personeListView.setAdapter(personaAdapter);
blsRequest request = new blsRequest(getActivity(), blsVolleyResponse());
request.add(blsUrl.listaPersone(), "00");
return view;
}
private void startFilterActivity() {
Intent intent = new Intent(getActivity(), PersonaFilterActivity.class);
intent.putStringArrayListExtra("listIdClas", this.listIdClas);
startActivityForResult(intent, FILTER_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FILTER_REQUEST) {
if (resultCode == RESULT_OK) {
this.listIdClas = data.getStringArrayListExtra("listIdClas");
personaAdapter.filterManual(this.listIdClas);
}
}
}
private blsVolleyResponse blsVolleyResponse() {
return new blsVolleyResponse() {
@Override
public void onResponse(String result, String tag) {
switch (tag) {
case "00":
personeList = blsJSON.ParseList(result, tag);
personaAdapter.updateData(personeList);
break;
}
}
@Override
public void onError(VolleyError error, String tag) {
Toast.makeText(getActivity(), "Ops! Qualcosa è andato storto", Toast.LENGTH_SHORT).show();
blsLog.i("blsVolleyResponse.onError: TAG = " + tag + " ERROR = " + error);
}
};
}
}
如您所见,我没有使用:
public class PersonaFilterActivity extends AppCompatActivity {
private ClasseAdapter classeAdapter;
private ListView classiListView;
private ArrayList<Classe> classiList = new ArrayList<Classe>();
private ArrayList<String> listIdClas = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.persona_filter_activity);
Bundle extras = getIntent().getExtras();
if (extras != null) {
listIdClas = extras.getStringArrayList("listIdClas");
}
classiListView = findViewById(R.id.lvClassi);
classeAdapter = new ClasseAdapter(this, classiList);
classiListView.setAdapter(classeAdapter);
classiListView.addOnLayoutChangeListener(onListLoaded);
blsRequest request = new blsRequest(this, blsVolleyResponse());
request.add(blsUrl.listaClassi(), "00");
}
private View.OnLayoutChangeListener onListLoaded = new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
setFilter();
}
};
private void applyFilter() {
fillListId();
Intent returnIntent = new Intent();
returnIntent.putStringArrayListExtra("listIdClas", listIdClas);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
private void fillListId() {
listIdClas.clear();
for (int i = 0; i < classiListView.getChildCount(); i++) {
View child = classiListView.getChildAt(i);
Classe classe = (Classe) classiListView.getItemAtPosition(i);
Switch aSwitch = child.findViewById(R.id.swFlt);
if (aSwitch.isChecked())
listIdClas.add(classe.getId());
}
}
private void ReturnValue(int Result) {
Intent returnIntent = new Intent();
setResult(Result, returnIntent);
finish();
}
private void clearFilter() {
listIdClas.clear();
for (int i = 0; i < classiListView.getChildCount(); i++) {
View child = classiListView.getChildAt(i);
Switch aSwitch = child.findViewById(R.id.swFlt);
aSwitch.setChecked(false);
}
}
private void setFilter() {
for (int i = 0; i < classiListView.getChildCount(); i++) {
for (String idCat : listIdClas) {
View child = classiListView.getChildAt(i);
Switch aSwitch = child.findViewById(R.id.swFlt);
Classe classe = (Classe) classiListView.getItemAtPosition(i);
if (classe.getId().compareTo(idCat) == 0)
aSwitch.setChecked(true);
}
}
}
private blsVolleyResponse blsVolleyResponse() {
return new blsVolleyResponse() {
@Override
public void onResponse(String result, String tag) {
switch (tag) {
case "00":
classiList = blsJSON.ParseList(result, tag);
classeAdapter.updateData(classiList);
break;
}
}
@Override
public void onError(VolleyError error, String tag) {
Toast.makeText(PersonaFilterActivity.this, "Ops! Qualcosa è andato storto", Toast.LENGTH_SHORT).show();
blsLog.i("blsVolleyResponse.onError: TAG = " + tag + " ERROR = " + error);
}
};
}
}
但是
classiListView.getCount()
因为过滤器listview上有很多项目,如果我使用“ getCount()”,则会收到类似以下的错误: “尝试在空对象引用上调用虚拟方法'android.view.View android.view.View.findViewById(int)'”
我没有任何问题可以过滤列表视图的自定义适配器:使用数组列表,存储选择的行ID,并将其应用于第一个活动列表视图。现在,我需要打开第二个活动并为每行设置已选中的Switch对象(如果/未选中),但是当我重新单击过滤器按钮以添加其他过滤器时,过滤器项的列表视图无法正确还原,由于使用了“ getChildCount()”。
希望我很清楚。我该如何执行?