私が覚えていることを説明するために、非常に初歩的なサンプルプロジェクトは、forforループを使用してHello Worldの番号を表示するよりも、数字の入力を促すようなものになります。アダプターを使ってこれを達成することは可能でしょうか、あるいはより良い選択肢を探すべきでしょうか?
はい、アダプターを使用してこれを実行できます。しかし、デフォルト以外のアダプタではなく、以下のようなカスタムアダプタ付き:
public class MyListAdapter : Java.Lang.Object,IListAdapter
{
public int Count {
get {
//list view will show 10 lines of data
return 10;
}
}
public bool HasStableIds { get { return false; } }
public bool IsEmpty { get { return false; } }
public int ViewTypeCount { get { return 1; } }
private Context context;
private string uniqueData;
//default constructor
public MyListAdapter()
{ }
//constructor with current context and the data you want to show in your listview
public MyListAdapter(Context c,string data)
{
context = c;
uniqueData = data;
}
public bool AreAllItemsEnabled()
{
return true;
}
public void Dispose()
{
this.Dispose();
}
public Java.Lang.Object GetItem(int position)
{
throw new NotImplementedException();
}
public long GetItemId(int position)
{
return 0;
}
public int GetItemViewType(int position)
{
return 1;
}
public View GetView(int position, View convertView, ViewGroup parent)
{
View et=convertView;
if (et == null)
{
et = new TextView(context);
(et as TextView).Text = uniqueData;
}
return et;
}
public bool IsEnabled(int position)
{
return true;
}
public void RegisterDataSetObserver(DataSetObserver observer)
{
}
public void UnregisterDataSetObserver(DataSetObserver observer)
{
}
}
あなたが見ることができるように、文字列データをとる、あなたのリストビューのループ数を返し、GetView
に私はプログラム的TextView
を作成しCount.get
コンストラクタを渡します。
こうして、私は以下のようにそれを使用することができます:
public class MainActivity : Activity
{
ListView myListView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
myListView = FindViewById<ListView>(Resource.Id.MyListView);
myListView.Adapter=new MyListAdapter(this,"this is my custom data");
}
}
ありがとうございました、これはきちんと見えます!いくつかのUI要素がある場合、これに似た何かを使うことも同様に可能です。この場合、データ面はコレクション(配列やリストなど)になり、UIにはTextViewやButtonがいくつかあります。 – rTECH
複数のUI要素がある場合は、オーバーライドされた 'GetView'メソッドを変更する必要があります。 [このドキュメント](http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown)を参照してください。それは 'IListAdapter'を使用しませんでした。しかし、その考え方は同じです。 –