如何在ListAdapter上显示更多信息?

时间:2014-08-05 15:56:24

标签: c# android listview xamarin listadapter

我有一个:

List<Fixture> allFixture = new List<Fixture>();

每个灯具:

 class Fixture
{
public int _id { get; set; }
public string Home { get; set; }
public DateTime matchDate { get; set; }
public string Away { get; set; }
}

我可以在ListAdapter上显示Home []:

ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, allFixture.Select(x => x.Home).ToArray())

但我希望在下面显示如下内容:“Home vs Away” DateTime 。 我怎么能这样做?

3 个答案:

答案 0 :(得分:1)

What Nicola linked提供了正确的信息。对于您明确要求的内容,您需要实施custom adapter并覆盖getView method

例如:

public class ExampleArrayAdapter extend ArrayAdapterAdapter<MyCustomItem>
{
    private Context mContext;
    private List<MyCustomItems> mCustomItems;

    public ExampleArrayAdapter(Context context, List<MyCustomItem> customItems)
    {
        mContext = context;
        mCustomItems = customItems;
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // Create the view of your row item here
        View rowView = inflater.inflate(R.layout.my_custom_layout, parent, false);
    }
}

public class MyActivity extends listActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        List<MyCustomItem> customItems = new ArrayList<MyCustomItems();
        // create your items...

        ExampleArrayAdapter arrayAdapter = new ExampleArrayAdapter(this, customItems);
        setListAdapter(arrayAdapter);
    }
}

创建您的Fixture布局并在getView中对其进行充气。

my_custom_layout:

<LinearLayout
    android:id="@+id/listItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Default"/>

    <TextView
        android:id="@+id/textDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="20140805"/>

</LinearLayout>

答案 1 :(得分:0)

尝试扩展ArrayAdapter以使用多个字符串。

你可以扩展它以使用你的类Fixture,然后再膨胀一个新的布局文件而不是simpleListItemLayout。

您需要一个新的xml文件,您将膨胀。

  1. 扩展arrayadapter

    private class SampleAdapter extends ArrayAdapter<Fixture> {
    
    public InteractiveArrayAdapter(Activity context, List<Model> list) {
    super(context, R.layout.rowbuttonlayout, list);
    this.context = context;
    this.list = list;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
    
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.YOUR_XML_LAYOUT, null);
        //find views that you want to access and populate here.
        return view;
    }
    }
    
  2. 创建一个新的xml文件,这可以是你想要的任何东西,在数组适配器中你会膨胀新的xml。

  3. 抱歉,我的格式全部关闭

答案 2 :(得分:0)

另请查看http://java.dzone.com/articles/android-listview-optimizations 使用viewHolder模式优化代码以实现平滑滚动。