简体   繁体   中英

open a menu in ListActivity with onListItemClick

I'm trying to show a menu once the user longclick an entry in my ListActivity but I cant figure it out. Unfourtenatly lists have been always a hard nut for me to crack and I'm still learning.

package android.GUI;

public class Shifts extends ListActivity implements OnClickListener,
    SimpleGestureListener {


private Typeface tf = Entry.tf, tf2 = Entry.tf2;
public static int count = 1;
int dbHourTime = 0;
private SimpleGestureFilter detector;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.shifts);
    detector = new SimpleGestureFilter(this, this);



    DBAdapter DB = new DBAdapter(this);
    DB.open();
    Cursor cursor = DB.getAllShifts();
    startManagingCursor(cursor);
    cursor.moveToLast();
    count = cursor.getPosition();
    int g = count;

    cursor.moveToNext();

    String[] columns = new String[] { DB.KEY_DATE, DB.KEY_HOURS,
            DB.KEY_DAY, DB.KEY_ROWID, DB.KEY_START, DB.KEY_END };
    int[] to = new int[] { R.id.dateDisp, R.id.shiftDisp, R.id.day,
            R.id.rawId, R.id.start, R.id.finish };

    ListView ls = getListView();




    TextView SF = (TextView) findViewById(R.id.total);
    SF.setTypeface(tf);

    TextView sum = (TextView)findViewById(R.id.sum);
    sum.setTypeface(tf);


    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
            R.layout.list_entry, cursor, columns, to);
    this.setListAdapter(mAdapter);


}

 @Override
 protected void onListItemClick(ListView ls, View v, int position, long id) {
  // TODO Auto-generated method stub
  super.onListItemClick(ls, v, position, id);

    CharSequence text = "Clicked";

    final int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(this, text, duration);
    toast.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER, 0, 0);

    toast.show();
 }

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.back:

        finish();
        return true;
    case R.id.clear:
        DBAdapter DB = new DBAdapter(this);
        DB.open();
        DB.deleteAll();
        startActivity(getIntent());
        finish();

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

@Override
public void onSwipe(int direction) {

    Intent intent = new Intent();

    switch (direction) {

    case SimpleGestureFilter.SWIPE_RIGHT:

        intent.setClass(this, Main.class);
        startActivity(intent);
        break;
    case SimpleGestureFilter.SWIPE_LEFT:

        intent.setClass(this, Entry.class);
        startActivity(intent);
        break;
    case SimpleGestureFilter.SWIPE_DOWN:

        break;
    case SimpleGestureFilter.SWIPE_UP:

        break;
    }



}

@Override
public boolean dispatchTouchEvent(MotionEvent me) {
    this.detector.onTouchEvent(me);
    return super.dispatchTouchEvent(me);
}

@Override
public void onDoubleTap() {
    // TODO Auto-generated method stub

}

@Override
public void onListItemClick(ListActivity l, View v, int position, long id) {
    // TODO Auto-generated method stub

}

}

This needs to be outside of your onCreate():

 @Override    // the error is with this method decleration
 protected void onListItemClick(ListView l, View v, int position, long id) { 

  super.onListItemClick(ls, v, position, id);

 }

You're creating the onListItemClick method inside the onCreate method. Move it outside the onCreate method :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM