mardi 2 juin 2015

force close errore in search in database

I have a database and i whant to write class to search in this i have xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<EditText
    android:id="@+id/search_word"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <requestFocus />
</EditText>



<TextView
    android:id="@+id/search_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/search_word"
    android:layout_marginTop="19dp"
    android:gravity="center"
    android:text="TextView" />



<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/search_status" >

</ListView>

</RelativeLayout>

and i whant when user type in EditText can see result in list and in TextView see number of result i write my class

public class search extends ListActivity {

   private database db;
   private String[] Name;
   private String[] cPage;
   private String[] Page;
   private EditText word;
   private TextView status;
   private TextView name;
   protected void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    db = new database(this);

    word=(EditText) findViewById(R.id.search_word);
    status=(TextView) findViewById(R.id.search_status);

    refresh(word.getText().toString(), "Matn");


    word.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

            refresh(word.getText().toString(), "Matn");

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
     });

}

  @Override
   protected void onListItemClick(ListView l, View v, int position, long id) {

      Intent i = new Intent(search.this,main_matn.class);
      i.putExtra("name",Name[position]);
      i.putExtra("page", cPage[position]);

      startActivity(i);

   }



   class AA extends ArrayAdapter<String>{

      public AA(){  
        super(search.this,R.layout.row_main,Name);
    }


      public View getView(final int position, View convertView, ViewGroup parent){


        LayoutInflater in=getLayoutInflater();
        View row =in.inflate(R.layout.row_main, parent, false);
         name =(TextView) row.findViewById(R.id.row_main_name);

        name.setText(Name[position]);


        return (row);
      }
  }


   private void refresh(String word,String field){

      db.open();

      int s = db.count_search(word, field);

      if(word.equals("")){

        s=0;
        status.setText("لطفا کلمه مورد جستجو را وارد کنید");

      }

      status.setText("تعداد"+s+" یافت شد");

      Name= new String[s];
      cPage= new String[s];
      Page= new String[s];

      for(int i=0;i<s;i++){

        Name[i]=db.search(i, 2, word, field);
        cPage[i]=db.page_count("text", Name[i])+"";
        Page[i]=db.search(i, 4, word, field);

        }
        setListAdapter(new AA());


   db.close();
        }
}

in database class i write function 

public Integer count_search(String word,String field){
    Cursor cu=mydb.rawQuery("select * from text where "+field+" Like '%"+word+"%'", null);
     int s=cu.getCount();
     return s;

  }

  public String search(int row,int col,String word,String field){

    Cursor cu=mydb.rawQuery("select * from text where "+field+" Like '%"+word+"%'",  null);
     cu.moveToPosition(row);
     String s=cu.getString(col);
        return s;
    }
public Integer page_count(String table,String name){

    Cursor cu=mydb.rawQuery("select * from "+table+" where Header='"+name+"'",       null);
        int s=cu.getCount();
        return s;
    }

and when i run this it force close and in logcat say problem in this

        setContentView(R.layout.search);

Aucun commentaire:

Enregistrer un commentaire