2011-09-22 4 views
4
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
    this, 
    R.layout.mytaskslayout, 
    cursor, 
    new String[] {"Aircraft","Discrepancy","ARR_FLT"}, 
    new int[] {R.id.ac, R.id.discrepancy, R.id.arrinfo} 
); 

두 개 이상의 열을 결합하여 하나의 R.id.XXX에 표시 할 수 있나요? 그렇다면 어떻게?Android SimpleCursorAdapter로 두 개 이상의 열을 결합하는 방법

+1

귀하의 질문에 더 완전한 제목을 부여하십시오. – Cristian

+0

당신의 owwn 어댑터를 simplecursoradapter를 기반으로 만들고 getView를 오버라이드하십시오. 또는 thiz select tab1.column || 이름에서 tabx.column .... – Selvin

답변

4

이렇게하려면 SimpleCursorAdapter.ViewBinder을 만들 수 있습니다. 예를 들어 this answer을 참조하십시오.

기본적으로 ViewBinder이 각보기에 호출됩니다. 따라서 두 개의 열을 모두 가져오고 자하는 뷰로 이동하면 두 열 값을 모두 검색하여 결합하고 뷰의 값을 직접 설정하십시오.

0

마지막으로 해결책을 찾았습니다. 나는 안드로이드에 대한 새로운 누군가와 안드로이드에 대한 Xamarin과 같은 질문을 가지고 있었다. 분명히하기 위해 - 저는 Xamarin Studio를 사용하고 있습니다. Xamarin for Android는 C#으로 작성되었습니다.

SimpleCursorAdapter가 여러 필드를 수용 할 수있는 것처럼 보이지만 SimpleListItem과 같은 바인딩을 사용하면 하나의 필드 만 사용됩니다.

 string[] fromColumns = new string[]{ "checkListName","checkListDesc" }; 
     int[] toControlIDs = new int[] {Android.Resource.Id.Text1, Android.Resource.id.Text1}; 

     try{ 
      listView.Adapter = new SimpleCursorAdapter(this,Android.Resource.Layout.SimpleListItem1 , c, fromColumns, toControlIDs, 0);   
     } 
     catch(SQLiteException e){ 
      Console.WriteLine ("whoops, " + e.Message.ToString()); 
     } 

그러나 모든 내가 커서 행의 마지막 필드했다 가지고 :

는 내가 처음이 시도.

나는 사용자 지정 목록이 필요하다는 것을 알게되었습니다. 코드 파일이 네 개의 파일에 대해 다음과 같습니다

  1. MainActivity.cs는
  2. myList.xml
  3. Main.axml
  4. PreFloat.cs (이 데이터베이스의 일부)

내가 가진 가능한 한 실생활에 가깝게 가능한 완벽하게 작동하는 샘플을 제공하기 위해이 모든 것을 포함 시켰습니다. 여기

은 콘텐츠 뷰를 설정 MainActivity.cs 인 SQLite 데이터베이스에서 데이터를 얻을 수있는 커서를 사용하고 Main.axml 파일을 여기에

using System; 

using Android.App; 
using Android.Content; 
using Android.Database; 
using Android.Database.Sqlite; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 

namespace Darjeeling 
{ 
[Activity (Label = "Darjeeling", MainLauncher = true, Icon = "@drawable/icon")] 
public class MainActivity : Activity 
{  

    ListView listView; 
    Darjeeling.PreFloatDatabase pdb; 
    ICursor c; 
    protected override void OnCreate (Bundle bundle) 
    { 
     base.OnCreate (bundle); 

     // Set our view from the "main" layout resource 
     SetContentView (Resource.Layout.Main); 
     listView = FindViewById<ListView> (Resource.Id.listView1); 
     pdb = new PreFloatDatabase (this); 
     // Assign the cursor to a query 
     c = pdb.ReadableDatabase.RawQuery ("select * from checkLists", null); 
     StartManagingCursor (c); 
     // A ListView needs an adapter -- so we'll assign our instantiated listView's adapter to our customized adapter called HomeScreenCursorAdapter. 
     listView.Adapter = (IListAdapter)new HomeScreenCursorAdapter (this,c); 
    } 
    // End onCreate method 

    // This handles the cursor when the user is done with the activity 
    protected override void OnDestroy() 
    { 
     StopManagingCursor(c); 
     c.Close(); 
     base.OnDestroy(); 
    } 
    // Here's the magic -- 
    public class HomeScreenCursorAdapter : CursorAdapter { 
     Activity context; 
     public HomeScreenCursorAdapter(Activity context, ICursor c) 
      : base (context, c) 
     { 
      this.context = context; 
     } 
     // This overridden BindView method is going to let me assign TextView controls that I've set up in an XML file, to specific fields in the cursor. 
     public override void BindView(View view, Context context, ICursor cursor) 
     { 
      var txtCheckListName = view.FindViewById<TextView> (Resource.Id.txtCheckListName); //(Android.Resource.Id.Text1); 
      var txtCheckListDesc = view.FindViewById<TextView> (Resource.Id.txtCheckListDesc); //(Android.Resource.Id.Text2); 
// For testing purposes, I first assigned static values to txtCheckListName and txtCheckListDesc, for instance, txtCheckListName.Text = "Hello"; and txtCheckListDesc.Text = "World"; 
      txtCheckListName.Text = cursor.GetString (3); 
      txtCheckListDesc.Text = cursor.GetString (4); 
     } 
     // This overridden View inflates each row (I think). This could inflate a built-in ListView control like SimpleListItem, OR, in this case, it references a custom written XML file called myList. 
     public override View NewView(Context context, ICursor cursor, ViewGroup parent) 
     { 
      return this.context.LayoutInflater.Inflate (Resource.Layout.myList, parent, false); 
     } 
    } 
} 

}

을 정의입니다 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/linearLayout1" 
android:minWidth="25px" 
android:minHeight="25px"> 
<ListView 
    android:minWidth="25px" 
    android:minHeight="25px" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/listView1" /> 
</LinearLayout> 

그리고 마지막으로, 기본적으로 ListView에 행의 정의입니다 myList.xml 파일 :

,691

using System; 
using Android.Database.Sqlite; 
using Android.Content; 

namespace Darjeeling { 
class PreFloatDatabase : SQLiteOpenHelper { 
    public static readonly string create_checkLists_table = "create table if not exists checkLists([_id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, checkListID INTEGER, checkListType INTEGER, checkListName TEXT, checkListDesc TEXT);"; 
    public static readonly string DatabaseName = "prefloat.db"; 
    public static readonly int DatabaseVersion = 1; 
    public PreFloatDatabase (Context context) : base (context, DatabaseName, null, DatabaseVersion){  } 

    public override void OnCreate(SQLiteDatabase db){ 
     // fire the statement that creates the checkLists table 
     try{ 
     db.ExecSQL (create_checkLists_table);   
     // Now pre-fill the checkLists table 
      db.ExecSQL ("insert into checkLists (checkListID, checkListType, checkListName, checkListDesc) values (0, 0, 'Widgeon','Widgeon Daysailer');"); 
      db.ExecSQL ("insert into checkLists (checkListID, checkListType, checkListName, checkListDesc) values (1, 1, 'Widgeon','Widgeon Daysailer');"); 
      db.ExecSQL ("insert into checkLists (checkListID, checkListType, checkListName, checkListDesc) values (2, 0, 'Bowrider', 'Mo Motor, Mo Fun');"); 
      db.ExecSQL ("insert into checkLists (checkListID, checkListType, checkListName, checkListDesc) values (3, 1, 'Bowrider', 'Mo Motor, Mo Fun');"); 
      db.ExecSQL ("insert into checkLists (checkListID, checkListType, checkListName, checkListDesc) values (4, 0, 'HobieCat','Hang yer ass out fun');"); 
      db.ExecSQL ("insert into checkLists (checkListID, checkListType, checkListName, checkListDesc) values (5, 1, 'HobieCat','Hang yer ass out fun');"); 
     } 
     catch(SQLiteException e){ 
      Console.WriteLine ("Problem with the database " + e.Message.ToString()); 
     } 

    } 
    public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ 
     throw new NotImplementedException(); 
    } 

} // matches with class PreFloatDatabase 
} // matches with namespace Darjeeling 

질문 : SHOULD SimpleListItem 제어 이 체크 박스 컨트롤과 레이블을 하나의 필드 이상 또는 대부분 더이 363,210

<?xml version="1.0" encoding="utf-8"?> 
<!---<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="200dp" 
      android:layout_height="match_parent" 
      android:background="#FF0000FF"> 
--> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/linearLayout1" 
android:minWidth="25px" 
android:minHeight="25px"> 
<TextView 
    android:id="@+id/txtCheckListName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="" /> 
<TextView 
    android:id="@+id/txtCheckListDesc" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0" 
    android:text="" /> 
</LinearLayout> 
<!---</FrameLayout>--> 

그리고 여기에 데이터베이스 파일입니까? 대신 그리드 컨트롤을 사용해야합니까?

대안 : 이러한 모든 속임수를 사용하는 대신 SQL을 사용하여 필요한 값을 간단하게 연결하는 것이 더 쉽지 않습니까? 특히 두 텍스트 컨트롤의 위치를 ​​조정하는 것은 너무 복잡 할 수 있습니다.

전체 공개 : 이 코드는 다른 게시물과 포럼에서 읽은 코드로, 내 자신의 특정 요구 사항에 맞게 사용자 정의 할 수 있습니다.

관련 문제