hey all,

could somebody please clue me in to why i'm not getting context menu
to show up with touch? it works fine if i use the keyboard.


public class ViewTasks extends ListActivity implements
View.OnCreateContextMenuListener {

        private static final String TAG = "TT:ListActivity";
        private static final int ACTIVITY_ADD=1;
        private static final int ACTIVITY_EDIT=2;
    private static final int DELETE_ID = Menu.FIRST;

    private TaskDbAdapter _dbHelper;
    private Cursor _taskCursor;
    private SimpleDateFormat _listFormat = new SimpleDateFormat("MM-dd-
yy");

        protected CharSequence[] _options;
        protected boolean[] _selections;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        _dbHelper = new TaskDbAdapter(this);
        try {
            _dbHelper.open();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        _options = getResources().getStringArray(R.array.task_item);
        _selections =  new boolean[_options.length];
        for (int i = 0; i < _selections.length; i++) {
                        _selections[i] = true;
                }

        fillData();
        registerForContextMenu(getListView());

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.general_menu, menu);
        return true;
    }


    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                        case R.id.add:
                    Intent i = new Intent(this, TrackTask.class);
                    startActivityForResult(i, ACTIVITY_ADD);
                                return true;
                        case R.id.export:
                                exportData();
                                break;
                        case R.id.filter:
                                showDialog(0);
                                break;
                }
                return super.onOptionsItemSelected(item);
        }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenuInfo menuInfoIn) {
        menu.add(0, DELETE_ID, 0, R.string.menu_delete_task);
    }


    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case DELETE_ID:
                AdapterContextMenuInfo info = (AdapterContextMenuInfo)
item.getMenuInfo();
                _dbHelper.deleteTask(info.id);
                fillData();
                return true;
            default: {
                return super.onContextItemSelected(item);
            }
        }
    }


        @Override
        protected Dialog onCreateDialog(int id) {
                return
                new AlertDialog.Builder(this)
                .setTitle("Filter")
                .setMultiChoiceItems(_options, _selections, new
DialogSelectionClickHandler())
                .setPositiveButton("OK", new DialogButtonClickHandler() )
                .create();
        }


        public class DialogSelectionClickHandler implements
DialogInterface.OnMultiChoiceClickListener {
                public void onClick( DialogInterface dialog, int clicked, 
boolean
selected ) {
                        Log.d( TAG, _options[ clicked ] + " selected: " + 
selected );
                }
        }


        public class DialogButtonClickHandler implements
DialogInterface.OnClickListener {
                public void onClick( DialogInterface dialog, int clicked ) {
                        switch(clicked)  {
                                case DialogInterface.BUTTON_POSITIVE:
                                        fillData();
                                        break;
                        }
                }
        }


    @Override
    protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        if(intent == null) return;
        Bundle extras = intent.getExtras();

        switch(requestCode) {
            case ACTIVITY_EDIT:
                Long rowId = extras.getLong(TaskDbAdapter.KEY_ROWID);
                if (rowId != null) {
                    String item =
extras.getString(TaskDbAdapter.KEY_ITEM);
                    String category =
extras.getString(TaskDbAdapter.KEY_CATEGORY);
                    String note =
extras.getString(TaskDbAdapter.KEY_NOTE);
                    long date =
extras.getLong(TaskDbAdapter.KEY_DATE);
                    _dbHelper.updateTask(rowId, item, category, date,
note);
                }
                fillData();
                break;
                case ACTIVITY_ADD:
                String item =
extras.getString(TaskDbAdapter.KEY_ITEM);
                String category =
extras.getString(TaskDbAdapter.KEY_CATEGORY);
                String note =
extras.getString(TaskDbAdapter.KEY_NOTE);
                long date = extras.getLong(TaskDbAdapter.KEY_DATE);
                        Log.v(TAG, "Adding Task - "+ item + ":"+ category + 
":"+ note
+ ":"+date);
                _dbHelper.createTask(item, category, date, note);
                fillData();
                        break;
        }
    }


    private void exportData() {
        Log.d(TAG, "exporting data");
        try {
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {}

                File root = Environment.getExternalStorageDirectory();
            if (root.canWrite()){
                File csvFile = new File(root, "tanktracker-tasks.csv");
                FileWriter csvWriter = new FileWriter(csvFile);
                BufferedWriter out = new BufferedWriter(csvWriter);

                _taskCursor.moveToFirst();
                while (_taskCursor.isAfterLast() == false) {
 
out.write(_taskCursor.getString(1)+","+_taskCursor.getString(2)+","+new
Date(_taskCursor.getLong(3))+","+_taskCursor.getString(4)+"\n");
                    _taskCursor.moveToNext();
                }
                out.close();
                Toast.makeText(this, "Tasks have been export to your SD
card", Toast.LENGTH_LONG).show();

            } else {
                Log.e(TAG, "Could not write to sdcard");
            }
        } catch (IOException e) {
            Log.e(TAG, "Could not write file " + e.getMessage());
        }
    }


        private void fillData() {
                ArrayList<String> names = new ArrayList<String>();
                for (int i = 0; i < _options.length; i++) {
                        String item = (String) _options[i];
                        if (_selections[i]) names.add(item);
                }
                _taskCursor = _dbHelper.fetchTasksByName(names);
                startManagingCursor(_taskCursor);

                String[] from = new String[] { TaskDbAdapter.KEY_ITEM,
TaskDbAdapter.KEY_CATEGORY, TaskDbAdapter.KEY_DATE };
                int[] to = new int[] { R.id.category_entry, R.id.item_entry,
R.id.date_completed };

                CustomListAdapter customListAdapter = new 
CustomListAdapter(this,
R.layout.list_item, _taskCursor, from, to);
                registerForContextMenu(getListView());
                setListAdapter(customListAdapter);
        }


    final class CustomListAdapter extends SimpleCursorAdapter
implements View.OnClickListener {

                public CustomListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
                        super(context, layout, c, from, to);
                }


                @Override
                public View newView(Context context, Cursor cursor, ViewGroup
parent) {
                LayoutInflater inflater =
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view =  inflater.inflate(R.layout.list_item, parent,
false);
                view.setOnClickListener(this);
                return view;
                }


                @Override
                public void bindView(View view, Context context, Cursor c) {
                        TextView itemView = (TextView) 
view.findViewById(R.id.item_entry);
                        TextView categoryView = (TextView)
view.findViewById(R.id.category_entry);
                        TextView dateView = (TextView)
view.findViewById(R.id.date_completed);

        
itemView.setText(c.getString(c.getColumnIndexOrThrow(TaskDbAdapter.KEY_ITEM)));
        
categoryView.setText(c.getString(c.getColumnIndexOrThrow(TaskDbAdapter.KEY_CATEGORY)));
                        dateView.setText(_listFormat.format(new
Date(c.getLong(c.getColumnIndexOrThrow(TaskDbAdapter.KEY_DATE)))));

                        // view.setBackgroundColor((c.getPosition() & 1) == 1 ?
Color.WHITE : Color.LTGRAY);
                        // view.setOnCreateContextMenuListener(ViewTasks.this); 
 // this
works - kinda of.. no ID
                        view.setTag(c.getPosition());

                }

                public void onClick(View v) {
                int position = (Integer) v.getTag();
                        Log.d(TAG, "You clicked item #" + position);
                Cursor c = getCursor();
                c.moveToPosition(position);
                Intent i = new Intent(v.getContext(), TrackTask.class);
                i.putExtra(TaskDbAdapter.KEY_ROWID,
c.getLong(c.getColumnIndexOrThrow(TaskDbAdapter.KEY_ROWID)));
                i.putExtra(TaskDbAdapter.KEY_ITEM,
c.getString(c.getColumnIndexOrThrow(TaskDbAdapter.KEY_ITEM)));
                i.putExtra(TaskDbAdapter.KEY_CATEGORY,
c.getString(c.getColumnIndexOrThrow(TaskDbAdapter.KEY_CATEGORY)));
                i.putExtra(TaskDbAdapter.KEY_NOTE,
c.getString(c.getColumnIndexOrThrow(TaskDbAdapter.KEY_NOTE)));
                i.putExtra(TaskDbAdapter.KEY_DATE,
c.getLong(c.getColumnIndexOrThrow(TaskDbAdapter.KEY_DATE)));
                startActivityForResult(i, ACTIVITY_EDIT);
                }

    }
}


----
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"     >

        <TextView android:id="@+id/TextView01"
                          android:layout_height="wrap_content"
                          android:text="put some more useful header here"
                          android:textStyle="normal|bold"
                          android:gravity="center_vertical|center_horizontal"
                          android:layout_width="fill_parent" />

        <ListView android:id="@android:id/list"
                          android:layout_height="wrap_content"
                      android:layout_width="fill_parent"  />

        <TextView android:id="@+id/android:empty"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:gravity="center_vertical|center_horizontal"
                  android:text="@string/nothing" />

 </LinearLayout>

-----

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/
android"
                android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
                android:id="@+id/item_entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="left" />
    <TextView
                android:id="@+id/category_entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/item_entry"
                android:gravity="right"
                android:paddingLeft="8sp" />
    <TextView
                android:id="@+id/date_completed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:gravity="right"
                android:paddingLeft="4sp" />

</RelativeLayout>

---

thanks! deno

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to