2017-05-03 74 views
0

這是我在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中的每個項目。請不要發送鏈接到教程,我看到了他們。我關心處理它的人的信息。

回答

1

我該如何解決?這樣的列表是顯示下載文件的好方法嗎?

不知道爲什麼,我有例外,沒有任何信息時,開始從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; 

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; 
} 

在未來,我想在ListView中添加進度到每個項目。

因此,對於你未來的打算,我不認爲使用ArrayAdapter直接是一個很好的方法,在這裏,我們可以自定義我們自己的適配器,在此同時您可以自定義ListView細胞中的AdapterGetView方法,只是舉例:

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; 
}