Results 1 to 2 of 2
  1. #1
    lisoslaw is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default OnItemLongClickListener not working properly

    For two days I cannot find the solution to my problem. I created a dialog file:

    Java Code:
    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();
        }
    }
    With appropriate xml for it. Then I updated my file that displays list like this:

    Java 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.AdapterView;
    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(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;
    			        
    			    }
    			});
    			
    			
    	}
    		
    		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);
    	}
    	
    	
    }
    Note this portion of code:
    Java Code:
    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;
    			        
    			    }
    			});
    When I long click item in the list nothing happens. I get no errors at all and I believe that either I put the code in the wrong place or I missed something else that stops Dialog from starting.
    Can anyone help me with this please?

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,087
    Rep Power
    4

    Default Re: OnItemLongClickListener not working properly

    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. For loop not working properly
    By phyzix5761 in forum New To Java
    Replies: 5
    Last Post: 11-18-2012, 08:59 PM
  2. setCaretPosition not working properly
    By phyzix5761 in forum AWT / Swing
    Replies: 6
    Last Post: 11-17-2012, 10:11 AM
  3. If statement is not working properly
    By Alpa in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 09:40 PM
  4. NetBeans IDE not working properly
    By farmer in forum NetBeans
    Replies: 2
    Last Post: 12-09-2011, 04:12 PM
  5. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 01:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •