2

I searched on various forums for the right answer but nothing works for me. For the moment I have this code in private class MyDiary:

ListView list = new ListView(Monday.this);
        list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    new EditListItemDialog(view.getContext()).show();

                return true;       
            }
        });

It doesn't return errors but the EditListItemDialog does not open. What am I missing here?

EDIT

code for EditListItemDialog:

package com.example.classorganizer;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

class EditListItemDialog extends Dialog implements View.OnClickListener {

private View editText;

public EditListItemDialog(Context context) {
    super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons
    View btnOk = findViewById(R.id.button_ok);
    editText = findViewById(R.id.edit_text);
    btnOk.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    ((TextView) editText).getText().toString();//here is your updated(or not updated) text
    dismiss();
}
}

XML files:

     <ListView
        android:layout_width="fill_parent"
                        android:gravity="center"
        android:layout_height="fill_parent"
    android:id="@android:id/list"
        android:longClickable="true"
         >

    </ListView>

And to create row:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignLeft="@+id/name"
android:layout_below="@+id/name"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="0dip">
<TextView android:textSize="16dip"
                                android:layout_width="wrap_content"
                            android:layout_height="29dp"
                            android:layout_marginTop="0dp"
                            android:id="@+id/name"
                            android:layout_marginRight="4dp"
                            android:text="Diary Title"
                            android:textStyle="bold"
                            android:longClickable="true" />



</RelativeLayout>
lisoslaw
  • 37
  • 1
  • 8
  • I searched for the solution in this thread but nothing helps. – lisoslaw Apr 20 '14 at 07:45
  • @lisoslaw a removed asnwer because your listener works good but most likely your Dialog is not correctly implemented. – Simon Dorociak Apr 20 '14 at 07:49
  • 1
    What is `EditListItemDialog`? Unless you show code for that it's impossible to tell why it doesn't open. – Squonk Apr 20 '14 at 07:50
  • @Ragnar can you instruct me on how to correctly implement Dialog please? – lisoslaw Apr 20 '14 at 07:50
  • @lisoslaw : In the first block of code you posted you are creating a `ListView` using `ListView list = new ListView(Monday.this);`. Where exactly are you adding that to your layout? – Squonk Apr 20 '14 at 08:13
  • @Squonk maybe this is the thing, maybe I didn't add it to the layout – lisoslaw Apr 20 '14 at 08:16
  • should I check my xml for it? – lisoslaw Apr 20 '14 at 08:16
  • @lisoslaw : If it's in your layout file then don't create a new instance just use `findViewById(...)` as you would for any layout content. – Squonk Apr 20 '14 at 08:18
  • @Squonk apart from the code above there is nothing with regards to the ListView – lisoslaw Apr 20 '14 at 08:18
  • IMO you really should consider using DialogFragment instead. Here is an example: http://android-developers.blogspot.dk/2012/05/using-dialogfragments.html. And to test that the longclick is even fired, try making a toast. – cYrixmorten Apr 20 '14 at 08:19
  • @lisoslaw : So how does your `ListView` get shown on the screen? What sort of `Activity` are you using? Is it a `ListActivity` or are you using something like a `ListFragment`? – Squonk Apr 20 '14 at 08:22
  • i will add xml file to my question so you can see, ok? – lisoslaw Apr 20 '14 at 08:34

1 Answers1

0

Assuming you are using a ListActivity you should change the following line (from your first code block)...

ListView list = new ListView(Monday.this);

...to be...

ListView list = getListView();

You can then set the listener for it.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • this change works. It long clicks now and dialog opens. But when I attempt to change value in dialog change doesn't take effect... – lisoslaw Apr 20 '14 at 11:41