2016-08-19 5 views
-6

マイコードは検索機能が動作しません。私のコードは正しく動作しません。私は私のコードで私の間違いが分かっていません....私のコードは下のリンクを見せて.....!検索機能を追加してください-------------------------..... >>>>>ここに画像の説明を入力してください。アンドロイドの別のarraylistに名前のarraylistを追加するには?

package com.example.jatinbodara.contactdemo0002; 

    import android.content.Context; 
    import android.content.Intent; 
    import android.database.Cursor; 
    import android.database.SQLException; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.text.Editable; 
    import android.text.TextWatcher; 
    import android.view.LayoutInflater; 
    import android.view.Menu; 
    import android.view.MenuInflater; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.widget.AdapterView; 
    import android.widget.EditText; 
    import android.widget.ListView; 
    import android.widget.Toast; 

    import java.util.ArrayList; 
    import java.util.Collections; 

    public class MainActivity extends AppCompatActivity { 

     SQLiteHelper SQLITEHELPER; 
     SQLiteDatabase SQLITEDATABASE; 
     Cursor cursor; 
     SQLiteListAdapter ListAdapter; 
     EditText searchBox; 
     ArrayList<String> ID_ArrayList = new ArrayList<String>(); 
     ArrayList<String> NAME_ArrayList = new ArrayList<String>(); 
     ArrayList<String> PHONE_NUMBER_ArrayList = new ArrayList<String>(); 
     ArrayList<String> searchResults ; 
     ListView LISTVIEW; 
     String GetSQliteQuery; 
     private String selectedItem; 
     //ArrayList thats going to hold the search results 
     LayoutInflater inflater; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      LISTVIEW = (ListView) findViewById(R.id.listView1); 
      searchBox = (EditText) findViewById(R.id.inputSearch); 
      SQLITEHELPER = new SQLiteHelper(this); 
      inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 

     @Override 
     protected void onResume() { 
      ShowSQLiteDBdata(); 
      super.onResume(); 
     } 

     private void ShowSQLiteDBdata() { 

      searchResults = new ArrayList<String>(NAME_ArrayList); 
      SQLITEDATABASE = SQLITEHELPER.getWritableDatabase(); 
       try{ 
        cursor = SQLITEDATABASE.rawQuery("SELECT * FROM demoTable ORDER BY name ASC", null); 
       } catch (SQLException e){ 
        e.printStackTrace(); 
       } 

       ID_ArrayList.clear(); 
       NAME_ArrayList.clear(); 
       PHONE_NUMBER_ArrayList.clear(); 

      if (cursor.moveToFirst()) { 
       do { 
        ID_ArrayList.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.KEY_ID))); 
        NAME_ArrayList.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.KEY_Name))); 
        PHONE_NUMBER_ArrayList.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.KEY_PhoneNumber))); 
       } while (cursor.moveToNext()); 
      } 

      ListAdapter = new SQLiteListAdapter(MainActivity.this, 
        ID_ArrayList, 
        NAME_ArrayList, 
        PHONE_NUMBER_ArrayList 
      ); 

      // Adapter set....... 
      LISTVIEW.setAdapter(ListAdapter); 

      Collections.sort(NAME_ArrayList, String.CASE_INSENSITIVE_ORDER); 

      LISTVIEW.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
        Intent intent = new Intent(Intent.ACTION_DIAL); 
        intent.setData(Uri.parse("tel:" + PHONE_NUMBER_ArrayList.get(+position))); 
        startActivity(intent); 
       } 
      }); 

      searchBox.addTextChangedListener(new TextWatcher() { 
       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
       } 

       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
        String searchString = searchBox.getText().toString(); 
        String getStringName = String.valueOf(NAME_ArrayList); 
        int textLength=searchString.length(); 

        searchResults.clear(); 

        for(int i=0; i<NAME_ArrayList.size();i++){ 
         String playerName=NAME_ArrayList.get(i).toString(); 
         if(textLength<=playerName.length()){ 
          if(searchString.equalsIgnoreCase(playerName.substring(0,textLength))) { 
           searchResults.add(NAME_ArrayList.get(i)); 
           Toast.makeText(MainActivity.this, "Good....!", Toast.LENGTH_SHORT).show(); 
          } 
         } 
        } 
        ListAdapter.notifyDataSetChanged(); 
       } 

       @Override 
       public void afterTextChanged(Editable s) { 
       } 
      }); 

      LISTVIEW.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
       @Override 
       public boolean onItemLongClick(final AdapterView<?> parent, View view, int position, long id) { 

        String DeleteQuery = "DELETE FROM demoTable WHERE id=" + ID_ArrayList.get(position) + ";"; 
        SQLITEDATABASE.execSQL(DeleteQuery); 
       // ListAdapter.notifyDataSetChanged(); 
       // cursor = SQLITEDATABASE.rawQuery("SELECT * FROM demoTable", null); 
        Toast.makeText(MainActivity.this, "Your Deleted Item Detail... \n \n Name : " + NAME_ArrayList.get(position) + "\n" + " \n Mobile No : " + PHONE_NUMBER_ArrayList.get(position), Toast.LENGTH_SHORT).show(); 


       /* AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this); 
        alert.setTitle("Delete"); 
        alert.setMessage("Do you want Delete this item ?"); 
        alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int position) { 

          try { 
           String DeleteQuery = "DELETE FROM demoTable WHERE id=" + ID_ArrayList.get(position) + ";"; 
           SQLITEDATABASE.execSQL(DeleteQuery); 
           cursor = SQLITEDATABASE.rawQuery("SELECT * FROM demoTable", null); 
           Toast.makeText(MainActivity.this, "Long Click Click ID No..." + ID_ArrayList.get(position), Toast.LENGTH_SHORT).show(); 
          }catch(ArrayIndexOutOfBoundsException e){ 
           Toast.makeText(MainActivity.this, "Exception detect" , Toast.LENGTH_SHORT).show(); 
           e.printStackTrace(); 
          } 
         } 
        }); 
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialogInterface, int i) { 
          dialogInterface.dismiss(); 
         } 
        }); 
        alert.show(); 
       */ 
        return true; 
       } 
      }); 

      cursor.close(); 

     } // onCreat(); 


     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.menu, menu); 
      return true; 

     } 

     public boolean onOptionsItemSelected(MenuItem item) { 

      int id = item.getItemId(); 
      switch (id) { 
       case R.id.menu_plus: 
        Intent intent = new Intent(MainActivity.this, AddDataActivity.class); 
        startActivity(intent); 
        return true; 
      } 
      return super.onOptionsItemSelected(item); 
     } 


    } // Main Method..... 
+1

コードのスクリーンショットではなく、実際のコードを投稿してください。 – Norbert

+3

あなたは[質問する方法](http://stackoverflow.com/help/how-to-ask)を読む必要があります** DESPERATLY ** – Takarii

答えて

0

equalsIgnoreCaseではなく、containsを試してみてください。それぞれの文字列を大文字または小文字に変換してください。

+0

はあなたに何を伝えるのか理解できません。私のコードを訂正してください.....! –

+0

if(searchString.equalsIgnoreCase(playerName.substring(0、textLength))){ searchResults.add(NAME_ArrayList.get(i)); Toast.makeText(MainActivity.this、 "Good ....!"、Toast.LENGTH_SHORT).show(); } ..ここでは、equalsIgnoreCase(playerName.substring(0、textLength))の代わりにcontainsを使用します。 –

関連する問題