问:
Android actionbar Search with customlitview
I m new to android development.I have custom listview using Arrayadopter my all data is fecth from sqlite database and show in this custom listview   how i can  implement the search functionality on my action bar i tried many other code but its not working here my  please tell me  if any experience developer is  who can help i am very thankful for him in advance please tell me?</p>

```<code>My custom list adopter:

    package bible.swordof.God;

    import android.app.Activity;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    import com.amulyakhare.textdrawable.TextDrawable;
    import com.amulyakhare.textdrawable.util.ColorGenerator;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Locale;

    public class ListViewAdapter extends ArrayAdapter<String> {
        private MainActivity activity;
        private List<String> friendList;
        private List<String> searchList;

        public ListViewAdapter(MainActivity context, int resource, List<String> objects) {
            super(context, resource, objects);
            this.activity = context;
            this.friendList = objects;
            this.searchList = new ArrayList<>();
            this.searchList.addAll(friendList);
        }

        @Override
        public int getCount() {
            return friendList.size();
        }

        @Override
        public String getItem(int position) {

            return friendList.get(position);

        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            // If holder not exist then locate all view from UI file.
            if (convertView == null) {
                // inflate UI from XML file
                convertView = inflater.inflate(R.layout.item_listview, parent, false);
                // get all UI view
                holder = new ViewHolder(convertView);
                // set tag for holder
                convertView.setTag(holder);
            } else {
                // if holder created, get tag from view
                holder = (ViewHolder) convertView.getTag();
            }

            holder.friendName.setText(getItem(position));

            //get first letter of each String item
            String firstLetter = String.valueOf(getItem(position).substring(0,1));

            ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
            // generate random color
            int color = generator.getColor(getItem(position));

            TextDrawable drawable = TextDrawable.builder()
                    .buildRound(firstLetter, color); // radius in px

            holder.imageView.setImageDrawable(drawable);

            return convertView;
        }

        public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            friendList.clear();
            if (charText.length() == 0) {
                friendList.addAll(searchList);
            } else {
                for (String s : searchList) {
                    if (s.toLowerCase(Locale.getDefault()).contains(charText)) {
                        friendList.add(s);
                    }
                }
            }
            notifyDataSetChanged();
        }

        private class ViewHolder {
            private ImageView imageView;
            private TextView friendName;

            public ViewHolder(View v) {
                imageView = (ImageView) v.findViewById(R.id.image_view);
                friendName = (TextView) v.findViewById(R.id.text);
            }
        }

        }

My Mainactivity where i implement search bar:

package bible.swordof.God;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.app.SearchManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    private ListView listView;
    private ArrayList<String> stringArrayList;
    private ArrayAdapter<String> adapter;
    private DatabaseHelper mDBHelper=null;
    private SQLiteDatabase mDb=null;

//life is awesome

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        setData();
        listView =findViewById(R.id.list);
        adapter = new ListViewAdapter(MainActivity.this, R.layout.item_listview, stringArrayList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                 int booknumber=position+1;
                 String  bookname=stringArrayList.get(position);

                //Toast.makeText(MainActivity.this, ""+booknumber, Toast.LENGTH_SHORT).show();

                Intent intent=new Intent(MainActivity.this,Chapters.class);

                intent.putExtra("booknumber",booknumber);

                intent.putExtra("bookname",bookname);

                startActivity(intent);

            }
        });
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.search_voice_btn:
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(intent, 10);
                } else {
                    Toast.makeText(this, "Your Device Don't Support Speech Input", Toast.LENGTH_SHORT).show();
                }

                break;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 10:
                if (resultCode == RESULT_OK && data != null) {

                    ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                    Toast.makeText(this, ""+result.get(0), Toast.LENGTH_SHORT).show();

                }
                break;
        }
    }

    @Override
    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.main,menu);
    MenuItem menuItem=menu.findItem(R.id.action_search);

        SearchView searchView=(SearchView)menuItem.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;

            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (TextUtils.isEmpty(newText)) {
                   adapter.getFilter().filter(newText);
                    listView.clearTextFilter();

                } else {
                    adapter.getFilter().filter(newText);
                }

                return true;

            }
        });

    return  true;
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        Fragment fragment;
        int id = item.getItemId();

        if (id == R.id.nav_camera) {

        } else if (id == R.id.favoruite)
        {
         Intent intent=new Intent(this,Favourite.class);

         startActivity(intent);
        }  else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private void setData() {

        stringArrayList = new ArrayList<>();

        mDBHelper = new DatabaseHelper(this);
        mDb = mDBHelper.getReadableDatabase();

        Cursor cursor = mDb.rawQuery("select * from key_english", new String[]{});

        if(cursor!=null && cursor.getCount() > 0)
        {
            if (cursor.moveToFirst())
            {
                do {
                    stringArrayList.add(cursor.getString(1));
                } while (cursor.moveToNext());
            }
        }
            }

            }

My action search menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_search"
        android:title="@string/search"
        android:icon="@android:drawable/ic_menu_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always" />

    <item
        android:id="@+id/search_voice_btn"
        android:icon="@drawable/ic_mic"
        app:showAsAction="always"
        android:title="Voice Search" />
</menu>
</code>```

答:
For your adapter, you have made a public method called filter which is not a real Filter class. I suggest 2 different methods that may make the posted code work.

Method 1: In the Activity, change:adapter.getFilter().filter(newText); to adapter.filter(newText);.

Method 2: Since your data is a String list and ArrayAdapter is already Filterable, so keep adapter.getFilter().filter(newText); and just make the adapter simple like this:

```<code>public class ListViewAdapter extends ArrayAdapter {
private MainActivity activity;

public ListViewAdapter(MainActivity context, int resource, List<String> objects) {
    super(context, resource, objects);
    this.activity = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    // If holder not exist then locate all view from UI file.
    if (convertView == null) {
        // inflate UI from XML file
        convertView = inflater.inflate(R.layout.item_listview, parent, false);
        // get all UI view
        holder = new ViewHolder(convertView);
        // set tag for holder
        convertView.setTag(holder);
    } else {
        // if holder created, get tag from view
        holder = (ViewHolder) convertView.getTag();
    }

    String item = (String)getItem(position);
    holder.friendName.setText(item);
    //get first letter of each String item
    String firstLetter = item.substring(0, 1);
    ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
    // generate random color
    int color = generator.getColor(item);
    TextDrawable drawable = TextDrawable.builder()
            .buildRound(firstLetter, color); // radius in px
    holder.imageView.setImageDrawable(drawable);
    return convertView;
}

private class ViewHolder {
    private ImageView imageView;
    private TextView friendName;

    public ViewHolder(View v) {
        imageView = (ImageView) v.findViewById(R.id.image_view);
        friendName = (TextView) v.findViewById(R.id.text);
    }
}

}
</code>```

My blog on ListView also contains samples with Filter: http://programandroidlistview.blogspot.com/ Hope that helps!

Body:
I m new to android development.I have custom listview using Arrayadopter my all data is fecth from sqlite database and show in this custom listview   how i can  implement the search functionality on my action bar i tried many other code but its not working here my  please tell me  if any experience developer is  who can help i am very thankful for him in advance please tell me?

My custom list adopter:

    package bible.swordof.God;

    import android.app.Activity;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;

    import com.amulyakhare.textdrawable.TextDrawable;
    import com.amulyakhare.textdrawable.util.ColorGenerator;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Locale;

    public class ListViewAdapter extends ArrayAdapter<String> {
        private MainActivity activity;
        private List<String> friendList;
        private List<String> searchList;

        public ListViewAdapter(MainActivity context, int resource, List<String> objects) {
            super(context, resource, objects);
            this.activity = context;
            this.friendList = objects;
            this.searchList = new ArrayList<>();
            this.searchList.addAll(friendList);
        }

        @Override
        public int getCount() {
            return friendList.size();
        }

        @Override
        public String getItem(int position) {

            return friendList.get(position);

        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            // If holder not exist then locate all view from UI file.
            if (convertView == null) {
                // inflate UI from XML file
                convertView = inflater.inflate(R.layout.item_listview, parent, false);
                // get all UI view
                holder = new ViewHolder(convertView);
                // set tag for holder
                convertView.setTag(holder);
            } else {
                // if holder created, get tag from view
                holder = (ViewHolder) convertView.getTag();
            }

            holder.friendName.setText(getItem(position));

            //get first letter of each String item
            String firstLetter = String.valueOf(getItem(position).substring(0,1));

            ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
            // generate random color
            int color = generator.getColor(getItem(position));

            TextDrawable drawable = TextDrawable.builder()
                    .buildRound(firstLetter, color); // radius in px

            holder.imageView.setImageDrawable(drawable);

            return convertView;
        }

        public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            friendList.clear();
            if (charText.length() == 0) {
                friendList.addAll(searchList);
            } else {
                for (String s : searchList) {
                    if (s.toLowerCase(Locale.getDefault()).contains(charText)) {
                        friendList.add(s);
                    }
                }
            }
            notifyDataSetChanged();
        }

        private class ViewHolder {
            private ImageView imageView;
            private TextView friendName;

            public ViewHolder(View v) {
                imageView = (ImageView) v.findViewById(R.id.image_view);
                friendName = (TextView) v.findViewById(R.id.text);
            }
        }

        }

My Mainactivity where i implement search bar:

package bible.swordof.God;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.app.SearchManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    private ListView listView;
    private ArrayList<String> stringArrayList;
    private ArrayAdapter<String> adapter;
    private DatabaseHelper mDBHelper=null;
    private SQLiteDatabase mDb=null;

//life is awesome

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        setData();
        listView =findViewById(R.id.list);
        adapter = new ListViewAdapter(MainActivity.this, R.layout.item_listview, stringArrayList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                 int booknumber=position+1;
                 String  bookname=stringArrayList.get(position);

                //Toast.makeText(MainActivity.this, ""+booknumber, Toast.LENGTH_SHORT).show();

                Intent intent=new Intent(MainActivity.this,Chapters.class);

                intent.putExtra("booknumber",booknumber);

                intent.putExtra("bookname",bookname);

                startActivity(intent);

            }
        });
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.search_voice_btn:
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivityForResult(intent, 10);
                } else {
                    Toast.makeText(this, "Your Device Don't Support Speech Input", Toast.LENGTH_SHORT).show();
                }

                break;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case 10:
                if (resultCode == RESULT_OK && data != null) {

                    ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                    Toast.makeText(this, ""+result.get(0), Toast.LENGTH_SHORT).show();

                }
                break;
        }
    }

    @Override
    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.main,menu);
    MenuItem menuItem=menu.findItem(R.id.action_search);

        SearchView searchView=(SearchView)menuItem.getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;

            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (TextUtils.isEmpty(newText)) {
                   adapter.getFilter().filter(newText);
                    listView.clearTextFilter();

                } else {
                    adapter.getFilter().filter(newText);
                }

                return true;

            }
        });

    return  true;
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        Fragment fragment;
        int id = item.getItemId();

        if (id == R.id.nav_camera) {

        } else if (id == R.id.favoruite)
        {
         Intent intent=new Intent(this,Favourite.class);

         startActivity(intent);
        }  else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private void setData() {

        stringArrayList = new ArrayList<>();

        mDBHelper = new DatabaseHelper(this);
        mDb = mDBHelper.getReadableDatabase();

        Cursor cursor = mDb.rawQuery("select * from key_english", new String[]{});

        if(cursor!=null && cursor.getCount() > 0)
        {
            if (cursor.moveToFirst())
            {
                do {
                    stringArrayList.add(cursor.getString(1));
                } while (cursor.moveToNext());
            }
        }
            }

            }

My action search menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_search"
        android:title="@string/search"
        android:icon="@android:drawable/ic_menu_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always" />

    <item
        android:id="@+id/search_voice_btn"
        android:icon="@drawable/ic_mic"
        app:showAsAction="always"
        android:title="Voice Search" />
</menu>

Body:
For your adapter, you have made a public method called filter which is not a real Filter class. I suggest 2 different methods that may make the posted code work.

Method 1: In the Activity, change:adapter.getFilter().filter(newText); to adapter.filter(newText);.

Method 2: Since your data is a String list and ArrayAdapter is already Filterable, so keep adapter.getFilter().filter(newText); and just make the adapter simple like this:

public class ListViewAdapter extends ArrayAdapter {
private MainActivity activity;

public ListViewAdapter(MainActivity context, int resource, List<String> objects) {
    super(context, resource, objects);
    this.activity = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    // If holder not exist then locate all view from UI file.
    if (convertView == null) {
        // inflate UI from XML file
        convertView = inflater.inflate(R.layout.item_listview, parent, false);
        // get all UI view
        holder = new ViewHolder(convertView);
        // set tag for holder
        convertView.setTag(holder);
    } else {
        // if holder created, get tag from view
        holder = (ViewHolder) convertView.getTag();
    }

    String item = (String)getItem(position);
    holder.friendName.setText(item);
    //get first letter of each String item
    String firstLetter = item.substring(0, 1);
    ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
    // generate random color
    int color = generator.getColor(item);
    TextDrawable drawable = TextDrawable.builder()
            .buildRound(firstLetter, color); // radius in px
    holder.imageView.setImageDrawable(drawable);
    return convertView;
}

private class ViewHolder {
    private ImageView imageView;
    private TextView friendName;

    public ViewHolder(View v) {
        imageView = (ImageView) v.findViewById(R.id.image_view);
        friendName = (TextView) v.findViewById(R.id.text);
    }
}

}

My blog on ListView also contains samples with Filter: http://programandroidlistview.blogspot.com/ Hope that helps!


来源: stackexchange

问:
Image flicker in recyclerview with glide
I am using glide 3.7 to load images in recyclerview from firebase storage. When new image is added to recyclerview and notifiydatasetchanged is called then all the existing images in recyclerview flickers and reloads again. How can I avoid image flickering when notifydatasetchanged is called.
here is my adapter class

```<code>public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

    private static final String TAG = "RecyclerAdapter";

    private Context mContext;
    private List<ModelGridItems> gridItemsList;
    private boolean fromService;

    public RecyclerAdapter(Context context, List<ModelGridItems> list, boolean fromservice) {
        gridItemsList = list;
        mContext = context;
        fromService = fromservice;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_items, parent, false);
        return new RecyclerAdapter.MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        try {
            holder.bind(gridItemsList.get(position));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (gridItemsList == null) {
            return 0;
        }
        return gridItemsList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView btnAction;
        TextView tvParticipantName;
        ImageView ivParticipantImage;
        TextView tvConnection;

        MyViewHolder(View itemView) {
            super(itemView);
            ivParticipantImage = itemView.findViewById(R.id.profile_image);
            btnAction = itemView.findViewById(R.id.iv_more);

            tvParticipantName = itemView.findViewById(R.id.tv_username);
            tvParticipantName.setTypeface(BaseActivity.typefaceCabinBold);

            tvConnection = itemView.findViewById(R.id.tv_status);
            tvConnection.setTypeface(BaseActivity.typefaceCabinBold);

        }

        void bind(final ModelGridItems item) {
            tvParticipantName.setText(item.getParticipantName());
            if (item.getIsConnecting()) {
                tvConnection.setVisibility(View.VISIBLE);
            } else {
                tvConnection.setVisibility(View.GONE);
            }
            if (item.getParticipantId().equals(BaseActivity.mUniqueId)) {
                btnAction.setVisibility(View.INVISIBLE);
            }
            if (BaseActivity.mUniqueId.equals(item.getParticipantId())) {
                loadProfileImage();

            } else {
                StorageReference storageReference = BaseActivity.storageReference.child(AppConfig.profileBucket + item.getParticipantId() + AppConfig.ImageExtension);
                SharedMethods.loadFromCloudCircular(mContext, ivParticipantImage, storageReference);
            }
            btnAction.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showAnchorMenu(btnAction, item.getParticipantId());
                }

            });
            ivParticipantImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (fromService) {
                        Intent groupCallIntent = new Intent(mContext, GroupCall.class);
                        groupCallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        groupCallIntent.putParcelableArrayListExtra("GridItems", (ArrayList<? extends Parcelable>) gridItemsList);
                        mContext.startActivity(groupCallIntent);
                    } else {
                        SharedMethods.showLargeImageFromCloud(mContext, item.getParticipantId());
                    }

                }
            });

        }

        private void loadProfileImage() {
            if (SharedMethods.isPermissionGranted(SharedMethods.PERMISSION_WRITE_STORAGE, mContext)) {
                SharedMethods.loadFromLocalStorageCircular((Activity) mContext, ivParticipantImage, SharedMethods.profileImagePath());
            } else {
                StorageReference storageReference = BaseActivity.storageReference.child(AppConfig.profileBucket + BaseActivity.mUniqueId + AppConfig.ImageExtension);
                SharedMethods.loadFromCloudCircular(mContext, ivParticipantImage, storageReference);
            }
        }
    }

}
</code>```

THis method lads images from firebase storage

```<code>public static void loadFromCloudCircular(final Context context, final ImageView imageView, StorageReference storageReference) {
        Glide.with(context)
                .using(new FirebaseImageLoader())
                .load(storageReference)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .placeholder(R.drawable.unknown_image)
                .transform(new CircularTransform(context))
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                        Dashboard.isCircularLoaded = true;
                    }
                });

    }
</code>```

I tried different options like setHasStableIds(true) but this adds the item to top of existing items and the animates to its position. Also tried with itemanimator shown below, but image always flicker. Any help?

```<code>ItemAnimator animator = rv.getItemAnimator();
if (animator instanceof SimpleItemAnimator) {
    ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
}
</code>```

答:
Suggestion 1: Use Picasso instead of Glide
  Picasso Square Image Viewer

I somehow don't like Glide, and Picasso is similar to it in syntax.

Suggestion 2: Don't use images that are above 1MB

It is memory intensive and has no effect on the image quality if you view it in 250dp height of you mobile. Better to reduce the size of the image and send it to firebase, if you are uploading from the app itself. If you need help with that reduction in size of Image check this link.Codota link to compress images to required size

Important Suggestion: Don't use the animation for now and check if it still flickers

Try these suggestions and check if it helps.

ContentLicense: CC BY-SA 4.0

Body:
I am using glide 3.7 to load images in recyclerview from firebase storage. When new image is added to recyclerview and notifiydatasetchanged is called then all the existing images in recyclerview flickers and reloads again. How can I avoid image flickering when notifydatasetchanged is called.
here is my adapter class

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {

    private static final String TAG = "RecyclerAdapter";

    private Context mContext;
    private List<ModelGridItems> gridItemsList;
    private boolean fromService;

    public RecyclerAdapter(Context context, List<ModelGridItems> list, boolean fromservice) {
        gridItemsList = list;
        mContext = context;
        fromService = fromservice;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_items, parent, false);
        return new RecyclerAdapter.MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        try {
            holder.bind(gridItemsList.get(position));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (gridItemsList == null) {
            return 0;
        }
        return gridItemsList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView btnAction;
        TextView tvParticipantName;
        ImageView ivParticipantImage;
        TextView tvConnection;

        MyViewHolder(View itemView) {
            super(itemView);
            ivParticipantImage = itemView.findViewById(R.id.profile_image);
            btnAction = itemView.findViewById(R.id.iv_more);

            tvParticipantName = itemView.findViewById(R.id.tv_username);
            tvParticipantName.setTypeface(BaseActivity.typefaceCabinBold);

            tvConnection = itemView.findViewById(R.id.tv_status);
            tvConnection.setTypeface(BaseActivity.typefaceCabinBold);

        }

        void bind(final ModelGridItems item) {
            tvParticipantName.setText(item.getParticipantName());
            if (item.getIsConnecting()) {
                tvConnection.setVisibility(View.VISIBLE);
            } else {
                tvConnection.setVisibility(View.GONE);
            }
            if (item.getParticipantId().equals(BaseActivity.mUniqueId)) {
                btnAction.setVisibility(View.INVISIBLE);
            }
            if (BaseActivity.mUniqueId.equals(item.getParticipantId())) {
                loadProfileImage();

            } else {
                StorageReference storageReference = BaseActivity.storageReference.child(AppConfig.profileBucket + item.getParticipantId() + AppConfig.ImageExtension);
                SharedMethods.loadFromCloudCircular(mContext, ivParticipantImage, storageReference);
            }
            btnAction.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    showAnchorMenu(btnAction, item.getParticipantId());
                }

            });
            ivParticipantImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (fromService) {
                        Intent groupCallIntent = new Intent(mContext, GroupCall.class);
                        groupCallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        groupCallIntent.putParcelableArrayListExtra("GridItems", (ArrayList<? extends Parcelable>) gridItemsList);
                        mContext.startActivity(groupCallIntent);
                    } else {
                        SharedMethods.showLargeImageFromCloud(mContext, item.getParticipantId());
                    }

                }
            });

        }

        private void loadProfileImage() {
            if (SharedMethods.isPermissionGranted(SharedMethods.PERMISSION_WRITE_STORAGE, mContext)) {
                SharedMethods.loadFromLocalStorageCircular((Activity) mContext, ivParticipantImage, SharedMethods.profileImagePath());
            } else {
                StorageReference storageReference = BaseActivity.storageReference.child(AppConfig.profileBucket + BaseActivity.mUniqueId + AppConfig.ImageExtension);
                SharedMethods.loadFromCloudCircular(mContext, ivParticipantImage, storageReference);
            }
        }
    }

}

THis method lads images from firebase storage

public static void loadFromCloudCircular(final Context context, final ImageView imageView, StorageReference storageReference) {
        Glide.with(context)
                .using(new FirebaseImageLoader())
                .load(storageReference)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .placeholder(R.drawable.unknown_image)
                .transform(new CircularTransform(context))
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                        Dashboard.isCircularLoaded = true;
                    }
                });

    }

I tried different options like setHasStableIds(true) but this adds the item to top of existing items and the animates to its position. Also tried with itemanimator shown below, but image always flicker. Any help?

ItemAnimator animator = rv.getItemAnimator();
if (animator instanceof SimpleItemAnimator) {
    ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
}


问:
How can my installer optionally delete some files it didn't initially create?
I store all my program's settings in the appdata directory %appdata%/MyProgram.  When there is a problem and the user has to reinstall, I want to ask whether to delete the data in that directory. I am using Inno Setup and added a custom page to prompt the user:

```<code>if DirExists(ExpandConstant('{userappdata}\MyProgram')) then
begin
  appdataPage := CreateInputOptionPage(wpSelectTasks,
    'Stored Settings', 'Would you like to remove settings before re-install?',
    'Please specify if you would like to remove the current settings before re-installing MyProgram.', True, False);
  appdataPage.Add('Remove current settings (Recommended if MyProgram is having problems).');
  appdataPage.Add('Keep all my settings and just reinstall.');
  //appdataPage.Add();
  appdataPage.Values[0] := true;
end;
</code>```

The installer doesn't put the data there, but the program generates it, thus the uninstaller won't delete it automatically.  I also can't count on the uninstaller being run; a lot of users just run the installer again.

How can I handle the selection from this dialog so that if they click 'remove before re-install' I delete the folder %appdata%/MyProgram?  I don't want to always delete it, but give them the option.

答:
You don't need a custom page to do that. Just set up an optional task something like this:

```<code>[Tasks]
Name: deletefiles; Description: 'Remove any existing settings'; Flags: unchecked
</code>```

Then delete those files in both the install and the uninstall section

```<code>[InstallDelete]
Type: files; Name: {userappdata}\MyProgram\*.whatever; Tasks: deletefiles

[UninstallDelete]
Type: files; Name: {userappdata}\MyProgram\*.whatever;
</code>```

The files can be optionally deleted during a reinstall, but always deleted during uninstall.

Be careful how you specify the wildcard however, to make sure only the right kinds of temporary files are discarded.

ContentLicense: CC BY-SA 3.0

Body:
I store all my program's settings in the appdata directory %appdata%/MyProgram.  When there is a problem and the user has to reinstall, I want to ask whether to delete the data in that directory. I am using Inno Setup and added a custom page to prompt the user:

if DirExists(ExpandConstant('{userappdata}\MyProgram')) then
begin
  appdataPage := CreateInputOptionPage(wpSelectTasks,
    'Stored Settings', 'Would you like to remove settings before re-install?',
    'Please specify if you would like to remove the current settings before re-installing MyProgram.', True, False);
  appdataPage.Add('Remove current settings (Recommended if MyProgram is having problems).');
  appdataPage.Add('Keep all my settings and just reinstall.');
  //appdataPage.Add();
  appdataPage.Values[0] := true;
end;

The installer doesn't put the data there, but the program generates it, thus the uninstaller won't delete it automatically.  I also can't count on the uninstaller being run; a lot of users just run the installer again.

How can I handle the selection from this dialog so that if they click 'remove before re-install' I delete the folder %appdata%/MyProgram?  I don't want to always delete it, but give them the option.

Body:
You don't need a custom page to do that. Just set up an optional task something like this:

[Tasks]
Name: deletefiles; Description: 'Remove any existing settings'; Flags: unchecked

Then delete those files in both the install and the uninstall section

[InstallDelete]
Type: files; Name: {userappdata}\MyProgram\*.whatever; Tasks: deletefiles

[UninstallDelete]
Type: files; Name: {userappdata}\MyProgram\*.whatever;

The files can be optionally deleted during a reinstall, but always deleted during uninstall.

Be careful how you specify the wildcard however, to make sure only the right kinds of temporary files are discarded.


问:
Ansible + Ubuntu 18.04 + MySQL = "The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is required."
I’m seeing the above message on multiple playbooks using Ansible 2.8 on Ubuntu 18.04. In the interests of simplicity I’ve reproduced it using this basic playbook for a single node Drupal server. https://github.com/geerlingguy/ansible-for-devops/tree/master/drupal; this playbook works fine on earlier versions of ubuntu, but not on 18.04 which I understand includes python3 by default.

I’ve used vagrant to create the base machine, which shows the following:

```<code>$ which python
/usr/bin/python
$ which python2
/usr/bin/python2
$ which python3
/usr/bin/python3
$ python --version
Python 2.7.15rc1
$ python2 --version
Python 2.7.15rc1
$ python3 --version
Python 3.6.7
</code>```

Which seems to be telling me that both python 2 and python 3 are installed, but that 2.7 is the default as that is what responds to $ python --version.

I have tried all the suggestions described in this article: https://www.rollnorocks.com/2018/12/ansible-python-and-mysql-untangling-the-mess/
Including specifying the

```<code>ansible_python_interpreter=/usr/bin/python3
</code>```

But nothing affects the message. The edited -vvv output from the playbook run is below. Has anyone got any more ideas about either the problem or solution.

```<code>TASK [Remove the MySQL test database.] ****************************************************************************************************************************
task path: /vagrant/provisioning/playbook.yml:96
<10.1.1.11> ESTABLISH SSH CONNECTION FOR USER: mt-ansible-user
<10.1.1.11> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o
.
.
.
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/database/mysql/mysql_db.py
<10.1.1.11> PUT /home/mt-tools-user/.ansible/tmp/ansible-local-21287bh5dK5/tmp7pOKOH TO /home/mt-ansible-user/.ansible/tmp/ansible-tmp-1558868136.49-16683638151793
1/AnsiballZ_mysql_db.py
<10.1.1.11> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/home/mt-tools-user/.ssh/mt_ansible_rsa"' -o KbdInteractiveAuthent
ication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="mt-ansible-user"' -o ConnectTimeout=
10 -o ControlPath=/home/mt-tools-user/.ansible/cp/af4de51057 '[10.1.1.11]'
<10.1.1.11> (0, 'sftp> put /home/mt-tools-user/.ansible/tmp/ansible-local-21287bh5dK5/tmp7pOKOH /home/mt-ansible-user/.ansible/tmp/ansible-tmp-1558868136.49-166836
381517931/AnsiballZ_mysql_db.py\n', '')
<10.1.1.11> ESTABLISH SSH CONNECTION FOR USER: mt-ansible-user
<10.1.1.11> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/home/mt-tools-user/.ssh/mt_ansible_rsa"' -o KbdInteractiveAuthenticatio
n=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="mt-ansible-user"' -o ConnectTimeout=10 -o
ControlPath=/home/mt-tools-user/.ansible/cp/af4de51057 10.1.1.11 '/bin/sh -c '"'"'chmod u+x /home/mt-ansible-user/.ansible/tmp/ansible-tmp-1558868136.49-1668363815
17931/ /home/mt-ansible-user/.ansible/tmp/ansible-tmp-1558868136.49-166836381517931/AnsiballZ_mysql_db.py && sleep 0'"'"''
<10.1.1.11> (0, '', '')
<10.1.1.11> ESTABLISH SSH CONNECTION FOR USER: mt-ansible-user
<10.1.1.11> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/home/mt-tools-user/.ssh/mt_ansible_rsa"' -o KbdInteractiveAuthenticatio
n=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="mt-ansible-user"' -o ConnectTimeout=10 -o
ControlPath=/home/mt-tools-user/.ansible/cp/af4de51057 -tt 10.1.1.11 '/bin/sh -c '"'"'sudo -H -S -n  -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-qwjewg
qckuyapsxnkbqoegainrkyiinc ; /usr/bin/python3 /home/mt-ansible-user/.ansible/tmp/ansible-tmp-1558868136.49-166836381517931/AnsiballZ_mysql_db.py'"'"'"'"'"'"'"'"' &
& sleep 0'"'"''
<10.1.1.11> (1, 'BECOME-SUCCESS-qwjewgqckuyapsxnkbqoegainrkyiinc\r\n\r\n{"msg": "The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is req
uired.", "failed": true, "invocation": {"module_args": {"db": "test", "state": "absent", "name": "test", "login_host": "localhost", "login_port": 3306, "encoding":
"", "collation": "", "connect_timeout": 30, "config_file": "/root/.my.cnf", "single_transaction": false, "quick": true, "ignore_tables": [], "login_user": null, "
login_password": null, "login_unix_socket": null, "target": null, "client_cert": null, "client_key": null, "ca_cert": null}}}\r\n', 'Shared connection to 10.1.1.11
closed.\r\n')
<10.1.1.11> Failed to connect to the host via ssh: Shared connection to 10.1.1.11 closed.
<10.1.1.11> ESTABLISH SSH CONNECTION FOR USER: mt-ansible-user
<10.1.1.11> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/home/mt-tools-user/.ssh/mt_ansible_rsa"' -o KbdInteractiveAuthenticatio
n=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="mt-ansible-user"' -o ConnectTimeout=10 -o
ControlPath=/home/mt-tools-user/.ansible/cp/af4de51057 10.1.1.11 '/bin/sh -c '"'"'rm -f -r /home/mt-ansible-user/.ansible/tmp/ansible-tmp-1558868136.49-16683638151
7931/ > /dev/null 2>&1 && sleep 0'"'"''
<10.1.1.11> (0, '', '')
</code>```

答:
Your remote host is telling you:

The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is required.

Did you follow the recommendations and install the required pymysql python package on your remote host ?

For a quick test, on your remote host:

if using python 2.7: sudo pip install pymysql

if using python 3.x: sudo pip3 install pymysql

Once tested, to make sure this dependency is always present, add a task in your playbook prior to launching any mysql task:

```<code>- name: Make sure pymysql is present
  become: true # needed if the other tasks are not played as root
  pip:
    name: pymysql
    state: present
</code>```

You should not have to specify the executable option in this case (see doc) as it will default to your ansible_python_interpreter

Body:
I’m seeing the above message on multiple playbooks using Ansible 2.8 on Ubuntu 18.04. In the interests of simplicity I’ve reproduced it using this basic playbook for a single node Drupal server. https://github.com/geerlingguy/ansible-for-devops/tree/master/drupal; this playbook works fine on earlier versions of ubuntu, but not on 18.04 which I understand includes python3 by default.

I’ve used vagrant to create the base machine, which shows the following:

$ which python
/usr/bin/python
$ which python2
/usr/bin/python2
$ which python3
/usr/bin/python3
$ python --version
Python 2.7.15rc1
$ python2 --version
Python 2.7.15rc1
$ python3 --version
Python 3.6.7

Which seems to be telling me that both python 2 and python 3 are installed, but that 2.7 is the default as that is what responds to $ python --version.

I have tried all the suggestions described in this article: https://www.rollnorocks.com/2018/12/ansible-python-and-mysql-untangling-the-mess/
Including specifying the

ansible_python_interpreter=/usr/bin/python3

But nothing affects the message. The edited -vvv output from the playbook run is below. Has anyone got any more ideas about either the problem or solution.

TASK [Remove the MySQL test database.] ****************************************************************************************************************************
task path: /vagrant/provisioning/playbook.yml:96
<10.1.1.11> ESTABLISH SSH CONNECTION FOR USER: mt-ansible-user
<10.1.1.11> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o
.
.
.
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/database/mysql/mysql_db.py
<10.1.1.11> PUT /home/mt-tools-user/.ansible/tmp/ansible-local-21287bh5dK5/tmp7pOKOH TO /home/mt-ansible-user/.ansible/tmp/ansible-tmp-1558868136.49-16683638151793
1/AnsiballZ_mysql_db.py
<10.1.1.11> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/home/mt-tools-user/.ssh/mt_ansible_rsa"' -o KbdInteractiveAuthent
ication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="mt-ansible-user"' -o ConnectTimeout=
10 -o ControlPath=/home/mt-tools-user/.ansible/cp/af4de51057 '[10.1.1.11]'
<10.1.1.11> (0, 'sftp> put /home/mt-tools-user/.ansible/tmp/ansible-local-21287bh5dK5/tmp7pOKOH /home/mt-ansible-user/.ansible/tmp/ansible-tmp-1558868136.49-166836
381517931/AnsiballZ_mysql_db.py\n', '')
<10.1.1.11> ESTABLISH SSH CONNECTION FOR USER: mt-ansible-user
<10.1.1.11> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/home/mt-tools-user/.ssh/mt_ansible_rsa"' -o KbdInteractiveAuthenticatio
n=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="mt-ansible-user"' -o ConnectTimeout=10 -o
ControlPath=/home/mt-tools-user/.ansible/cp/af4de51057 10.1.1.11 '/bin/sh -c '"'"'chmod u+x /home/mt-ansible-user/.ansible/tmp/ansible-tmp-1558868136.49-1668363815
17931/ /home/mt-ansible-user/.ansible/tmp/ansible-tmp-1558868136.49-166836381517931/AnsiballZ_mysql_db.py && sleep 0'"'"''
<10.1.1.11> (0, '', '')
<10.1.1.11> ESTABLISH SSH CONNECTION FOR USER: mt-ansible-user
<10.1.1.11> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/home/mt-tools-user/.ssh/mt_ansible_rsa"' -o KbdInteractiveAuthenticatio
n=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="mt-ansible-user"' -o ConnectTimeout=10 -o
ControlPath=/home/mt-tools-user/.ansible/cp/af4de51057 -tt 10.1.1.11 '/bin/sh -c '"'"'sudo -H -S -n  -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-qwjewg
qckuyapsxnkbqoegainrkyiinc ; /usr/bin/python3 /home/mt-ansible-user/.ansible/tmp/ansible-tmp-1558868136.49-166836381517931/AnsiballZ_mysql_db.py'"'"'"'"'"'"'"'"' &
& sleep 0'"'"''
<10.1.1.11> (1, 'BECOME-SUCCESS-qwjewgqckuyapsxnkbqoegainrkyiinc\r\n\r\n{"msg": "The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is req
uired.", "failed": true, "invocation": {"module_args": {"db": "test", "state": "absent", "name": "test", "login_host": "localhost", "login_port": 3306, "encoding":
"", "collation": "", "connect_timeout": 30, "config_file": "/root/.my.cnf", "single_transaction": false, "quick": true, "ignore_tables": [], "login_user": null, "
login_password": null, "login_unix_socket": null, "target": null, "client_cert": null, "client_key": null, "ca_cert": null}}}\r\n', 'Shared connection to 10.1.1.11
closed.\r\n')
<10.1.1.11> Failed to connect to the host via ssh: Shared connection to 10.1.1.11 closed.
<10.1.1.11> ESTABLISH SSH CONNECTION FOR USER: mt-ansible-user
<10.1.1.11> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/home/mt-tools-user/.ssh/mt_ansible_rsa"' -o KbdInteractiveAuthenticatio
n=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="mt-ansible-user"' -o ConnectTimeout=10 -o
ControlPath=/home/mt-tools-user/.ansible/cp/af4de51057 10.1.1.11 '/bin/sh -c '"'"'rm -f -r /home/mt-ansible-user/.ansible/tmp/ansible-tmp-1558868136.49-16683638151
7931/ > /dev/null 2>&1 && sleep 0'"'"''
<10.1.1.11> (0, '', '')

Body:
Your remote host is telling you:

The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is required.

Did you follow the recommendations and install the required pymysql python package on your remote host ?

For a quick test, on your remote host:

if using python 2.7: sudo pip install pymysql

if using python 3.x: sudo pip3 install pymysql

Once tested, to make sure this dependency is always present, add a task in your playbook prior to launching any mysql task:

- name: Make sure pymysql is present
  become: true # needed if the other tasks are not played as root
  pip:
    name: pymysql
    state: present

You should not have to specify the executable option in this case (see doc) as it will default to your ansible_python_interpreter


问:
Macro to change a page in a website with "pagination"
I am trying to program a macro in Excel to change the page in the following web: https://cebra.com.ar/category/18/Disfraces-and-Accesorios.html

The page does not have the typical buttons to click, with which, several of the solutions that exist on this page do not work ...

Below is a code that I programmed and it works quite well, only that it would lack the final click.

Could someone please help me develop this macro?
Thank you very much in advance!

```<code>Sub change_webpage()

    Dim ie As InternetExplorer

    Dim lis As IHTMLElementCollection

    Dim nextLi As HTMLLIElement, i As Long

    sheetnom = ActiveSheet.Name

    link = "https://cebra.com.ar/category/18/Disfraces-y-Accesorios.html"

    Worksheets(sheetnom).Activate

    Set ie = New InternetExplorer

    ie.Visible = True

    ie.navigate link

    Do While ie.readyState <> READYSTATE_COMPLETE

        Application.StatusBar = "Loading Web page …"

        DoEvents

    Loop

    newNum = -1

    Set objIE = New InternetExplorer

    objIE.navigate fullUrl

    Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop

    Set currPage = ie.document

    Do Until oldNum = newNum

        oldNum = newNum

        newNum = currPage.getElementsByClassName("box-data").length

        Application.Wait Now + TimeSerial(0, 0, 2)

        currPage.parentWindow.scrollBy 0, 100000

        Application.Wait Now + TimeSerial(0, 0, 2)

        If newNum > 400 Then newNum = 400

    Loop

    Set lis = ie.document.getElementsByClassName("pagination")

    Set nextLi = Nothing

    i = 0

    While i < lis.length And nextLi Is Nothing

        If lis(i).innerText = "2" Then Set nextLi = lis(i)

        i = i + 1

    Wend

    If Not nextLi Is Nothing Then

        nextLi.Change = Active

    End If

    'ie.Quit

    link = Empty

    Sheets(sheetnom).Select

    Range("A1").Select

    MsgBox "Done"

End Sub
</code>```

答:
The page doesn't load properly for me but something like the following should work where you find the number of pages and click the > button until you have exhausted all pages.

```<code>Option Explicit
'VBE > Tools > References: Microsoft Internet Controls

Public Sub ClickThroughPages()
    Dim ie As New InternetExplorer, numberOfPages As Long, page As Long
    With ie
        .Visible = True
        .Navigate2 "https://cebra.com.ar/category/18/Disfraces-y-Accesorios.html"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document

            numberOfPages = .querySelectorAll(".setPage").length

            'do something with page 1
            If numberOfPages > 1 Then
                For page = 2 To numberOfPages

                    .querySelector(".fa-angle-right").Click

                    While ie.Busy Or ie.readyState < 4: DoEvents: Wend

                    'do something with next pages
                Next
            End If
        End With
        Stop
        .Quit
    End With
</code>```

Body:
I am trying to program a macro in Excel to change the page in the following web: https://cebra.com.ar/category/18/Disfraces-and-Accesorios.html

The page does not have the typical buttons to click, with which, several of the solutions that exist on this page do not work ...

Below is a code that I programmed and it works quite well, only that it would lack the final click.

Could someone please help me develop this macro?
Thank you very much in advance!

Sub change_webpage()

    Dim ie As InternetExplorer

    Dim lis As IHTMLElementCollection

    Dim nextLi As HTMLLIElement, i As Long

    sheetnom = ActiveSheet.Name

    link = "https://cebra.com.ar/category/18/Disfraces-y-Accesorios.html"

    Worksheets(sheetnom).Activate

    Set ie = New InternetExplorer

    ie.Visible = True

    ie.navigate link

    Do While ie.readyState <> READYSTATE_COMPLETE

        Application.StatusBar = "Loading Web page …"

        DoEvents

    Loop

    newNum = -1

    Set objIE = New InternetExplorer

    objIE.navigate fullUrl

    Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop

    Set currPage = ie.document

    Do Until oldNum = newNum

        oldNum = newNum

        newNum = currPage.getElementsByClassName("box-data").length

        Application.Wait Now + TimeSerial(0, 0, 2)

        currPage.parentWindow.scrollBy 0, 100000

        Application.Wait Now + TimeSerial(0, 0, 2)

        If newNum > 400 Then newNum = 400

    Loop

    Set lis = ie.document.getElementsByClassName("pagination")

    Set nextLi = Nothing

    i = 0

    While i < lis.length And nextLi Is Nothing

        If lis(i).innerText = "2" Then Set nextLi = lis(i)

        i = i + 1

    Wend

    If Not nextLi Is Nothing Then

        nextLi.Change = Active

    End If

    'ie.Quit

    link = Empty

    Sheets(sheetnom).Select

    Range("A1").Select

    MsgBox "Done"

End Sub

Body:
The page doesn't load properly for me but something like the following should work where you find the number of pages and click the > button until you have exhausted all pages.

Option Explicit
'VBE > Tools > References: Microsoft Internet Controls

Public Sub ClickThroughPages()
    Dim ie As New InternetExplorer, numberOfPages As Long, page As Long
    With ie
        .Visible = True
        .Navigate2 "https://cebra.com.ar/category/18/Disfraces-y-Accesorios.html"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document

            numberOfPages = .querySelectorAll(".setPage").length

            'do something with page 1
            If numberOfPages > 1 Then
                For page = 2 To numberOfPages

                    .querySelector(".fa-angle-right").Click

                    While ie.Busy Or ie.readyState < 4: DoEvents: Wend

                    'do something with next pages
                Next
            End If
        End With
        Stop
        .Quit
    End With


问:
Hibernate: Caused by: java.sql.SQLException: Table/View 'USERS' already exists in Schema
I am on Java SE 8, and I use the Hibernate ORM for persistence.

Hibernate 5.3.6.Final

Derby DB 10.14.2.0

The Hibernate Config:

```<code><property name="hibernate.dialect">org.hibernate.dialect.DerbyTenSevenDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property><!-- update vs validate-->
</code>```

The persistence entity, Users DB table, has the following key:

```<code>    @Id
    @Column(name = "email", nullable = false, unique = true, updatable = false)
    private String email = "";
</code>```

When I start the app I get the following information:

```<code> May 23, 2019 10:53:16 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.2.Final}
May 23, 2019 10:53:17 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
May 23, 2019 10:53:17 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.DerbyTenSevenDialect
May 23, 2019 10:53:19 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
INFO: HHH000130: Instantiating explicit connection provider: org.hibernate.connection.C3P0ConnectionProvider
May 23, 2019 10:53:19 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH010002: C3P0 using driver: org.apache.derby.jdbc.EmbeddedDriver at URL: jdbc:derby:appName-v1.db;create=false;useSSL=false
May 23, 2019 10:53:19 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001001: Connection properties: {user=user, password=****}
May 23, 2019 10:53:19 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001003: Autocommit mode: false
May 23, 2019 10:53:19 PM com.mchange.v2.log.MLog
INFO: MLog clients using java 1.4+ standard logging.
May 23, 2019 10:53:20 PM com.mchange.v2.c3p0.C3P0Registry
INFO: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
May 23, 2019 10:53:20 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001007: JDBC isolation level: <unknown>
May 23, 2019 10:53:20 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@c07c305b [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@36772381 [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 250, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, identityToken -> 1hge17oa21s08ng61w9xxa8|40b5cb4d, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 60, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@59324586 [ description -> null, driverClass -> null, factoryClassLocation -> null, forceUseNamedDriverClass -> false, identityToken -> 1hge17oa21s08ng61w9xxa8|73f7aa9e, jdbcUrl -> jdbc:derby:appName-v1.db;create=false;useSSL=false, properties -> {user=******, password=******} ], preferredTestQuery -> null, privilegeSpawnedThreads -> false, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, extensions -> {}, factoryClassLocation -> null, identityToken -> 1hge17oa21s08ng61w9xxa8|71ff733a, numHelperThreads -> 3 ]
May 23, 2019 10:53:22 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@72bcd19] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate: create table app.Users (email varchar(255) not null, pass varchar(255) not null, recoveryPhrase varchar(255) not null, salt varchar(255) not null, uuid varchar(255) not null, primary key (email))
</code>```

Then I get Exceptions:

```<code>WARN: GenerationTarget encountered exception accepting command : Error executing DDL "create table app.Users (email varchar(255) not null, pass varchar(255) not null, recoveryPhrase varchar(255) not null, salt varchar(255) not null, uuid varchar(255) not null, primary key (email))" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table app.Users (email varchar(255) not null, pass varchar(255) not null, recoveryPhrase varchar(255) not null, salt varchar(255) not null, uuid varchar(255) not null, primary key (email))" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:277)
    at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:71)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:309)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at mr.m.v.dataBase.HibernateBOProcessor.<init>(HibernateBOProcessor.java:49)
    at mr.m.v.dataBase.HibernateBOProcessor.<clinit>(HibernateBOProcessor.java:28)
    at mr.m.v.security.authJaas.steady.DBLoginModule.login(DBLoginModule.java:87)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at javax.security.auth.login.LoginContext.invoke(Unknown Source)
    at javax.security.auth.login.LoginContext.access$000(Unknown Source)
    at javax.security.auth.login.LoginContext$4.run(Unknown Source)
    at javax.security.auth.login.LoginContext$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.login.LoginContext.invokePriv(Unknown Source)
    at javax.security.auth.login.LoginContext.login(Unknown Source)
    at mr.m.v.security.authJaas.JaasLoginContext.login(JaasLoginContext.java:103)
    at mr.m.v.gui.MainUI$1.lambda$0(MainUI.java:227)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: Table/View 'USERS' already exists in Schema 'app'.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
    at com.mchange.v2.c3p0.impl.NewProxyStatement.execute(NewProxyStatement.java:75)
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
    ... 29 more
Caused by: ERROR X0Y32: Table/View 'USERS' already exists in Schema 'app'.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.duplicateDescriptorException(Unknown Source)
    at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.addDescriptor(Unknown Source)
    at org.apache.derby.impl.sql.execute.CreateTableConstantAction.executeConstantAction(Unknown Source)
    at org.apache.derby.impl.sql.execute.MiscResultSet.open(Unknown Source)
    at org.apache.derby.impl.sql.GenericPreparedStatement.executeStmt(Unknown Source)
    at org.apache.derby.impl.sql.GenericPreparedStatement.execute(Unknown Source)
    ... 34 more

Hibernate: alter table app.Users drop constraint UK_bnk3qe2j3b69p30frfqk2i76d
Hibernate: alter table app.Users add constraint UK_bnk3qe2j3b69p30frfqk2i76d unique (uuid)
</code>```

What I do not like about it, is that it tries to re-create the DB table each time I start the app up.
It does so when the hibernate.hbm2ddl.auto is set to update.
If I set it to the validate, the Hibernate complains that the DB table does not exist.
The create option is not viable either, it drops the table and re-creates it.
So, I want to keep the DB tables that I have created already. So, I need either update, or validate as for the value.

On the older version of the Hibernate the update option met my requirements.

I wonder whether or not the exceptions might be an acceptable outcome. As, for example, on an application server, if the user authentication fails, you get an exception (on the console on an IDE) as a valid indication of authentication failure. To name only few...

答:
I've figured out the work around.
It appears that the solution is to set the hibernate.hbm2ddl.auto to none. Then the app runs as expected, and does the DB communication. That is, does not try to re-create the table.

Yet, if the DB has no tables, and you want to create them at run-time (with Hibernate), the key can be set, for example, to update. But next time you start the app up, set the key to none, as long all the tables that you want the system has created already.

Body:
I am on Java SE 8, and I use the Hibernate ORM for persistence.

Hibernate 5.3.6.Final

Derby DB 10.14.2.0

The Hibernate Config:

<property name="hibernate.dialect">org.hibernate.dialect.DerbyTenSevenDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>

The persistence entity, Users DB table, has the following key:

    @Id
    @Column(name = "email", nullable = false, unique = true, updatable = false)
    private String email = "";

When I start the app I get the following information:

 May 23, 2019 10:53:16 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.2.Final}
May 23, 2019 10:53:17 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
May 23, 2019 10:53:17 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.DerbyTenSevenDialect
May 23, 2019 10:53:19 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
INFO: HHH000130: Instantiating explicit connection provider: org.hibernate.connection.C3P0ConnectionProvider
May 23, 2019 10:53:19 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH010002: C3P0 using driver: org.apache.derby.jdbc.EmbeddedDriver at URL: jdbc:derby:appName-v1.db;create=false;useSSL=false
May 23, 2019 10:53:19 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001001: Connection properties: {user=user, password=****}
May 23, 2019 10:53:19 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001003: Autocommit mode: false
May 23, 2019 10:53:19 PM com.mchange.v2.log.MLog
INFO: MLog clients using java 1.4+ standard logging.
May 23, 2019 10:53:20 PM com.mchange.v2.c3p0.C3P0Registry
INFO: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
May 23, 2019 10:53:20 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001007: JDBC isolation level: <unknown>
May 23, 2019 10:53:20 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@c07c305b [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@36772381 [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 250, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, identityToken -> 1hge17oa21s08ng61w9xxa8|40b5cb4d, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 60, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@59324586 [ description -> null, driverClass -> null, factoryClassLocation -> null, forceUseNamedDriverClass -> false, identityToken -> 1hge17oa21s08ng61w9xxa8|73f7aa9e, jdbcUrl -> jdbc:derby:appName-v1.db;create=false;useSSL=false, properties -> {user=******, password=******} ], preferredTestQuery -> null, privilegeSpawnedThreads -> false, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, extensions -> {}, factoryClassLocation -> null, identityToken -> 1hge17oa21s08ng61w9xxa8|71ff733a, numHelperThreads -> 3 ]
May 23, 2019 10:53:22 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@72bcd19] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate: create table app.Users (email varchar(255) not null, pass varchar(255) not null, recoveryPhrase varchar(255) not null, salt varchar(255) not null, uuid varchar(255) not null, primary key (email))

Then I get Exceptions:

WARN: GenerationTarget encountered exception accepting command : Error executing DDL "create table app.Users (email varchar(255) not null, pass varchar(255) not null, recoveryPhrase varchar(255) not null, salt varchar(255) not null, uuid varchar(255) not null, primary key (email))" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table app.Users (email varchar(255) not null, pass varchar(255) not null, recoveryPhrase varchar(255) not null, salt varchar(255) not null, uuid varchar(255) not null, primary key (email))" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.createTable(AbstractSchemaMigrator.java:277)
    at org.hibernate.tool.schema.internal.GroupedSchemaMigratorImpl.performTablesMigration(GroupedSchemaMigratorImpl.java:71)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.performMigration(AbstractSchemaMigrator.java:207)
    at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.doMigration(AbstractSchemaMigrator.java:114)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:309)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:462)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
    at mr.m.v.dataBase.HibernateBOProcessor.<init>(HibernateBOProcessor.java:49)
    at mr.m.v.dataBase.HibernateBOProcessor.<clinit>(HibernateBOProcessor.java:28)
    at mr.m.v.security.authJaas.steady.DBLoginModule.login(DBLoginModule.java:87)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at javax.security.auth.login.LoginContext.invoke(Unknown Source)
    at javax.security.auth.login.LoginContext.access$000(Unknown Source)
    at javax.security.auth.login.LoginContext$4.run(Unknown Source)
    at javax.security.auth.login.LoginContext$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.login.LoginContext.invokePriv(Unknown Source)
    at javax.security.auth.login.LoginContext.login(Unknown Source)
    at mr.m.v.security.authJaas.JaasLoginContext.login(JaasLoginContext.java:103)
    at mr.m.v.gui.MainUI$1.lambda$0(MainUI.java:227)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: Table/View 'USERS' already exists in Schema 'app'.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
    at com.mchange.v2.c3p0.impl.NewProxyStatement.execute(NewProxyStatement.java:75)
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:54)
    ... 29 more
Caused by: ERROR X0Y32: Table/View 'USERS' already exists in Schema 'app'.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.duplicateDescriptorException(Unknown Source)
    at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.addDescriptor(Unknown Source)
    at org.apache.derby.impl.sql.execute.CreateTableConstantAction.executeConstantAction(Unknown Source)
    at org.apache.derby.impl.sql.execute.MiscResultSet.open(Unknown Source)
    at org.apache.derby.impl.sql.GenericPreparedStatement.executeStmt(Unknown Source)
    at org.apache.derby.impl.sql.GenericPreparedStatement.execute(Unknown Source)
    ... 34 more

Hibernate: alter table app.Users drop constraint UK_bnk3qe2j3b69p30frfqk2i76d
Hibernate: alter table app.Users add constraint UK_bnk3qe2j3b69p30frfqk2i76d unique (uuid)

What I do not like about it, is that it tries to re-create the DB table each time I start the app up.
It does so when the hibernate.hbm2ddl.auto is set to update.
If I set it to the validate, the Hibernate complains that the DB table does not exist.
The create option is not viable either, it drops the table and re-creates it.
So, I want to keep the DB tables that I have created already. So, I need either update, or validate as for the value.

On the older version of the Hibernate the update option met my requirements.

I wonder whether or not the exceptions might be an acceptable outcome. As, for example, on an application server, if the user authentication fails, you get an exception (on the console on an IDE) as a valid indication of authentication failure. To name only few...


问:
How to extract lines after specific words?
I want to get date and specific item in a text using regular expression in python 3. Below is an example:

```<code>text = '''
190219 7:05:30 line1 fail
               line1 this is the 1st fail
               line2 fail
               line2 this is the 2nd fail
               line3 success
               line3 this is the 1st success process
               line3 this process need 3sec
200219 9:10:10 line1 fail
               line1 this is the 1st fail
               line2 success
               line2 this is the 1st success process
               line2 this process need 4sec
               line3 success
               line3 this is the 2st success process
               line3 this process need 2sec

'''
</code>```

In the example above, I would like to get all line after 'success line'. Here desired output:

```<code>[('190219','7:05:30','line3 this is the 1st success process', 'line3 this process need 3sec'),
('200219', '9:10:10', 'line2 this is the 1st success process', 'line2 this process need 4sec', 'line3 this is the 2st success process','line3 this process need 2sec')]
</code>```

This is want I've tried:

```<code>>>> newLine = re.sub(r'\t|\n|\r|\s{2,}',' ', text)
>>> newLine
>>> Out[3]: ' 190219 7:05:30 line1 fail  line1 this is the 1st fail  line2 fail  line2 this is the 2nd fail  line3 success line3 this is the 1st success process  line3 this process need 3sec 200219 9:10:10 line1 fail  line1 this is the 1st fail  line2 success line2 this is the 1st success process  line2 this process need 4sec  line3 success line3 this is the 2st success process  line3 this process need 2sec  '
</code>```

I don't know what the proper way to get result. I've tried this to get the line :

```<code>(\b\d{6}\b \d{1,}:\d{2}:\d{2})...
</code>```

How do I solve this problem?

答:
This is my solution using regex:

```<code>text = '''
190219 7:05:30 line1 fail
               line1 this is the 1st fail
               line2 fail
               line2 this is the 2nd fail
               line3 success
               line3 this is the 1st success process
               line3 this process need 3sec
200219 9:10:10 line1 fail
               line1 this is the 1st fail
               line2 success
               line2 this is the 1st success process
               line2 this process need 4sec
               line3 success
               line3 this is the 2st success process
               line3 this process need 2sec
'''

# find desired lines
count = 0
data = []
for item in text.splitlines():
    # find date
    match_date = re.search('\d+\s\d+:\d\d:\d\d', item)
    # get date
    if match_date != None:
        count = 1
        date_time = match_date.group().split(' ')
        for item in date_time:
            data.append(item)
    # find line with success
    match = re.search('\w+\d\ssuccess',item)
    # handle collecting next lines
    if match != None:
        count = 2

    if count > 2:
        data.append(item.strip())

    if count == 2:
        count += 1

# split list data
# find integers i list
numbers = []
for item in data:
     numbers.append(item.isdigit())

# get positions of integers
indexes = [i for i,x in enumerate(numbers) if x == True]
number_of_elements = len(data)
indexes = indexes + [number_of_elements]

# create list of list
result = []
for i in range(0, len(indexes)-1):
    result.append(data[indexes[i]:indexes[i+1]])
</code>```

Result:

```<code>[['190219', '7:05:30', 'line3 this is the 1st success process', 'line3 this process need 3sec'], ['200219', '9:10:10', 'line2 this is the 1st success process', 'line2 this process need 4sec', 'line3 this is the 2st success process', 'line3 this process need 2sec']]
</code>```

Body:
I want to get date and specific item in a text using regular expression in python 3. Below is an example:

text = '''
190219 7:05:30 line1 fail
               line1 this is the 1st fail
               line2 fail
               line2 this is the 2nd fail
               line3 success
               line3 this is the 1st success process
               line3 this process need 3sec
200219 9:10:10 line1 fail
               line1 this is the 1st fail
               line2 success
               line2 this is the 1st success process
               line2 this process need 4sec
               line3 success
               line3 this is the 2st success process
               line3 this process need 2sec

'''

In the example above, I would like to get all line after 'success line'. Here desired output:

[('190219','7:05:30','line3 this is the 1st success process', 'line3 this process need 3sec'),
('200219', '9:10:10', 'line2 this is the 1st success process', 'line2 this process need 4sec', 'line3 this is the 2st success process','line3 this process need 2sec')]

This is want I've tried:

>>> newLine = re.sub(r'\t|\n|\r|\s{2,}',' ', text)
>>> newLine
>>> Out[3]: ' 190219 7:05:30 line1 fail  line1 this is the 1st fail  line2 fail  line2 this is the 2nd fail  line3 success line3 this is the 1st success process  line3 this process need 3sec 200219 9:10:10 line1 fail  line1 this is the 1st fail  line2 success line2 this is the 1st success process  line2 this process need 4sec  line3 success line3 this is the 2st success process  line3 this process need 2sec  '

I don't know what the proper way to get result. I've tried this to get the line :

(\b\d{6}\b \d{1,}:\d{2}:\d{2})...

How do I solve this problem?

Body:
This is my solution using regex:

text = '''
190219 7:05:30 line1 fail
               line1 this is the 1st fail
               line2 fail
               line2 this is the 2nd fail
               line3 success
               line3 this is the 1st success process
               line3 this process need 3sec
200219 9:10:10 line1 fail
               line1 this is the 1st fail
               line2 success
               line2 this is the 1st success process
               line2 this process need 4sec
               line3 success
               line3 this is the 2st success process
               line3 this process need 2sec
'''

# find desired lines
count = 0
data = []
for item in text.splitlines():
    # find date
    match_date = re.search('\d+\s\d+:\d\d:\d\d', item)
    # get date
    if match_date != None:
        count = 1
        date_time = match_date.group().split(' ')
        for item in date_time:
            data.append(item)
    # find line with success
    match = re.search('\w+\d\ssuccess',item)
    # handle collecting next lines
    if match != None:
        count = 2

    if count > 2:
        data.append(item.strip())

    if count == 2:
        count += 1

# split list data
# find integers i list
numbers = []
for item in data:
     numbers.append(item.isdigit())

# get positions of integers
indexes = [i for i,x in enumerate(numbers) if x == True]
number_of_elements = len(data)
indexes = indexes + [number_of_elements]

# create list of list
result = []
for i in range(0, len(indexes)-1):
    result.append(data[indexes[i]:indexes[i+1]])

Result:

[['190219', '7:05:30', 'line3 this is the 1st success process', 'line3 this process need 3sec'], ['200219', '9:10:10', 'line2 this is the 1st success process', 'line2 this process need 4sec', 'line3 this is the 2st success process', 'line3 this process need 2sec']]


问:
slicing pandas df based on repeated cycle values in a particular column
I have df like below (Example)

```<code>index       y       z
0           118     .
1           118     .
2           118     .
3           116
4           116
5           110
6           110
7           104
8           104
.
.
.
.
.
320         3       .
321         3       .
322         3
323         7
324         7
328         11
329         11
.
.
.
350         25
351         25
</code>```

As you can see in column y the values starts from 118 and it keeps on decreasing till number 3 at index 322. Again it keeps increasing from index 323 of value 7 and it reaches the value of 25 at index 351.

Also you can find the repeated value in column y. (118 repeated thrice, 116 repeated twice and so on....)

My Requirement

I would like to slice the first part of that df.(Value from 118 to 3)
i.e., from index 0 till 322.

how my df should look like

```<code>index       y       z
0           118     .
1           118     .
2           118     .
3           116
4           116
5           110
6           110
7           104
8           104
.
.
.
.
.
320         3       .
321         3       .
322         3
</code>```

I believe that there should be an alternate pythonic way which make it easier. (In-built function or using numpy) Any help would be appreciated.

答:
You can get a copy of the required slice by the following command:</p>

```<code>df_copy = df.iloc[0:322]
</code>```

Body:
I have df like below (Example)

index       y       z
0           118     .
1           118     .
2           118     .
3           116
4           116
5           110
6           110
7           104
8           104
.
.
.
.
.
320         3       .
321         3       .
322         3
323         7
324         7
328         11
329         11
.
.
.
350         25
351         25

As you can see in column y the values starts from 118 and it keeps on decreasing till number 3 at index 322. Again it keeps increasing from index 323 of value 7 and it reaches the value of 25 at index 351.

Also you can find the repeated value in column y. (118 repeated thrice, 116 repeated twice and so on....)

My Requirement

I would like to slice the first part of that df.(Value from 118 to 3)
i.e., from index 0 till 322.

how my df should look like

index       y       z
0           118     .
1           118     .
2           118     .
3           116
4           116
5           110
6           110
7           104
8           104
.
.
.
.
.
320         3       .
321         3       .
322         3

I believe that there should be an alternate pythonic way which make it easier. (In-built function or using numpy) Any help would be appreciated.

Body:
You can get a copy of the required slice by the following command:

df_copy = df.iloc[0:322]


问:
rbind data based on matching values in a column
I have several data frames I would like to combine, but I need to get rid of rows that don't have matching values in a column in the other data frames. For example, I want to merge a, b, and c data frames, based on the values in column x.

```<code>a <- data.frame(1:5, 5:9)
colnames(a) <- c("x", "y")
b <- data.frame(1:4, 7:10)
colnames(b) <- c("x", "y")
c <- data.frame(1:3, 6:8)
colnames(c) <- c("x", "y")
</code>```

and have the result be

```<code>1   5
2   6
3   7
1   7
2   8
3   9
1   6
2   7
3   8
</code>```

where the first three rows are from data frame a, the second three rows are from data frame b, and the third three rows are from data frame c, and the rows that didn't have matching values in column x were not included.

答:
We create an index based on intersecting elements of 'x'

```<code>v1 <- Reduce(intersect, list(a$x, b$x, c$x))
rbind(a[a$x %in% v1,], b[b$x %in% v1,], c[c$x %in% v1, ])
#  x y
#1 1 5
#2 2 6
#3 3 7
#4 1 7
#5 2 8
#6 3 9
#7 1 6
#8 2 7
#9 3 8
</code>```

If there are many dataset objects, it is better to keep it in a list.  Here, the example showed the object identifiers as completely different, but if the identifiers have a pattern e.g. df1, df2, ..df100 etc, it becomes easier to get it to a list

```<code>lst1 <- mget(ls(pattern = "^df\\d+$"))
</code>```

If the object identifiers are all different xyz, abc, fq12 etc, but these are the only data.frame objects loaded in the global environment

```<code>lst1 <-  mget(names(eapply(.GlobalEnv, 'is.data.frame')))
</code>```

Then, get the interesecitng elements of the column 'x'

```<code>v1 <- Reduce(intersect, lapply(lst1, `[[`, "x"))
</code>```

Use the intersecting vector to subset the rows of the list elements

```<code>do.call(rbind, lapply(lst1, function(x) dat[dat$x %in% v1,]))
</code>```

Here, we assume the column names are the same across all the datasets

Another option is to do a merge and then unlist

```<code>out <- Reduce(function(...) merge(..., by = 'x'), list(a, b, c))
data.frame(x = out$x, y = unlist(out[-1], use.name = FALSE))
</code>```

Body:
I have several data frames I would like to combine, but I need to get rid of rows that don't have matching values in a column in the other data frames. For example, I want to merge a, b, and c data frames, based on the values in column x.

a <- data.frame(1:5, 5:9)
colnames(a) <- c("x", "y")
b <- data.frame(1:4, 7:10)
colnames(b) <- c("x", "y")
c <- data.frame(1:3, 6:8)
colnames(c) <- c("x", "y")

and have the result be

1   5
2   6
3   7
1   7
2   8
3   9
1   6
2   7
3   8

where the first three rows are from data frame a, the second three rows are from data frame b, and the third three rows are from data frame c, and the rows that didn't have matching values in column x were not included.

Body:
We create an index based on intersecting elements of 'x'

v1 <- Reduce(intersect, list(a$x, b$x, c$x))
rbind(a[a$x %in% v1,], b[b$x %in% v1,], c[c$x %in% v1, ])
#  x y
#1 1 5
#2 2 6
#3 3 7
#4 1 7
#5 2 8
#6 3 9
#7 1 6
#8 2 7
#9 3 8

If there are many dataset objects, it is better to keep it in a list.  Here, the example showed the object identifiers as completely different, but if the identifiers have a pattern e.g. df1, df2, ..df100 etc, it becomes easier to get it to a list

lst1 <- mget(ls(pattern = "^df\\d+$"))

If the object identifiers are all different xyz, abc, fq12 etc, but these are the only data.frame objects loaded in the global environment

lst1 <-  mget(names(eapply(.GlobalEnv, 'is.data.frame')))

Then, get the interesecitng elements of the column 'x'

v1 <- Reduce(intersect, lapply(lst1, `[[`, "x"))

Use the intersecting vector to subset the rows of the list elements

do.call(rbind, lapply(lst1, function(x) dat[dat$x %in% v1,]))

Here, we assume the column names are the same across all the datasets

Another option is to do a merge and then unlist

out <- Reduce(function(...) merge(..., by = 'x'), list(a, b, c))
data.frame(x = out$x, y = unlist(out[-1], use.name = FALSE))