【问题标题】:ListView for downloading files in XamarinListView 用于在 Xamarin 中下载文件
【发布时间】:2017-05-03 10:40:35
【问题描述】:

这是我在 Xamarin for Android 中的第一个应用程序。我想从两个屏幕开始:

  • 第一个(主)屏幕有 3 个按钮:加载任务编号 1、加载任务编号 2、显示任务列表,
  • 按下第一个或第二个按钮后,项目将添加到第二个屏幕的列表中,
  • 按下第三个按钮后,将打开带有任务列表的第二个屏幕。

但我对 ListView 有疑问。例如:我想将第一个任务添加到 ListView,回到第一个屏幕,添加第二个任务(第一个任务应该已经在列表中),回到第一个屏幕,按钮显示第二个屏幕,两个任务应该已经存在。

主要活动:

public class MainActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        Button addButton1 = FindViewById<Button>(Resource.Id.AddButton1);
        Button addButton2 = FindViewById<Button>(Resource.Id.AddButton2);
        Button downloadsScreen = FindViewById<Button>(Resource.Id.DownloadsScreen);
        var intent = new Intent(this, typeof(Downloads));

        addButton1.Click += (object sender, EventArgs e) =>
        {
            intent.PutExtra("downloads", "wartosc1");
            StartActivity(intent);
        };

        addButton2.Click += (object sender, EventArgs e) =>
        {
            intent.PutExtra("downloads", "wartosc2");
            StartActivity(intent);
        };

        downloadsScreen.Click += (object sender, EventArgs e) =>
        {
            StartActivity(intent);
        };
    }
}

有任务的活动:

public class Downloads : ListActivity
{
    Dictionary<string, Task> zadania = new Dictionary<string, Task>();
    List<string> listaZadan = new List<string>();

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        ArrayAdapter<string> lista = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, listaZadan);
    }

    protected override void OnStart()
    {
        var download = Intent.GetStringExtra("downloads") ?? null;
        Task zadanie = new Task();
        EncryptAndDecrypt decryption = new EncryptAndDecrypt();
        zadanie = JsonConvert.DeserializeObject<Task>(decryption.Decrypt(download, "haslo"));
        if (!zadania.ContainsKey(zadanie.Name))
        {
            zadania.Add(zadanie.Name, zadanie);
            listaZadan.Add(zadanie.Name);
        }
        else
            new AlertDialog.Builder(this).SetMessage("Zadanie zostało już dodane do pobierania").Show();
    }
}

我该如何解决?这样的列表是显示下载文件的好方法吗?将来我想将 ProgressBar 添加到 ListView 中的每个项目。请不要发送教程的链接,我都看到了。我关心来自与它打交道的人的信息。

【问题讨论】:

    标签: c# android listview xamarin


    【解决方案1】:

    我该如何解决?这样的列表是显示下载文件的好方法吗?

    不知道为什么,当启动一个继承自 ListActivity 的新活动时,我有一个没有任何信息的异常,就像你在代码中所做的那样,所以我决定在其布局中使用带有 ListView 控件的普通 Activity例如:

    下载布局.axml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      <ListView android:id="@+id/listview"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:background="#00FF00"
                   android:layout_weight="1"
                   android:drawSelectorOnTop="false" />
    </LinearLayout>
    

    通常当你的Downloads继承自ListActivity时,我们需要设置ListAdapter,所以如果你没有得到和我一样的异常,你需要添加这样的代码:

    ListAdapter = lista;  
    

    DownloadsOnCreate 中。

    但是对于我的方法,我们需要先在DownloadsLayout中找到这个ListView,然后设置它的适配器,它们其实是一样的。

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.downloadslayout);
    
        ListView lv = FindViewById<ListView>(Resource.Id.listview);
        ArrayAdapter<string> lista = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, listaZadan);
        lv.Adapter = lista;
    }
    

    将来我想将 ProgressBar 添加到 ListView 中的每个项目。

    所以为了你以后的打算,我不认为在这里直接使用ArrayAdapter是一个好方法,我们可以自定义我们自己的适配器,同时你可以在GetView方法中自定义你的ListView单元格Adapter,例如:

    public class DownloadsAdapter : BaseAdapter<string>
    {
        private List<string> items = new List<string>();
        private Activity context;
    
        public DownloadsAdapter(Activity context, List<string> items) : base()
        {
            this.context = context;
            this.items = items;
        }
    
        public override string this[int position]
        {
            get { return items[position]; }
        }
    
        public override int Count
        {
            get { return items.Count; }
        }
    
        public override long GetItemId(int position)
        {
            return position;
        }
    
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView; // re-use an existing view, if one is available
            if (view == null)
            { // otherwise create a new one
                view = context.LayoutInflater.Inflate(Resource.Layout.downloadscell, null);
            }
            var pbar = view.FindViewById<ProgressBar>(Resource.Id.progressbar);
    
            var tv = view.FindViewById<TextView>(Resource.Id.tv);
            tv.Text = items[position];
    
            return view;
        }
    }
    

    例如downloadscell.axml 是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Small"
        android:layout_marginRight="5dp" />
      <TextView
          android:id="@+id/tv"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/loading" />
    </LinearLayout>
    

    通过检查您的代码,您似乎只想将字符串传递给每个项目,因此我在这里使用了BaseAdapter&lt;string&gt;,您可以根据需要更改为其他类型。最后在你的OnCreate:

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.downloadslayout);
    
        ListView lv = FindViewById<ListView>(Resource.Id.listview);
        var adapter = new DownloadsAdapter(this, listaZadan);
        lv.Adapter = adapter;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多