使用以字符串数组作为元素的 ArrayList 填充 ListView [英] Populate ListView with ArrayList having String array as elements
本文介绍了使用以字符串数组作为元素的 ArrayList 填充 ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
dataAdapter = new ArrayAdapter(getApplicationContext(), R.layout.lst_layout, R.id.lstLayout, exList);
lstView = (ListView) findViewById(R.id.listView);
lstView.setAdapter(dataAdapter);
exList 是字符串数组的 ArayList.如何用它填充 ListView?
exList is the ArayList of String arrays. How to populate ListView with it?
exList = new ArrayList<String[]>();
编辑从这里开始.它在此处创建的异步任务中 asdad asd asd asdasada sdada daasd asd ad asda dasd asd asda da dasdasd asdasd ad :
Edit starts here. It's in asynctask created here asdad asd asd asdasada sdada daasd asd ad asda dasd asd asda da dasdasd asdasd ad :
@Override
protected String doInBackground(String... urls) {
String[] temp = new String[4];
try {
InputStream stream = downloadUrl("http://www.tcmb.gov.tr/kurlar/today.xml");
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new InputStreamReader(stream));
int eventType = xpp.getEventType();
int kontrol = 0;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equals("Currency")) {
kontrol++;
//ExchangeRate exr = new ExchangeRate();
//exr.setCurrencyCode(xpp.getAttributeValue(null, "CurrencyCode"));
temp[0] = xpp.getAttributeValue(null, "CurrencyCode");
Log.d("deneme: ", xpp.getAttributeValue(null, "CurrencyCode"));
} else if (xpp.getName().equals("Isim") && xpp.next() == XmlPullParser.TEXT) {
kontrol++;
//ExchangeRate exr = exList.get(exList.size() - 1);
//exr.setCurrencyName(xpp.getText());
temp[1] = xpp.getText();
Log.d("deneme", xpp.getText());
} else if (xpp.getName().equals("ForexBuying") && xpp.next() == XmlPullParser.TEXT) {
kontrol++;
//ExchangeRate exr = exList.get(exList.size() - 1);
//exr.setForexBuying(Double.valueOf(xpp.getText()));
temp[2] = xpp.getText();
Log.d("deneme", xpp.getText());
}else if (xpp.getName().equals("ForexSelling") && xpp.next() == XmlPullParser.TEXT) {
kontrol++;
//ExchangeRate exr = exList.get(exList.size() - 1);
//exr.setForexBuying(Double.valueOf(xpp.getText()));
temp[3] = xpp.getText();
Log.d("deneme", xpp.getText());
}
if (kontrol == 4) {
exList.add(temp);
temp = new String[4];
kontrol=0;
}
}
eventType = xpp.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return "";
} catch (IOException e) {
return e.getMessage();
}
}
推荐答案
我按照建议使用自定义适配器处理了它.这是适配器:
I handled it using custom adapter as suggested. Here is the adapter:
package com.xxx.xxx;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class OzelAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<String[]> mKisiListesi;
public OzelAdapter(Activity activity, ArrayList<String[]> kisiler) {
//XML'i alıp View'a çevirecek inflater'ı örnekleyelim
mInflater = (LayoutInflater) activity.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
//gösterilecek listeyi de alalım
mKisiListesi = kisiler;
}
@Override
public int getCount() {
return mKisiListesi.size();
}
@Override
public Object getItem(int position) {
//şöyle de olabilir: public Object getItem(int position)
return mKisiListesi.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View satirView;
satirView = mInflater.inflate(R.layout.lst_layout, null);
TextView textView1 =
(TextView) satirView.findViewById(R.id.lstLayout1);
TextView textView2 =
(TextView) satirView.findViewById(R.id.lstLayout2);
TextView textView3 =
(TextView) satirView.findViewById(R.id.lstLayout3);
TextView textView4 =
(TextView) satirView.findViewById(R.id.lstLayout4);
textView1.setText(mKisiListesi.get(position)[0]);
textView2.setText(mKisiListesi.get(position)[1]);
textView3.setText(mKisiListesi.get(position)[2]);
textView4.setText(mKisiListesi.get(position)[3]);
return satirView;
}
}
这是列表视图的布局行:
Here is the layout row of the listview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:gravity="left"
android:textColor="@color/black"
android:maxLines="8"
android:lines="8"
android:id="@+id/lstLayout1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:editable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:gravity="left"
android:textColor="@color/black"
android:maxLines="8"
android:lines="8"
android:id="@+id/lstLayout2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:editable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:gravity="left"
android:textColor="@color/black"
android:maxLines="8"
android:lines="8"
android:id="@+id/lstLayout3"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:editable="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:gravity="left"
android:textColor="@color/black"
android:maxLines="8"
android:lines="8"
android:id="@+id/lstLayout4"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:editable="false" />
</LinearLayout>
最后:
OzelAdapter dataAdapter = new OzelAdapter(MainActivity.this, exList);
lstView = (ListView) findViewById(R.id.listView);
lstView.setAdapter(dataAdapter);
这篇关于使用以字符串数组作为元素的 ArrayList 填充 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文