2013-06-18 72 views
0
CalendarView.java 


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.GregorianCalendar; 
import java.util.Locale; 
import android.app.Activity; 
import android.database.Cursor;  
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.GridView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

public class CalendarView extends Activity { 
    DatabaseHandler db; 

    TextView t1, t2, t3, t4, t5, t6, t7, t8, t9, t10; 
    public GregorianCalendar month, itemmonth;// calendar instances. 

    public CalendarAdapter adapter;// adapter instance 
    public Handler handler;// for grabbing some event values for showing the dot 
          // marker. 
    public ArrayList<String> items; // container to store calendar items which 
    String selectedGridDate;        // needs showing the event marker 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.calendar); 

     t1 = (TextView) findViewById(R.id.textv1); 
     t2 = (TextView) findViewById(R.id.textv2); 
     t3 = (TextView) findViewById(R.id.textv3); 
     t4 = (TextView) findViewById(R.id.textv4); 
     t5 = (TextView) findViewById(R.id.textv5); 
     t6 = (TextView) findViewById(R.id.textv6); 
     t7 = (TextView) findViewById(R.id.textv7); 
     t8 = (TextView) findViewById(R.id.textv8); 
     t9 = (TextView) findViewById(R.id.textv9); 
     t10 = (TextView) findViewById(R.id.textv10); 

     db = new DatabaseHandler(this); 
    enter code here 
     Locale.setDefault(Locale.US); 
     month = (GregorianCalendar) GregorianCalendar.getInstance(); 
     itemmonth = (GregorianCalendar) month.clone(); 

     items = new ArrayList<String>(); 
     adapter = new CalendarAdapter(this, month); 

     GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(adapter); 

     handler = new Handler(); 
     handler.post(calendarUpdater); 

     TextView title = (TextView) findViewById(R.id.title); 
     title.setText(android.text.format.DateFormat.format("MMMM yyyy", month)); 

     RelativeLayout previous = (RelativeLayout) findViewById(R.id.previous); 

     previous.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       setPreviousMonth(); 
       refreshCalendar(); 
      } 
     }); 

     RelativeLayout next = (RelativeLayout) findViewById(R.id.next); 
     next.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       setNextMonth(); 
       refreshCalendar(); 

      } 
     }); 

     gridview.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View v, 
        int position, long id) { 

       ((CalendarAdapter) parent.getAdapter()).setSelected(v); 
       selectedGridDate = CalendarAdapter.dayString 
         .get(position); 
       String[] separatedTime = selectedGridDate.split("-"); 
       String gridvalueString = separatedTime[2].replaceFirst("^0*", 
         "");// taking last part of date. ie; 2 from 2012-12-02. 
       int gridvalue = Integer.parseInt(gridvalueString); 
       // navigate to next or previous month on clicking offdays. 
       if ((gridvalue > 10) && (position < 8)) { 
        setPreviousMonth(); 
        refreshCalendar(); 
       } else if ((gridvalue < 7) && (position > 28)) { 
        setNextMonth(); 
        refreshCalendar(); 
       } 
       ((CalendarAdapter) parent.getAdapter()).setSelected(v); 
       Log.e("selectedGridDate..", ""+selectedGridDate); 
       showToast(selectedGridDate); 

       Contact con=db.getContact(selectedGridDate); 
       Log.e("con", ""+con); 
       String log1 = con.getC1NAME(); 
       Log.e("log1", log1); 
       t1.setText(log1); 
       String log2 = con.getC1NAME(); 
       Log.e("log2", log2); 
       t2.setText(log2); 
       String log3 = con.getC1NAME(); 
       t3.setText(log3); 
       String log4 = con.getC1NAME(); 
       t4.setText(log4); 
       String log5 = con.getC1NAME(); 
       t5.setText(log5); 
       String log6 = con.getC1NAME(); 
       t6.setText(log6); 
       String log7 = con.getC1NAME(); 
       t7.setText(log7); 
       String log8 = con.getC1NAME(); 
       t8.setText(log8); 
       String log9 = con.getC1NAME(); 
       t9.setText(log9); 
       String log10 = con.getC1NAME(); 
       t10.setText(log10); 
      } 
     }); 


    } 

    protected void setNextMonth() { 
     if (month.get(GregorianCalendar.MONTH) == month 
       .getActualMaximum(GregorianCalendar.MONTH)) { 
      month.set((month.get(GregorianCalendar.YEAR) + 1), 
        month.getActualMinimum(GregorianCalendar.MONTH), 1); 
     } else { 
      month.set(GregorianCalendar.MONTH, 
        month.get(GregorianCalendar.MONTH) + 1); 
     } 

    } 

    protected void setPreviousMonth() { 
     if (month.get(GregorianCalendar.MONTH) == month 
       .getActualMinimum(GregorianCalendar.MONTH)) { 
      month.set((month.get(GregorianCalendar.YEAR) - 1), 
        month.getActualMaximum(GregorianCalendar.MONTH), 1); 
     } else { 
      month.set(GregorianCalendar.MONTH, 
        month.get(GregorianCalendar.MONTH) - 1); 
     } 

    } 

    protected void showToast(String string) { 
     Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); 
    } 

    public void refreshCalendar() { 
     TextView title = (TextView) findViewById(R.id.title); 

     adapter.refreshDays(); 
     adapter.notifyDataSetChanged(); 
     handler.post(calendarUpdater); // generate some calendar items 

     title.setText(android.text.format.DateFormat.format("MMMM yyyy", month)); 
    } 

    public Runnable calendarUpdater = new Runnable() { 

     public void run() { 
      items.clear(); 

      // Print dates of the current week 
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US); 

      for (int i = 0; i < 7; i++) { 
       String itemvalue; 
       itemvalue = df.format(itemmonth.getTime()); 
       itemmonth.add(GregorianCalendar.DATE, 1); 
       items.add("2012-09-12"); 
       items.add("2012-10-07"); 
       items.add("2012-10-15"); 
       items.add("2012-10-20"); 
       items.add("2012-11-30"); 
       items.add("2012-11-28"); 
      } 

      adapter.setItems(items); 
      adapter.notifyDataSetChanged(); 
     } 
    }; 

} 




My Database is.... 


import java.util.ArrayList; 
import java.util.List; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DatabaseHandler extends SQLiteOpenHelper { 
    public static final int DATABASE_VERSION = 1; 
    public static final String DATABASE_NAME = "contactsManager.db"; 
    public static final String TABLE_NAME = "contacts"; 

    public static final String KEY_DT = "date"; 
    public static final String KEY_C1 = "c1"; 
    public static final String KEY_C2 = "c2"; 
    public static final String KEY_C3 = "c3"; 
    public static final String KEY_C4 = "c4"; 
    public static final String KEY_C5 = "c5"; 
    public static final String KEY_C6 = "c6"; 
    public static final String KEY_C7 = "c7"; 
    public static final String KEY_C8 = "c8"; 
    public static final String KEY_C9 = "c9"; 
    public static final String KEY_C10 = "c10"; 

    public DatabaseHandler(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String Create_Table = "CREATE TABLE " + TABLE_NAME + "(" + KEY_DT 
       + " TEXT, " + KEY_C1 + " TEXT, " + KEY_C2 + " TEXT, " + KEY_C3 
       + " TEXT, " + KEY_C4 + " TEXT, " + KEY_C5 + " TEXT, " + KEY_C6 
       + " TEXT, " + KEY_C7 + " TEXT, " + KEY_C8 + " TEXT, " + KEY_C9 
       + " TEXT, " + KEY_C10 + " TEXT " + ")"; 
     db.execSQL(Create_Table); 
    } 

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

    public void addContact(Contact contact) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues cv = new ContentValues(); 
     cv.put(KEY_C1, contact.getC1NAME()); 
     cv.put(KEY_C2, contact.getC2NAME()); 
     cv.put(KEY_C3, contact.getC3NAME()); 
     cv.put(KEY_C4, contact.getC4NAME()); 
     cv.put(KEY_C5, contact.getC5NAME()); 
     cv.put(KEY_C6, contact.getC6NAME()); 
     cv.put(KEY_C7, contact.getC7NAME()); 
     cv.put(KEY_C8, contact.getC8NAME()); 
     cv.put(KEY_C9, contact.getC9NAME()); 
     cv.put(KEY_C10, contact.getC10NAME()); 
     db.insert(TABLE_NAME, null, cv); 
     db.close(); 
    } 

    public Contact getContact(String _date) { 
     Log.e("_date", "" + _date); 
     SQLiteDatabase db = this.getReadableDatabase(); 

     Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_DT, KEY_C1, 
       KEY_C2, KEY_C3, KEY_C4, KEY_C5, KEY_C6, KEY_C7, KEY_C8, KEY_C9, 
       KEY_C10 }, KEY_DT + "='" + _date + "'", null, null, null, null); 

     /*Cursor cursor = db.query(TABLE_NAME, new String[] { KEY_DT, KEY_C1, 
       KEY_C2, KEY_C3, KEY_C4, KEY_C5, KEY_C6, KEY_C7, KEY_C8, KEY_C9, 
       KEY_C10 }, KEY_DT + "=?", new String[] { _date }, null, null, 
       null, null);*/ 
     Log.e("cursor", cursor.getCount() + ""); 
     if (cursor != null) 
      Log.e("cursor", cursor + ""); 
     Log.e("cursor1", cursor.moveToFirst() + ""); 

     cursor.moveToFirst(); 
     Log.e("cursor2", cursor.moveToFirst() + ""); 

     Contact contact = new Contact(cursor.getString(0), cursor.getString(1), 
       cursor.getString(2), cursor.getString(3), cursor.getString(4), 
       cursor.getString(5), cursor.getString(6), cursor.getString(7), 
       cursor.getString(8), cursor.getString(9), cursor.getString(10)); 
     db.close(); 
     return contact; 
    } 

    public List<Contact> getAllContacts() { 
     List<Contact> contactList = new ArrayList<Contact>(); 

     String selectQuery = "SELECT * FROM " + TABLE_NAME; 

     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     if (cursor.moveToFirst()) { 
      do { 
       Contact contact = new Contact(); 
       contact.setDATE(cursor.getString(0)); 
       contact.setC1NAME(cursor.getString(1)); 
       contact.setC2NAME(cursor.getString(2)); 
       contact.setC3NAME(cursor.getString(3)); 
       contact.setC4NAME(cursor.getString(4)); 
       contact.setC5NAME(cursor.getString(5)); 
       contact.setC6NAME(cursor.getString(6)); 
       contact.setC7NAME(cursor.getString(7)); 
       contact.setC8NAME(cursor.getString(8)); 
       contact.setC9NAME(cursor.getString(9)); 
       contact.setC10NAME(cursor.getString(10)); 

       contactList.add(contact); 
      } while (cursor.moveToNext()); 
     } 

     db.close(); 
     return contactList; 
    } 

    public int updateContact(Contact contact) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_C1, contact.getC1NAME()); 
     values.put(KEY_C2, contact.getC2NAME()); 
     values.put(KEY_C3, contact.getC3NAME()); 
     values.put(KEY_C4, contact.getC4NAME()); 
     values.put(KEY_C5, contact.getC5NAME()); 
     values.put(KEY_C6, contact.getC6NAME()); 
     values.put(KEY_C7, contact.getC7NAME()); 
     values.put(KEY_C8, contact.getC8NAME()); 
     values.put(KEY_C9, contact.getC9NAME()); 
     values.put(KEY_C10, contact.getC10NAME()); 

     return db.update(TABLE_NAME, values, KEY_DT + " = ?", 
       new String[] { contact.getDATE() }); 
    } 

    public void deleteContact(Contact contact) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     db.delete(TABLE_NAME, KEY_DT + " = ?", 
       new String[] { contact.getDATE() }); 
     db.close(); 
    } 

    public int getContactsCount() { 
     String countQuery = "SELECT * FROM " + TABLE_NAME; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(countQuery, null); 
     cursor.close(); 

     db.close(); 
     return cursor.getCount(); 
    } 
} 

我想獲取所有項的行(日期,c1,c2,c3,.......,c10)在每個日期點擊,但有光標空例外.ie數據庫沒有任何記錄。我在資產文件夾中插入了contactsManager.db。當我檢查文件資源管理器data/data/...中的contactsManager.db,並且我在sqlitedatabase瀏覽器中打開此文件時,顯示空數據庫。如何解決android.database.CursorIndexOutOfBoundException索引0請求,大小爲0

My Logcat is 

06-18 18:09:05.746: D/dalvikvm(29525): GC_EXTERNAL_ALLOC freed 51K, 53% free 2554K/5379K, external 1625K/2137K, paused 54ms 
    06-18 18:09:08.436: D/dalvikvm(29525): GC_EXTERNAL_ALLOC freed 8K, 53% free 2575K/5379K, external 4209K/5256K, paused 111ms 
    06-18 18:09:12.197: E/selectedGridDate..(29525): 2013-06-12 
    06-18 18:09:12.326: E/con(29525): [email protected] 
    06-18 18:09:12.326: E/con...(29525): false 
    06-18 18:09:12.326: E/first(29525): first 
    06-18 18:09:12.336: E/first1(29525): first1 
    06-18 18:09:12.350: E/hellllllll(29525): hello 
    06-18 18:09:12.350: D/AndroidRuntime(29525): Shutting down VM 
    06-18 18:09:12.356: W/dalvikvm(29525): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): FATAL EXCEPTION: main 
    06-18 18:09:12.386: E/AndroidRuntime(29525): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at com.para.digital.technologies.panchang2013.CalendarView$4.onItemClick(CalendarView.java:122) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at android.os.Handler.handleCallback(Handler.java:587) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at android.os.Handler.dispatchMessage(Handler.java:92) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at android.os.Looper.loop(Looper.java:123) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at android.app.ActivityThread.main(ActivityThread.java:3683) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at java.lang.reflect.Method.invokeNative(Native Method) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at java.lang.reflect.Method.invoke(Method.java:507) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
    06-18 18:09:12.386: E/AndroidRuntime(29525): at dalvik.system.NativeStart.main(Native Method) 
    06-18 18:14:12.459: I/Process(29525): Sending signal. PID: 29525 SIG: 9 

    i want to get single row items,but it gives cursor is 0/empty .i do not understand, what should do i . 
    i am new for android developer ,please help me 
    THANKS in Advance.... 
+0

你能否澄清哪一行是'CalendarView.java'的第122行 - 它看起來就像那是錯誤所在。如果你還沒有發佈源代碼,那麼你可能會在這一點上發表幾行文章。 –

回答

2

好像你正試圖讀取Cursor這是空的。嘗試返回它如下:

if (cursor.moveToFirst()) 
    return cursor; 
else 
    return null; 
+0

是的,遊標是空的,cursor.moveToFirst()是假的,但我插入資產文件夾中的數據庫文件。爲什麼顯示空遊標。 – Navdeep

+0

請幫我先生 – Navdeep

+0

首先你需要將數據庫從assets文件夾複製到內部/外部存儲位置,然後從該路徑打開數據庫並最終執行你的查詢。還要確保你的查詢不會返回空結果 – waqaslam

相關問題