ListView OnItemLongClickLisener

47 views Asked by At

Maybe someone can help me. I'm trying to install my ListView to a ItemLongClickListener. What I want to do is this: I have a ListView (lv) recorded with audio tracks. They will be played with one click. It works. The next is when I press a long time an input box opens and sets the name of the pressed items on the EditText of the Input Box. Then I change the name and save the new name. This is the code that I use, but something is not right. And writes "Result of string Replace is ignored"! The declarations are all global.

String str;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText txtInput = new EditText(this);
String m_Text;

public void newName(){

    str = lv.getSelectedItem().getClass().getName();
    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                       int pos, long id) {

            builder.setTitle("New Name");
            txtInput.setInputType(InputType.TYPE_CLASS_TEXT);
            builder.setView(txtInput);
            builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    m_Text = txtInput.getText().toString();
                    str.replace(str, m_Text);
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.show();

            return true;
        }
    });


}
1

There are 1 answers

4
Eric B. On

Strings are immutable. The value is not replaced in the str object itself, rather it is returned, therefore change your code to this:

        builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                m_Text = txtInput.getText().toString();
                str = str.replace(str, m_Text);
            }
        });