2015-07-22 53 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; 
      }