2015-07-22 56 views
0

請檢查此編輯的代碼..它的工作,但不顯示數據庫數據..檢查框越來越滴答,但其他2列r打印爲ID和日期..我的setviewvalue方法沒有得到什麼錯..設置複選框,如果數據庫表值是1 xamarin.android

protected override void OnCreate (Bundle SaveInstace) 
     { 
      base.OnCreate (SaveInstace); 
      SetContentView (Resource.Layout.ListView_layout); 
      ActionBar.SetDisplayHomeAsUpEnabled (true); 
      //Gets ListView object instance 
      Database sqldb1 = ((GlobalClass)this.Application).sqldb; 
      listItems = FindViewById<ListView> (Resource.Id.listItems); 

      GetCursorView(); 
     } 

     void GetCursorView() 
     { 
      Database sqldb1 = ((GlobalClass)this.Application).sqldb; 
      Android.Database.ICursor sqldb_cursor = sqldb1.GetRecordCursor(); 
      if (sqldb_cursor != null) 
      { 
       sqldb_cursor.MoveToFirst(); 
       string[] from = new string[] {"_id","date","Value"}; 
       int[] to = new int[] { 
        Resource.Id.ListRow1, 
        Resource.Id.ListRow2, 
        Resource.Id.ListRow3 

       }; 
        //Creates a SimplecursorAdapter for ListView object 
       SimpleCursorAdapter sqldb_adapter = new SimpleCursorAdapter (this, Resource.Layout.record_view, sqldb_cursor, from, to); 
       sqldb_adapter.ViewBinder = new MyCustomView(); 

       listItems.Adapter = sqldb_adapter; 
      } 
      else 
      { 

      } 
     } 

     public class MyCustomView:Java.Lang.Object, SimpleCursorAdapter.IViewBinder 
     { 

      public bool SetViewValue (View view, Android.Database.ICursor cursor, int i) 
      { 
       if (view.Id == Resource.Id.ListRow3) 

       { 
        // If the column is IS_STAR then we use custom view. 
        int is_val = cursor.GetInt (i); 
        CheckBox cb = (CheckBox) view; 
        if (is_val != 0) 
        { 
         // set the visibility of the view to GONE 

         cb.Checked = true; 
         return true; 
       } 
        else 
       { 
        return true; 
       } 
       // For others, we simply return false so that the default binding 
       // happens. 

      } 
       return true; 
     } 
    } 
+0

請人幫我 –

+0

看看這個,可能會有幫助 http://stackoverflow.com/ a/12883426 – RIYAZ

+0

感謝您的重播..我會通過它。 –

回答

0

看起來不錯,只是添加複選框點擊,看看。

public class MyCustomView : Java.Lang.Object, SimpleCursorAdapter.IViewBinder 
{ 

    public bool SetViewValue(View view, Android.Database.ICursor cursor, int i) 
    { 
     if (view.Id == Resource.Id.action_bar) 
     { 
      // If the column is IS_STAR then we use custom view. 
      int is_val = cursor.GetInt(i); 
      CheckBox cb = (CheckBox)view; 
      cb.Click += cb_Click; 
      if (is_val != 0) 
      { 
       // set the visibility of the view to GONE 
       cb.Checked = true; 
       return true; 
      } 
      else 
      { 
       // cb.Checked = false; //in case you want to make it (uncheck) 
       return true; 
      } 
      // For others, we simply return false so that the default binding 
      // happens. 

     } 
     return true; 
    } 

    void cb_Click(object sender, EventArgs e) 
    { 
     //Handle checkbox click because value will be cahnge while clicking on checkbox 
    } 
} 
+0

謝謝你的重播.....但我不想要複選框可點擊。複選框必須設置如果數據庫列3的值爲1,如果0沒有設置..我的代碼是做這個任務,但其他2列r不工作,如果我把這個代碼從數據庫中的數據不是訪問,而是它的打印行列標題作爲ID和日期 –

+0

我知道你正在使用操作欄..請告訴我 –

+0

你有你的解決方案嗎? @Sunita – RIYAZ

0

此代碼工作我謝謝... :)

public class MyCustomView:Java.Lang.Object, SimpleCursorAdapter.IViewBinder 
      { 

       public bool SetViewValue (View view, Android.Database.ICursor cursor, int i) 
       { 
        if (view.Id == Resource.Id.ListRow1) { 
         int val = cursor.GetInt (i); 
         TextView txt = (TextView)view; 
         txt.Text = val.ToString(); 
        } 
        if (view.Id == Resource.Id.ListRow2) { 
         string val1 = cursor.GetString (i); 
         TextView txt = (TextView)view; 
         txt.Text = val1; 
        } 
        if (view.Id == Resource.Id.ListRow3) 

        { 
         // If the column is IS_STAR then we use custom view. 
         int is_val = cursor.GetInt (i); 
         CheckBox cb = (CheckBox) view; 
         if (is_val != 0) 
         { 
          // set the visibility of the view to GONE 

          cb.Checked = true; 
          return true; 
        } 
         else 
        { 
          cb.Clickable = false; 
         return true; 
        } 
        // For others, we simply return false so that the default binding 
        // happens. 

       } 
        return true; 
      }