数据库查询返回所有值,而不是匹配输入的列详细信息

时间:2014-05-26 14:02:43

标签: java android sql database sqlite

我正在开发一个Android应用程序,用户可以通过填写多字段形式的任何列来搜索他的详细信息,无需填写所有字段,只需输入任何一列。但问题是不是匹配输入的字段列它显示整个database.i尝试填充1字段,2字段无论我填写甚至只是单击提交而不输入任何东西它返回整个数据库!我哪里错了?

Search.java

public class Search extends Activity {

SqlHandler sqlHandler;
EditText txtname, txt_relative_type, txt_father, txt_id, txt_part, txt_sl,
        txt_age, txt_house, txt_poling, txt_section, txt_ac_name,
        txt_ac_no;
ImageButton reset_btn, submit_btn;
RadioButton RdioBtn_male,RdioBtn_female;
ListView lvCustomList;
String intentname,intentacno,intentpartno,intentpoll,intentsl,intenthouse,intentAc_name,intentRelative,intentfather,intentage,intentsection;
String name1,acno,part,poll,sl,house,Ac_name,Relative,father,age,id,section,intentid;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    txtname = (EditText) findViewById(R.id.editText);
    txt_relative_type = (EditText) findViewById(R.id.editText3);
    txt_father = (EditText) findViewById(R.id.editText1);
    txt_id = (EditText) findViewById(R.id.editText4);
    txt_part = (EditText) findViewById(R.id.editText5);
    txt_sl = (EditText) findViewById(R.id.editText6);
    txt_age = (EditText) findViewById(R.id.editText7);
    txt_house = (EditText) findViewById(R.id.editText8);
    txt_poling = (EditText) findViewById(R.id.editText9);
    txt_section = (EditText) findViewById(R.id.editText10);
    txt_ac_name = (EditText) findViewById(R.id.editText11);
    txt_ac_no = (EditText) findViewById(R.id.editText12);
    reset_btn = (ImageButton) findViewById(R.id.imageButton1);
    submit_btn = (ImageButton) findViewById(R.id.imageButton2);



    submit_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            name1 =  txtname.getText().toString();
            acno =  txt_ac_no.getText().toString();
            part =  txt_part.getText().toString();
            poll =  txt_poling.getText().toString();
            sl =  txt_sl.getText().toString();
            house =  txt_house.getText().toString();
            Ac_name =  txt_ac_name.getText().toString();
            Relative=  txt_relative_type.getText().toString();
            father =  txt_father.getText().toString();
            age =  txt_age.getText().toString();
            id =  txt_id.getText().toString();
            section =  txt_section.getText().toString();
            Intent i = new Intent (Search.this,Listdisplay.class);
            i.putExtra(intentname, name1);
            i.putExtra(intentacno, acno);
            i.putExtra(intentpartno, part);
            i.putExtra(intentpoll, poll);
            i.putExtra(intentsl, sl);
            i.putExtra(intentAc_name, Ac_name);
            i.putExtra(intentRelative, Relative);
            i.putExtra(intentfather, father );
            i.putExtra(intentage, age);
            i.putExtra(intentid, id);
            i.putExtra(intentsection, section);
            i.putExtra(intenthouse, house);
            startActivity(i);

        }
    });

    reset_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            txtname.setText("");
            txt_relative_type.setText("");
            txt_father.setText("");
            txt_id.setText("");
            txt_part.setText("");
            txt_sl.setText("");
            txt_age.setText("");
            txt_house.setText("");
            txt_poling.setText("");
            txt_section.setText("");
            txt_ac_name.setText("");
            txt_ac_no.setText("");
        }
    });
    }}

Listdisplay.java

  public class Listdisplay extends Activity {

    ListView lvCustomList;
    String intentname,intentacno,intentpartno,intentpoll,intentsl,intentAc_name,intentRelative,intentfather,intentage,intentid,intentsection,intenthouse;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchlayout);
         Intent myIntent = getIntent();
         String name_search = myIntent.getStringExtra(intentname);
         String acno_search = myIntent.getStringExtra(intentacno);
         String part_search = myIntent.getStringExtra(intentpartno);
         String address_search = myIntent.getStringExtra(intentpoll);
         String sl_search = myIntent.getStringExtra(intentsl);
         String area_search = myIntent.getStringExtra(intentAc_name);
         String guardian_search = myIntent.getStringExtra(intentRelative);
         String father_search = myIntent.getStringExtra(intentfather);
         String age_search1 = myIntent.getStringExtra(intentage);
         String idcard_search = myIntent.getStringExtra(intentid);
         String section_search = myIntent.getStringExtra(intentsection);
         String house_search = myIntent.getStringExtra(intenthouse);
         lvCustomList = (ListView) findViewById(R.id.listView1);
         showList(name_search,acno_search,part_search,address_search,sl_search,area_search,guardian_search,father_search,age_search1,idcard_search,section_search,house_search);
    }

    private void showList(String name_search,String acno_search,String part_search, String address_search,String sl_search,String area_search,String guardian_search,String father_search,String age_search1,String idcard_search,String section_search,String house_search ) {

        SqlHandler sql = new SqlHandler(this);
        ArrayList<contactlistitems> contactlist = new ArrayList<contactlistitems>();
        contactlist.clear();

        String query="SELECT  * FROM VoterSearch WHERE name LIKE (case when '"+name_search+"'!='' then '%"+name_search+"%' else name end) and PollAddress LIKE (case when '"+address_search+"'!='' then '%"+address_search+"%' else PollAddress end) and Relation LIKE (case when '"+guardian_search+"'!='' then '%"+guardian_search+"%' else Relation end) and FatherName LIKE (case when '"+father_search+"'!='' then '%"+father_search+"%' else FatherName end) and AcName LIKE (case when '"+area_search+"'!='' then '%"+area_search+"%' else AcName end)"
        +" and Section LIKE (case when '"+section_search+"'!='' then '%"+section_search+"%' else Section end)"+" and IdCard LIKE (case when '"+idcard_search+"'!='' then '%"+idcard_search+"%' else IdCard end)"+" and SlNo = (case when '"+sl_search+"'!='' then '"+sl_search+"' else SlNo end) and AcNo = (case when '"+acno_search+"'!='' then '"+acno_search+"' else AcNo end) and PartNo = (case when '"+part_search+"'!='' then '"+part_search+"' else PartNo end)"
        +" and HouseNo LIKE (case when '"+house_search+"'!='' then '%"+name_search+"%' else HouseNo end)"+" and Age BETWEEN (case when '"+age_search1+"'!='' then '"+age_search1+"' else Age end)"+" AND (case when '"+age_search1+"'!='' then '"+age_search1+"' else Age end)";

               Cursor c1 = sql.selectQuery(query);
               Log.i("Error","error");

               if(c1 != null && c1.getCount() != 0)
               {
                   Log.i("Error","error");
                   if(c1.moveToFirst()) {
                       do {
                           contactlistitems contactlistitems = new contactlistitems();
                           contactlistitems.setname(c1.getString(c1.getColumnIndex("name")));
                           contactlistitems.setrelative_type(c1.getString(c1.getColumnIndex("Relation")));
                           contactlistitems.setfather(c1.getString(c1.getColumnIndex("FatherName")));
                           contactlistitems.setid(c1.getString(c1.getColumnIndex("IdCard")));
                           contactlistitems.sethouse(c1.getString(c1.getColumnIndex("HouseNo")));
                           contactlistitems.setsl(c1.getString(c1.getColumnIndex("SlNo")));
                           contactlistitems.setage(c1.getString(c1.getColumnIndex("Age")));
                           contactlistitems.setpoling(c1.getString(c1.getColumnIndex("PollAddress")));
                           contactlistitems.setsection(c1.getString(c1.getColumnIndex("Section")));
                           contactlistitems.setac_name(c1.getString(c1.getColumnIndex("AcName")));
                           contactlistitems.setac_no(c1.getString(c1.getColumnIndex("AcNo")));
                           contactlistitems.setpart(c1.getString(c1.getColumnIndex("PartNo")));
                           contactlistitems.setSex(c1.getString(c1.getColumnIndex("Sex")));
                           contactlist.add(contactlistitems);

                       } while (c1.moveToNext());
                   }
               }
               c1.close();


               ContactListAdapter contactListAdapter = new ContactListAdapter(Listdisplay.this, contactlist);
               lvCustomList.setAdapter(contactListAdapter); 

    }
}

ContactListAdapter.java

    public class ContactListAdapter extends BaseAdapter {

    Context context;
    ArrayList<contactlistitems> contactlist;
    LayoutInflater mInflater;

    public ContactListAdapter(Context context, ArrayList<contactlistitems> list) {
         mInflater = LayoutInflater.from(context);
        this.context = context;
        contactlist = list;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return contactlist.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return contactlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list, null);
                holder = new ViewHolder();
                holder.name = (TextView) convertView.findViewById(R.id.textView1);
                holder.house = (TextView) convertView.findViewById(R.id.textView2);
                holder.father = (TextView) convertView.findViewById(R.id.textView3);
                holder.sex = (TextView) convertView.findViewById(R.id.textView4);
                holder.part = (TextView) convertView.findViewById(R.id.textView5);


                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.name.setText(contactlist.get(position).getname());
            holder.house.setText(contactlist.get(position).gethouse());
            holder.father.setText(contactlist.get(position).getfather());
            holder.sex.setText(contactlist.get(position).getSex());
            holder.part.setText(contactlist.get(position).getpart());

            return convertView;
        }



   static  class ViewHolder
     {
        TextView name,house,father,sex,part;
     }

}

我相信查询是完全正确的!不知道它出错的地方,它是通过意图导致问题而不确定将值从类搜索传递到listadisplay。请帮助解决这个问题

1 个答案:

答案 0 :(得分:0)

当我尝试使用实时数据库时,查询工作正常。问题是返回整个数据库而不是匹配,因此查询可能返回null,现在必须查找代码。当我测试通过getIntent()获得的值时,它没有收到值,因此问题是有意图的,没有发送值。错误只是在intent和getintent中包含双引号

在Search.java双引号参数

i.putExtra("intentname", name1);

在ListDisplay.java

String name_search = myIntent.getStringExtra("intentname");

它有效