ListView用于在Xamarin中下载文件
这是我在Xamarin for Android中的第一个应用程序。我想与两个屏幕开始:ListView用于在Xamarin中下载文件
- 第一(主)屏幕具有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中的每个项目。请不要发送链接到教程,我看到了他们。我关心处理它的人的信息。
我该如何解决?这样的列表是显示下载文件的好方法吗?
不知道为什么,我有例外,没有任何信息时,开始从ListActivity
就像你在你的代码确实继承了一个新的活动,所以我决定用一个正常Activity
在其布局例如ListView
控制:
downloadslayout.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;
在Downloads
的OnCreate
。
但是对于我的方法,我们首先需要在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;
}
在未来,我想在ListView中添加进度到每个项目。
因此,对于你未来的打算,我不认为使用ArrayAdapter
直接是一个很好的方法,在这里,我们可以自定义我们自己的适配器,在此同时您可以自定义ListView
细胞中的Adapter
的GetView
方法,只是举例:
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>
用C hecking你的代码,你似乎只想传递字符串到每个项目,所以我在这里使用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;
}