2

因此,当我在 Item上向左滑动时,我想得到一个TimePicker,当我向右滑动时,我想得到一个DatePicker。这是 MainActivity,希望你能得到答案;)

package com.example.dailyplanner;


    class MyDbHelper extends SQLiteOpenHelper {

        private static final String DB_NAME = "mydb";
        private static final int DB_VERSION = 1;

        public static final String TABLE_NAME = "TaskTbl";
        public static final String COL_TXT = "Text";
        public static final String COL_DATE = "Date";
        public static final String COL_TIME = "Time";
        private static final String STRING_CREATE = "CREATE TABLE " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " + COL_TXT + " VARCHAR(250), " + COL_DATE + " VARCHAR(20), " + COL_TIME + " VARCHAR(20));";


        public MyDbHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(STRING_CREATE);
            ContentValues cv = new ContentValues(2);
            cv.put(COL_TXT, "New Entry");
            db.insert(TABLE_NAME, null, cv);
        }


        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }


public class MainActivity extends ListActivity {

    public static final int RESULT_CODE = 1; //numri qe e percakton se me cfar kodi do percillet rezultati nga nje activity ne tjetren


    MyDbHelper mHelper;
    SQLiteDatabase mDb;
    Cursor mCursor;
    SimpleCursorAdapter mAdapter;
    Intent weather;
    Intent i;
    String text;
    String date;
    String time;
    ListView mList;
    Intent nTaskScreen;


    public TextView timeText;
    public TextView dateText;
    public EditText taskText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.RGBA_8888);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
        setContentView(R.layout.activity_main);

        timeText = (TextView) findViewById(R.id.timeTxt);
        dateText = (TextView) findViewById(R.id.dateTxt);
        taskText = (EditText) findViewById(R.id.editText1);

        mHelper = new MyDbHelper(getApplicationContext());

        Button showBtn = (Button) findViewById(R.id.shTask);
        showBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {


            }
        });


        Button nTaskBtn = (Button) findViewById(R.id.nTask);
        nTaskBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                nTaskScreen = new Intent(MainActivity.this, new_task.class);
                startActivityForResult(nTaskScreen, RESULT_CODE);

            }
        });

        Button wthrBtn = (Button) findViewById(R.id.wthrBtn);
        registerForContextMenu(wthrBtn);


        mList = (ListView) findViewById(id.list);

        // I implemented here the code from the Link

    }

    public void onResume() {
        super.onResume();
        mDb = mHelper.getWritableDatabase();
        String[] columns = new String[] {
            "_id", MyDbHelper.COL_TXT, MyDbHelper.COL_TIME
        };
        mCursor = mDb.query(MyDbHelper.TABLE_NAME, columns, null, null, null, null, null, null);
        String[] headers = new String[] {
            MyDbHelper.COL_TXT, MyDbHelper.COL_TIME
        };
        mAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,
            mCursor, headers, new int[] {
            android.R.id.text1, android.R.id.text2
        });
        mList.setAdapter(mAdapter);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_CODE) {

            String text = data.getStringExtra("text");
            String date = data.getStringExtra("date");
            String time = data.getStringExtra("time");

            Toast.makeText(this, "Registration was successfull! \nTask: " + text + "\nOn: " + date + "\nat:" + time, Toast.LENGTH_LONG).show();

            ContentValues cv = new ContentValues(2);
            cv.put(MyDbHelper.COL_TXT, text.toString());
            //cv.put(MyDbHelper.COL_DATE, date); //Insert 'now' as the date
            cv.put(MyDbHelper.COL_TIME, time);

            mDb.insert(MyDbHelper.TABLE_NAME, null, cv);
            mCursor.requery();
            mAdapter.notifyDataSetChanged();
            text = null;

            //mDb.execSQL("INSERT INTO TaskTbl (text,time,date) VALUES(" + "'" + text + "', '" + time + "', '" + date + "');");      

        }
        super.onActivityResult(requestCode, resultCode, data);

    }
}
4

1 回答 1

1

我为你做了一个样品

笔记:

  1. 如果您使用 ListActivity,那么 xml 中的 ListView 应该有 id android:id="@android:id/list"

  2. 检查我的示例代码并根据您的代码实现相同的代码。

  3. 在我评论过的地方调用 TimePicker 和 DatePicker。

` 公共类 MyListActivity 扩展 ListActivity 实现 OnTouchListener {

float historicX = Float.NaN, historicY = Float.NaN;
static final float TRIGGER_DELTA = 50; // Number of pixels to travel till
                                        // trigger
private GestureDetector gestureDetector;

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

    gestureDetector = new GestureDetector(new SwipeGestureDetector());

    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
            new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_ID }, null,
            null, null);
    startManagingCursor(cursor);

    // the desired columns to be bound
    String[] columns = new String[] {  ContactsContract.Contacts.DISPLAY_NAME,  ContactsContract.Contacts.PHOTO_ID };
    // the XML defined views which the data will be bound to
    int[] to = new int[] { R.id.name_entry, R.id.number_entry };

    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_example_entry, cursor, columns, to);
    setListAdapter(mAdapter);
    findViewById(android.R.id.list).setOnTouchListener(this);

}

public void onResume() {
    super.onResume();
}

enum Direction {
    LEFT, RIGHT;
}

interface OnSlideCompleteListener {
    void onSlideComplete(Direction dir);
}

@Override
public boolean onTouch(View v, MotionEvent event) {

    if (gestureDetector.onTouchEvent(event)) {
          return true;
        }
        return super.onTouchEvent(event);
}

public void onSlideComplete(Direction direction) {

    if (Direction.LEFT == direction) {
        Log.d("SWIPE LEFT", "LEFT");
        // Invoke Time Picker here
    } else if (direction == Direction.RIGHT) {
        Log.d("SWIPE RIGHT", "RIGHT");
        // Invoke Date Picker here
    }
}

// Private class for gestures
  private class SwipeGestureDetector extends SimpleOnGestureListener {
    // Swipe properties, you can change it to make the swipe 
    // longer or shorter and speed
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 200;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
      try {
        float diffAbs = Math.abs(e1.getY() - e2.getY());
        float diff = e1.getX() - e2.getX();

        if (diffAbs > SWIPE_MAX_OFF_PATH)
          return false;

        // Left swipe
        if (diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            onSlideComplete(Direction.LEFT);

        // Right swipe
        } else if (-diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            onSlideComplete(Direction.RIGHT);
        }
      } catch (Exception e) {
        Log.e("YourActivity", "Error on gestures");
      }
      return false;
    }
  }

} `

于 2013-04-17T12:56:50.003 回答