2013-04-15 70 views
-2

所以這個列表以這種方式工作,當我點擊該項目,該項目被刪除。所以我想要它,當我點擊該項目,它必須讓我看看TimeDialog(TimePicker)更改日期和時間。點擊列表視圖,並能夠得到一個時間Picker

mList = (ListView) findViewById(id.list); 
    mList.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View v, int position, long id)  { 
      mCursor.moveToPosition(position); 
      String rowId = mCursor.getString(0); //Column 0 of the cursor is the id 
      mDb.delete(MyDbHelper.TABLE_NAME, "_id = ?", new String[]{rowId}); 
      mCursor.requery(); 
      mAdapter.notifyDataSetChanged(); 

* 這裏是我的MainActivity,我認爲這將是有益的!!還包括列表視圖! *

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; 


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); 
    mList.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 


      int hourOfDay = 0, minute = 0; 




      TimePickerDialog tp = new TimePickerDialog(MainActivity.this, 
        mTimeSetListener, hourOfDay, minute, false); 
      tp.show(); 
     }; 


    TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { 

     public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
      // TODO Auto-generated method stub 
      timeText.setText("" + hourOfDay + "" + minute); 
     }; 

    }; 

    }); 
    } 


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

} 



} 
+0

想要刪除該項目還是要在列表項目單擊時顯示TimeDialog?是否可以更簡要地解釋您的需求? –

+0

當我點擊該項目時,我想向我展示TimeDialog.And,如果有可能將它保存在數據庫中。所以你想讓我展示你的項目嗎?我擁有的2個活動會更容易些嗎? –

回答

0

你可以通過意向的日期和時間數據,然後保存按鈕,點擊可以將數據保存到數據庫和保存數據的onResume以前的活動,你可以重新加載光標並重新加載數據到列表視圖後。並請說明你確切想要的。 你可以這樣說:

listview.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> l, View arg1, int arg2, 
       long id) { 
      int hourOfDay = 0, minute = 0; 


      TimePickerDialog tp = new TimePickerDialog(MainActivity.this, 
        mTimeListener, hourOfDay, minute, false); 
      tp.show(); 

     } 
    }); 

哪裏mTimeListener是如下:

TimePickerDialog.OnTimeSetListener maxtime = new OnTimeSetListener() { 

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     // TODO Auto-generated method stub 
     textview.setText("" + hourOfDay + "" + minute); 
    } 
}; 

希望它爲你工作...

+0

我希望它很簡單,你可以告訴我代碼。所以當我點擊列表中的項目時,我想讓我顯示TimeDialg(或TimePicker)來更改日期和時間。 –

+0

現在你可以參考答案.. @ Arjeta Ballazhi – AndiM

+0

我試過了,它顯示TimePicker,但是當我選擇時間,然後單擊設置,然後我會得到一個錯誤和應用程序關閉。任何想法?因爲我希望它也被存儲在我選擇的時間列表中。 –

1

如果要動態地添加布局的意見,你可以保存點擊的位置&然後可以設置文字

添加視圖到佈局

final View v = View.inflate(MainActivity.this,R.layout.row_timw,null); 
final TextView txTextView = (TextView) v.findViewById(R.id.time); 
EditText hour11 = (EditText)v.findViewById(R.id.hour11); 
EditText hour22 = (EditText)v.findViewById(R.id.hour22); 
EditText minute11 = (EditText)v.findViewById(R.id.minute11); 
EditText minute22 = (EditText)v.findViewById(R.id.minute22); 
ArrayListhour11.add(hour11); 
ArrayListhour22.add(hour22); 
ArrayListminute11.add(minute11); 
ArrayListminute22.add(minute22); 

然後保存clciked位置&然後在時間或日期設置中獲得值。

private TimePickerDialog.OnTimeSetListener timePickerListener1 = 
     new TimePickerDialog.OnTimeSetListener() { 
      public void onTimeSet(TimePicker view1, int selectedHour, 
            int selectedMinute) { 
       ArrayListhour11.get(clickedPos).setText(selectedHour+""); 
       ArrayListminute11.get(clickedPos).setText(selectedMinute+""); 
       showDialog(BOutTIME_DIALOG_ID); 
      } 
     }; 
相關問題