MonoDroid的:我怎样才能填补从数据表的数据一个ListView我从SQL带来了什么? [英] Monodroid : how can I fill a ListView with data from a DataTable I bring from SQL?

查看:135
本文介绍了MonoDroid的:我怎样才能填补从数据表的数据一个ListView我从SQL带来了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从SQL Server查询的名称的DataTable。我想用这些名字来填充.axml文件中创建一个ListView。什么是处理这个的最佳方式?这是我到目前为止有:

I have a DataTable with names queried from SQL Server. I want to use those names to populate a ListView created in the .axml file. What would be the best way to handle this? This is what I have so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Data;

namespace MonoAndroidApplication3
{
    [Activity(Label = "care sheet list test")]
    public class caresheetlist : ListActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);


            DAL d = new DAL("Data Source=.;Initial Catalog=reptitips;Integrated Security=True");
            DataTable tbl = d.FillAndReturn("select ReptileCommonName from reptiles order by ReptileCommonName", null, null, null);

            string[] reptileNames = new string[tbl.Rows.Count];

            for (int i = 0; i < reptileNames.Length; i++)
            {
                reptileNames[i] = tbl.Rows[i]["ReptileCommonName"].ToString();
            }


            ListAdapter = new ArrayAdapter<string>(this, Resource.Layout.listitem, reptileNames);

            ListView.TextFilterEnabled = true;

            ListView.ItemClick += delegate(object sender, ItemEventArgs args)
            {
                // When clicked, show a toast with the TextView text
                Toast.MakeText(Application, ((TextView)args.View).Text, ToastLength.Short).Show();
            };

            //AutoCompleteTextView textView1 = (AutoCompleteTextView)FindViewById(autoComplete1);
            //ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, Resource.Layout.listitem, reptileNames);
        }
    }
}

和.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    android:orientation="vertical" >

  <LinearLayout
      android:id="@+id/linearLayout2"
      android:layout_width="match_parent"
      android:layout_height="44dp"
      android:orientation="vertical" >

    <AutoCompleteTextView
        android:id="@+id/autoComplete1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="AutoCompleteTextView" >
    </AutoCompleteTextView>
  </LinearLayout>

  <LinearLayout
      android:id="@+id/linearLayout1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1" >

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

      <ListView
          android:id="@+id/listView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1" >
      </ListView>

    </LinearLayout>

  </LinearLayout>

</LinearLayout>

但是,在上述code的结果是,它返回去到该应用程序的第一页,然后仿真器关闭。

However, the result of the above code is that it returns me to the first page of the application, and then the emulator shuts down.

Android设备记录显示这些错误

请帮助尽快

推荐答案

我现在知道该如何实现这个在C#中,但用java我想:

I'm now sure how to implement this on c# but using java I would:

listView.setOnItemClickListener(this);

放生活动类实现监听,然后你将需要添加的方法

Letting the activity class implement the listener and then you would need to add the method

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    Log.d(TAG, "onListItemClick: " + position);

    // you coce here
}

希望这有助于

这篇关于MonoDroid的:我怎样才能填补从数据表的数据一个ListView我从SQL带来了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆