使用复选框过滤数据,我该怎么办?

时间:2013-07-15 06:38:20

标签: java android checkbox filter

我正在尝试过滤从Web服务检索的数据,并且我正在尝试根据用户选中的类别复选框对检索到的数据进行一些过滤。以下是我的MainActivity代码示例:

    package com.example.fambond;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends Activity {

    private class CheckBoxListener implements OnClickListener {

        public void onClick(View aV) {
            String newLine = "\n";
            String boxesChecked = "";
            String boxInput = "";
            if (a.isChecked()) {
                boxesChecked = boxesChecked + a.getText() + newLine;
                boxInput = boxInput + "education";
            }
            if (b.isChecked()) {
                boxesChecked = boxesChecked + b.getText() + newLine;
                boxInput =  boxInput + "," + "festivals_parades";
            }
            if (c.isChecked()) {
                boxesChecked = boxesChecked + c.getText() + newLine;
                boxInput = boxInput + "," + "sports";
            }
            if (d.isChecked()) {
                boxesChecked = boxesChecked + d.getText() + newLine;
                boxInput =  boxInput + "," + "attractions";
            }
            if (e.isChecked()) {
                boxesChecked = boxesChecked + e.getText() + newLine;
                boxInput =  boxInput + "," +"performing_arts";
            }
            if (f.isChecked()) {
                boxesChecked = boxesChecked + f.getText() + newLine;
                boxInput = boxInput + "," + "community" ;
            }
            if (boxesChecked.length() == 0) {
                boxesChecked = "No CheckBoxes Checked";         
            }
            Toast.makeText(MainActivity.this, boxInput, Toast.LENGTH_SHORT)
                    .show();
        }

    }

    private CheckBox a;
    private CheckBox b;
    private CheckBox c;
    private CheckBox d;
    private CheckBox e;
    private CheckBox f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setWidgetReference();
        setOnClickListeners();
    }

    private void setWidgetReference() {
        // TODO Auto-generated method stub
        a = (CheckBox) findViewById(R.id.checkBox1);
        b = (CheckBox) findViewById(R.id.checkBox2);
        c = (CheckBox) findViewById(R.id.checkBox3);
        d = (CheckBox) findViewById(R.id.checkBox4);
        e = (CheckBox) findViewById(R.id.checkBox5);
        f = (CheckBox) findViewById(R.id.checkBox6);
    }

    private void setOnClickListeners() {
        // TODO Auto-generated method stub
        CheckBoxListener aListener = new CheckBoxListener();
        a.setOnClickListener(aListener);
        b.setOnClickListener(aListener);
        c.setOnClickListener(aListener);
        d.setOnClickListener(aListener);
        e.setOnClickListener(aListener);
        f.setOnClickListener(aListener);
    }

}

这是我从网络服务获得的类别链接:http://api.evdb.com/rest/categories/list?app_key=rDkKF6nSx6LjWTDR

0 个答案:

没有答案