【问题标题】:Create listview with button using Xamarin and SQL Server使用 Xamarin 和 SQL Server 创建带有按钮的列表视图
【发布时间】:2023-03-22 21:54:01
【问题描述】:

我创建了一个项目,我在其中从数据库中检索数据并将其显示到ListView 中。见下图。

这是为ListView检索数据的代码

public class MainActivity : Activity
{
  public static  Context context;
    public static List<UserInfo> UserInfoList = new List<UserInfo>();

    public static ListView ListView;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView (Resource.Layout.Main); 
        ListView = FindViewById<ListView>(Resource.Id.Listview);

        GetList list = new GetList();
        list.Execute();
    }
    public class GetList : AsyncTask
    { 
        Context con;
        protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
        {
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient( );

            var _WebApiUrl =  string.Format("URL");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage messge = client.GetAsync(_WebApiUrl).Result;
            var Return_EventList = messge.Content.ReadAsStringAsync().Result;
            var EventList = JsonConvert.DeserializeObject<List<UserInfo>>(Return_EventList);

            foreach (var data in EventList)
            {
                UserInfoList.Add(data);
            }
            return true;
        }
        protected override void OnPreExecute()
        {
            base.OnPreExecute();

        }
        protected override void OnPostExecute(Java.Lang.Object result)
        {
            base.OnPostExecute(result);
            ListView.Adapter = new UserInfoListAdapter(context, UserInfoList);
        }

    }


    class UserInfoListAdapter : BaseAdapter<UserInfo>
    {
        private List<UserInfo> mItem = new List<UserInfo>();
        private Context context;
        public UserInfoListAdapter(Context mcontext, List<UserInfo> mItems)
        {
            mItem.Clear();
            mItem = mItems;
            context = mcontext;
            this.NotifyDataSetChanged();

        }
        public override UserInfo this[int position]
        {
            get
            {
                return mItem[position];
            }
        }

        public override int Count
        {
            get
            {
                return mItem.Count;
            }
        }

        public Context MContext { get; private set; }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        { 
            View listitem = convertView; 
            listitem = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.ListViewDesign, parent, false);
            TextView TxtName = listitem.FindViewById<TextView>(Resource.Id.TxtName);
            TextView TxtNumber = listitem.FindViewById<TextView>(Resource.Id.TxtNumber);
            TxtName.Text = mItem[position].firstname;
            TxtNumber.Text = mItem[position].contact_no;
            listitem.Click += (object sender, EventArgs e) =>
            {
                Toast.MakeText(parent.Context, "Clicked " + mItem[position].firstname, ToastLength.Long).Show();
            };

            return listitem;
        }
    }

}

我想在ListView 中放置按钮,每行一个。这是我想如何实现它的示例

现在,当用户单击按钮时,应根据数据库中的当前 ID 下载 pdf 文件。例如在我的数据库xray id 是1,所以当用户下载xray 的pdf 时,它应该基于1 的id 下载

【问题讨论】:

    标签: c# xamarin sql-server-2012 asp.net-web-api2


    【解决方案1】:
    • 将 TableLayout 放置在您的“ListViewDesign”android 布局中,以便 获得所需的视图(或者它可以通过设置线性布局来实现 它的方向水平和它的 layout_weight)
    • 在第四列放置按钮
    • 从 GetView 方法中移除点击事件
    • GetView 方法中的访问按钮

      Button DownloadButton = view.FindViewbyId<Button>(Resource.Id.btn_download);
      DownloadButton.Click += delegate { DownloadFile( link + mItem[position].Id ) };
      
    • 添加命名空间

      using System.Net;
      using System.IO;
      using System.Text;
      
    • 在您的 listView 适配器类中添加一个新方法

      private void DownloadFile(string url)
      {
         var webClient = new WebClient();
         webClient.DownloadStringCompleted += (s, e) => {
         var text = e.Result; // get the downloaded text
         string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
         string localFilename = "downloaded.txt";
         string localPath = Path.Combine(documentsPath, localFilename);
         File.WriteAllText(localPath, text); // writes to local storage
      };
      var url = new Uri("http://xamarin.com");
      webClient.Encoding = Encoding.UTF8;
      webClient.DownloadStringAsync(url);
      RunOnUiThread(() => {
          Toast.MakeText(this, "Download Completed", ToastLength.Short).Show();
      });
      }
      

    【讨论】:

    • 假设如果我的数据库中有 pdf 文件,这段代码会根据 id 从数据库中下载 pdf 吗?
    • 不,您可以使用此代码从基于其 ID 的链接下载文件
    • 我会将pdf的路径保存在数据库中,它会从路径下载吗?
    • 是的,它将通过将链接传递给 DownloadFile 方法
    • { DownloadFile( **link** + mItem[position].Id ) };此链接来自哪里
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多