Dies ist meine erste App in Xamarin. Hauptmenü: Update Textansicht und Fortschrittsbalken in Listview in Xamarin
Wenn ich auf die erste oder zweite Schaltfläche klicke, wird das Element zur Listenansicht hinzugefügt. Dritte Schaltfläche offen Listenansicht:
Mein 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;
if (view == null)
view = context.LayoutInflater.Inflate(Resource.Layout.DownloadsCell, null);
var name = view.FindViewById<TextView>(Resource.Id.NameInList);
var status = view.FindViewById<TextView>(Resource.Id.StatusInList);
var pbar = view.FindViewById<ProgressBar>(Resource.Id.ProgressBarInList);
name.Text = items[position];
//status.Text = itemsStatus[position];
return view;
}
}
Meine Haupttätigkeit:
public class MainActivity : Activity
{
List<string> downloads = new List<string>();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button loadHashcode = FindViewById<Button>(Resource.Id.LoadHashcode);
Button loadHashcode2 = FindViewById<Button>(Resource.Id.LoadHashcode2);
Button downloadsScreen = FindViewById<Button>(Resource.Id.DownloadsScreen);
loadHashcode.Click += (object sender, EventArgs e) =>
{
downloads.Add("firstItem");
};
loadHashcode2.Click += (object sender, EventArgs e) =>
{
downloads.Add("secondItem");
};
downloadsScreen.Click += (object sender, EventArgs e) =>
{
var intent = new Intent(this, typeof(Downloads));
intent.PutStringArrayListExtra("downloads", downloads);
StartActivity(intent);
};
}
}
Mein Download-Aktivität:
public class Downloads : Activity
{
Dictionary<string, Accounts> accountsList = new Dictionary<string, Accounts>();
Dictionary<string, Task> zadania = new Dictionary<string, Task>();
List<string> listaNazwZadan = new List<string>();
DownloadsAdapter adapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//commented code for getting accountsList from server
new AlertDialog.Builder(this).SetMessage("Getting file completed").Show();
IList<string> listWithDownloads = Intent.Extras.GetStringArrayList("downloads") ?? new string[0];
foreach (var download in listWithDownloads)
{
//commented code for getting zadanie
zadania.Add(zadanie.Name, zadanie);
listaNazwZadan.Add(zadanie.Name);
}
SetContentView(Resource.Layout.DownloadsLayout);
ListView lv = FindViewById<ListView>(Resource.Id.ListWithTasks);
adapter = new DownloadsAdapter(this, listaNazwZadan);
lv.Adapter = adapter;
lv.ItemLongClick += LvItemLongClick;
}
private void LvItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
{
Task zzadanie = new Task();
zzadanie = zadania.FirstOrDefault(x => x.Key == listaNazwZadan[e.Position]).Value;
MakeDownload(zzadanie.Name);
}
private void MakeDownload(string nazwa)
{
Task zzadanie = new Task();
zzadanie = zadania.FirstOrDefault(x => x.Key == nazwa).Value;
//commented code for getting account
//here I want set "Small Text" for clicked item to "Connecting" and progressbar to 33%
client.Connect(account.ImapHost, account.ImapPort, (account.ImapSsl == true) ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.None);
//here I want set "Small Text" for clicked item to "Login" and progressbar to 66%
client.Authenticate(zzadanie.Receivers.Keys.ElementAt(0), zzadanie.Receivers.Values.ElementAt(0));
//here I want set "Small Text" for clicked item to "Done" and progressbar to 100%
client.Disconnect(true);
}
}
Ich habe zwei Probleme:
- Ich möchte Textansicht und Fortschrittsbalken (kommentierte Code in
MakeDownload
Funktion) aktualisieren. - Wenn ich die dritte Taste drücke, gehe ich zurück zum Hauptmenü und als nächstes drücke ich erneut die dritte Taste, ich bekomme wieder die Meldung "Datei fertig". Download
accountsList
vom Server dauert eine lange Zeit und ich möchte es einmal herunterladen und zum Schließen der Anwendung verwenden.
Vielen Dank im Voraus für die Hilfe :)
@ gnade-feng-msft Danke für die Antwort. Das Aktualisieren von Textansicht und Fortschrittsbalken funktioniert perfekt. _Ich verstehe das nicht ruhig, du willst nur herunterladen, wenn deine App geschlossen ist? ** Generell: ** Sagen wir, ich habe einen Gegenstand mit Fortschritt, zB. 50%. Wenn ich vom Bildschirm mit der Liste zum Hauptmenü in meiner App zurückkomme und als nächstes die dritte Taste erneut drücke, wird Fortschrittsbalken und Status "zurückgesetzt"; Auch die Funktion zum Abrufen von accountsList vom Server wird erneut aufgerufen. Das ist mein Problem - wie kann ich die "Downloads" -Aktivität einmal aufrufen? Ich hoffe ich habe das klar erklärt :) –
Soll ich hier gebundene Dienste nutzen? –
@ DamianOS.MP5, ist der Download-Status von Ihrer Seite zugänglich? Wenn ja, wenn Sie Ihre Download-Seite öffnen und wenn Sie die Download-Datei zur Liste hinzufügen möchten, überprüfen Sie zuerst den Download-Status. –