【问题标题】:How to add name one arraylist to another arraylist in android?如何在android中将名称一个arraylist添加到另一个arraylist?
【发布时间】:2016-08-19 08:35:16
【问题描述】:

我的代码是搜索功能不起作用。我的代码不对。我不知道我的代码中的错误是什么......我的代码显示在下面的链接......!请帮我添加搜索功能--------------------------------------------------------->>>>> 在此处输入图片描述。

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.....

【问题讨论】:

标签: android arraylist get


【解决方案1】:

为什么不尝试使用包含,而不是equalsIgnoreCase,将每个字符串转换为小写或大写。

【讨论】:

  • if(searchString.equalsIgnoreCase(playerName.substring(0,textLength))) { searchResults.add(NAME_ArrayList.get(i)); Toast.makeText(MainActivity.this, "好....!", Toast.LENGTH_SHORT).show(); }..这里使用包含,而不是equalsIgnoreCase(playerName.substring(0,textLength))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多