Вопрос

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>
Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top