相关Android中的数据和ListView [英] Relate data and ListView in Android
问题描述
我的情况是这样的:
在我的视图 Highscore_zeigen
从来就得到了一个的ArrayList< Rangliste>
,我想展示。整个事情是 onPostExecute
试图从一个URL获取JSON数据的一个线程。
In my View Highscore_zeigen
I´ve got an ArrayList<Rangliste>
that I want to show. The whole thing is in onPostExecute
of a thread that tries to get JSON data from an URL.
ArrayList<String> gewinnernamen = new ArrayList<String>();
ArrayList<String> gewinnerdatum = new ArrayList<String>();
Rangliste r = new Rangliste("","");
ArrayList<Rangliste> rangliste = new ArrayList<Rangliste>();
...
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
if(isOnline() == true){
System.out.println("HIHIHIHIHHIHI " +result);
super.onPostExecute(result);
JSONArray jsonarray = null;
try {
jsonarray = new JSONArray(result);
} catch (JSONException e) {
Toast.makeText(Highscore_zeigen.this, "Ups! Bitte überprüfe deine Netzwerkverbindung! ", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
String namest = "";
String datumst ="";
try {
jb = (JSONObject) jsonarray.getJSONObject(0);
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
for(int i = 0; i <5;i++){
try {
jb = (JSONObject) jsonarray.getJSONObject(i);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
namest = jb.getString("name");
datumst = jb.getString("datum");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gewinnernamen.add(i, namest);
gewinnerdatum.add(i, datumst);
adapter.notifyDataSetChanged();
}
for(int is = 0; is<5; is++){
r = new Rangliste(gewinnernamen.get(is), gewinnerdatum.get(is));
rangliste.add(r);
}
所以现在我要显示的数据是 rangliste
。一切工作正常,但我不知道如何表达我的的ListView
我的数据。 ListView控件是Highscore_zeigen的布局文件,我也有一个布局为我行:
So the data I want to show now is in rangliste
. Everything works fine but I don´t know how to show my data in my ListView
. The ListView is in the layout file of Highscore_zeigen, I also have a layout for my row:
<ListView
android:id="@+id/lvGewinner"
android:paddingTop="40dp"
android:paddingLeft="20dp"
android:paddingBottom="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >
</ListView>
下面是我的布局:
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textStyle="bold"
/>
<TextView
android:id="@+id/dist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="8dp"
android:textStyle="italic"/>
所以我有数据,我有一个ListView,我也有我的ListView的布局。什么我不知道的是我如何连接这三样东西,这样在我的ListView中显示的数据。
So I have the data, I have a ListView and I also have a layout for my ListView. What I don´t know is how I can connect these three things so that the data is shown in my ListView.
推荐答案
您需要使用 R.android.two_line_list_item 作为下面的链接描述。
有链接到的例子,但第二应答解释pretty井
You need to use R.android.two_line_list_item as described at the link below. There are links to the examples, but the second answer explains pretty well.
<一个href=\"http://stackoverflow.com/questions/2109271/can-any-one-provide-me-example-of-two-line-list-item-in-android\">can任何一个向我提供的例子Two_line_list_item在Android的?
编辑:
我会做它的方式,我会创造向全班访问的ListView控件的引用(即,到AsyncTask的)。然后在onPostExecute():
The way I would do it, I would create a reference to the ListView accessible to the whole class (namely, to AsyncTask). Then in onPostExecute():
lv = (ListView) findViewById(R.id.lvGewinner);
try {
for(int i = 0; i <5;i++){
...
namest = jb.getString("name");
datumst = jb.getString("datum");
...
Rangliste[] ... // create your array
...
}
lv.setAdapter(new RanglisteArrayAdapter(this, rangliste);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
您可能需要您的JSON转换成数组
这里是你的addapter:
you might need to convert your JSON into array Here is your addapter:
public class RanglisteArrayAdapter extends TwoLineArrayAdapter<Rangliste> {
public RanglisteArrayAdapter(Context context, Rangliste[] rangliste) {
super(context, rangliste);
}
@Override
public String lineOneText(Rangliste r) {
return r.namest;
}
@Override
public String lineTwoText(Rngliste r) {
return r.datumst;
}
}
这篇关于相关Android中的数据和ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!