I cannot set onLongClick Listener
Asked Answered
O

2

0

Newest Edit ---------------------------------------------------------------------------- I updated code and it is correct now, although I cannot get the functionality to run. On long click event nothing happens...


In My file that displays list of rows from database I put the code for setting OnLongClickListener but part of the code (commented) returns an error: The constructor ListView(Monday.MyDiary) is undefined.

Here is my file with inserted new code:

package com.example.classorganizer;


import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.cookbook.data.Constants;
import com.cookbook.data.MyDB;






public class Monday extends ListActivity {



private static final int MyMenu = 0;
MyDB dba;
DiaryAdapter myAdapter;

private class MyDiary{
    public MyDiary(String t, String c){
        title=t;
        content=c;

        ListView listView = new ListView(this); //here the error pops out
        listView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new EditListItemDialog(v.getContext()).show();
                return true;
            }
        });

}

    public String title;
    public String content;

}
@Override
protected void onCreate(Bundle savedInstanceState) {
    dba = new MyDB(this);
    dba.open();
    setContentView(R.layout.fragment_monday);



    super.onCreate(savedInstanceState);
    myAdapter = new DiaryAdapter(this);
    this.setListAdapter(myAdapter);
}



private class DiaryAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private ArrayList<MyDiary> fragment_monday;
    public DiaryAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        fragment_monday = new ArrayList<MyDiary>();
        getdata();


    }

    public void getdata(){
        Cursor c = dba.getdiaries();
        startManagingCursor(c);
        if(c.moveToFirst()){
            do{
                String title =
                        c.getString(c.getColumnIndex(Constants.TITLE_NAME));
                String content =
                        c.getString(c.getColumnIndex(Constants.CONTENT_NAME));

                MyDiary temp = new MyDiary(title,content);
                fragment_monday.add(temp);
            } while(c.moveToNext());
        }

    }



    @Override
    public int getCount() {return fragment_monday.size();}
    public MyDiary getItem(int i) {return fragment_monday.get(i);}
    public long getItemId(int i) {return i;}
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        final ViewHolder holder;
        View v = arg1;
        if ((v == null) || (v.getTag() == null)) {
            v = mInflater.inflate(R.layout.diaryrow,  null);
            holder = new ViewHolder();
            holder.mTitle = (TextView)v.findViewById(R.id.name);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        holder.mdiary = getItem(arg0);
        holder.mTitle.setText(holder.mdiary.title);


        v.setTag(holder);

        return v;


    }

    public class ViewHolder {
        MyDiary mdiary;
        TextView mTitle;


    }

}




/** Called when the user clicks the Edit button */
public void visitDiary(View view) {
    Intent intent = new Intent(this, Diary.class);
    startActivity(intent);
}
/** Called when the user clicks the back button */
public void visitSchedule(View view) {
    Intent intent = new Intent(this, DisplayScheduleScreen.class);
    startActivity(intent);
}


}

I also created a Dialog file:

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();
}
}

I don't know how to resolve this problem. WHat I was looking to achieve is functionality for editing rows displayed in list by longclicking on them.

EDIT----------------------------------------------------------------------------------

now the code looks like this:

ListView listView = new ListView(Monday.this);
        listView.setOnItemLongClickListener(new View.OnItemLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                new EditListItemDialog(v.getContext()).show();
                return true;
            }
        });

Error occurs in View.OnItemLongClickListener in second line

EDIT--------------------------------------------------------------------------------------

Updated code without errors, but not sure if it's right:

ListView listView = new ListView(Monday.this);
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                new EditListItemDialog(view.getContext()).show();
                return true;
            }
        });
Octahedral answered 18/4, 2014 at 9:19 Comment(0)
E
0
ListView listView = new ListView(this);

You should pass to ListView constructor a context, not a MyDiary object.

Fix it using

ListView listView = new ListView(Monday.this);

It should work since your MyDiary is an innerclass of Monday and is not static.

If you change this, you should pass to MyDiary constructor the Context of the activity and use it.


And, for your next question: You should use OnItemLongClickListener in a ListView, not OnLongClickListener.

Eric answered 18/4, 2014 at 9:26 Comment(25)
Your answer fixed my error. But can you tell me why dialog does not start upon LongClick?Octahedral
Have you changed onLongClick with setOnItemLongClickListener?Eric
now it complains that: View.OnItemLongClickListener cannot be resolved to a typeOctahedral
Where you changed it?Eric
I changed this: listView.setOnItemLongClickListener(new View.OnItemLongClickListener() { //second line of codeOctahedral
Maybe you missed the import? add this in top of the file import android.widget.AdapterView.OnItemLongClickListener;Eric
still complains View.OnItemLongClickListener cannot be resolved to a typeOctahedral
try to change View. with AdapterView. // AdapterView.OnItemLongClickListenerEric
- AdapterView cannot be resolved to a type - The method setOnItemLongClickListener(AdapterView.OnItemLongClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (new OnItemLongClickListener(){})Octahedral
Reading comment in the other answer: you should import ListView too import android.widget.ListView;Eric
Change new OnItemLongClickListener with new View.OnItemLongClickListenerEric
"Change new OnItemLongClickListener with new View.OnItemLongClickListener" => have you tried it?Eric
in this case it says that View.OnItemLongClickListener cannot be resolved to a typeOctahedral
have you added all imports?Eric
I updated my question to give you an insight into how it looks now.Octahedral
@MarcoAcierno any idea why this View.OnItemLongClickListener complaints?Octahedral
If it says "cannot be resolved to a type" it means you missed an import. Please use the "auto-import" in your IDEEric
Change your onLongClick(View v) with onItemLongClick(AdapterView<?> parent, View view, int position, long id)Eric
@MarcoAcierno ok, new code doesn't bring new errors, but the old one remains.Octahedral
now, I changed View.OnItemLongClickListener to AdapterView and error disappeared, although now v in 5th lane complaints that it cannot be resolved.Octahedral
@MarcoAcierno I changed this v to view and now there are no errors at all, but I am not sure if code is right. I will update it in my question.Octahedral
@MarcoAcierno is it enough then to be able to use long click functionality on my list?Octahedral
Why not just test it? Anyway yes.Eric
@MarcoAcierno when I run the code, rows in my list are clickable but nothing happens. I guess that Dialog file does not startOctahedral
@MarcoAcierno can you please help me with this OnItemLingClick? I spent whole day on looking for the solution and cannot figure out why dialog does not show up...Octahedral
B
1

Listview can not have setonLongClickListener you are supposed to implement the setOnItemItemLongClickListener as ListView contains the list of items so you can always implement the its items long click listener as below:

listView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            return false;
        }
    });
Breann answered 18/4, 2014 at 9:31 Comment(11)
when I use your answer eclipse returns few errors. - listView cannot be resolved - OnItemLongClickListener cannot be resolved to a type and The method onItemLongClick(AdapterView<?>, View, int, long) of type new OnItemLongClickListener(){} must override or implement a supertype methodOctahedral
@Octahedral Just import the line import android.widget.AdapterView.OnItemLongClickListener; and check. OR press Ctrl+Shift+OBreann
changed it and imported. Now the error listView cannot be resolved pops out in the firt line of your codeOctahedral
I said just press Ctrl+Shift+O.Breann
I've done it but errors remain. it says listView cannot be resolved //in first line listView complainsOctahedral
Its just your ListView only. Can you post your errors screenshot?Breann
yes, it's only listView from first line that complaints listView cannot be resolvedOctahedral
Please update your code. and post screenshot of your error. Make sure you have imported the statement import android.widget.ListView;Breann
I am not sure how to paste screenshot.Octahedral
but I imported listView and OnItemLongClickListener as wellOctahedral
@GrlsHu there are no errors now but I am still unable to run Dialog to update rows from the list...Octahedral
E
0
ListView listView = new ListView(this);

You should pass to ListView constructor a context, not a MyDiary object.

Fix it using

ListView listView = new ListView(Monday.this);

It should work since your MyDiary is an innerclass of Monday and is not static.

If you change this, you should pass to MyDiary constructor the Context of the activity and use it.


And, for your next question: You should use OnItemLongClickListener in a ListView, not OnLongClickListener.

Eric answered 18/4, 2014 at 9:26 Comment(25)
Your answer fixed my error. But can you tell me why dialog does not start upon LongClick?Octahedral
Have you changed onLongClick with setOnItemLongClickListener?Eric
now it complains that: View.OnItemLongClickListener cannot be resolved to a typeOctahedral
Where you changed it?Eric
I changed this: listView.setOnItemLongClickListener(new View.OnItemLongClickListener() { //second line of codeOctahedral
Maybe you missed the import? add this in top of the file import android.widget.AdapterView.OnItemLongClickListener;Eric
still complains View.OnItemLongClickListener cannot be resolved to a typeOctahedral
try to change View. with AdapterView. // AdapterView.OnItemLongClickListenerEric
- AdapterView cannot be resolved to a type - The method setOnItemLongClickListener(AdapterView.OnItemLongClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (new OnItemLongClickListener(){})Octahedral
Reading comment in the other answer: you should import ListView too import android.widget.ListView;Eric
Change new OnItemLongClickListener with new View.OnItemLongClickListenerEric
"Change new OnItemLongClickListener with new View.OnItemLongClickListener" => have you tried it?Eric
in this case it says that View.OnItemLongClickListener cannot be resolved to a typeOctahedral
have you added all imports?Eric
I updated my question to give you an insight into how it looks now.Octahedral
@MarcoAcierno any idea why this View.OnItemLongClickListener complaints?Octahedral
If it says "cannot be resolved to a type" it means you missed an import. Please use the "auto-import" in your IDEEric
Change your onLongClick(View v) with onItemLongClick(AdapterView<?> parent, View view, int position, long id)Eric
@MarcoAcierno ok, new code doesn't bring new errors, but the old one remains.Octahedral
now, I changed View.OnItemLongClickListener to AdapterView and error disappeared, although now v in 5th lane complaints that it cannot be resolved.Octahedral
@MarcoAcierno I changed this v to view and now there are no errors at all, but I am not sure if code is right. I will update it in my question.Octahedral
@MarcoAcierno is it enough then to be able to use long click functionality on my list?Octahedral
Why not just test it? Anyway yes.Eric
@MarcoAcierno when I run the code, rows in my list are clickable but nothing happens. I guess that Dialog file does not startOctahedral
@MarcoAcierno can you please help me with this OnItemLingClick? I spent whole day on looking for the solution and cannot figure out why dialog does not show up...Octahedral

© 2022 - 2024 — McMap. All rights reserved.