ListView用于在Xamarin中下载文件



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

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

但是我对ListView有问题。例如:我想将第一个任务添加到listView,返回第一个屏幕,添加第二个任务(第一个任务已经在列表中(,返回第一个屏幕,显示第二个屏幕的按钮,两个任务应该已经存在。p>主要活动:

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中的每个项目。请不要发送指向教程的链接,我看到了所有内容。我关心处理该信息的人的信息。

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

不确定为什么,当启动像您在代码中那样从ListActivity继承的新活动时,我有例外,没有任何信息,因此我决定在其布局中使用带有ListView控件的普通Activity,例如:

下载slayout.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>

通常,当您的DownloadsListActivity继承时,我们需要设置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是一种很好的方法,我们可以自定义自己的适配器,同时您可以在AdapterGetView中自定义ListView单元格,例如:

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<string>,可以根据需要更改为其他类型。最后在您的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;
}

最新更新