![](/img/trans.png)
[英]Passing data for list view from Asynctask to a custom list adapter class
[英]Parse Data from XML into List with Custom Adapter
我從網站上上傳的XML中獲取數據。 我想在具有兩個TextView
的自定義ListView
顯示XML中的文本。 'title'應該進入上部TextView
,'guid'應該進入下部TextView
。 我不確定該怎么做。 我編寫了以下自定義適配器。
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private ArrayList list;
private LayoutInflater layoutInflater;
public CustomAdapter(Context context, ArrayList list) {
this.list= list;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.layout_list_row, null);
holder = new ViewHolder();
holder.backgroundImage = (ImageView) convertView.findViewById(R.id.backgroundImage);
holder.topText = (TextView) convertView.findViewById(R.id.topText);
holder.bottomText = (TextView) convertView.findViewById(R.id.bottomText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
DiscourseItem discourseItem = (DiscourseItem) discourseList.get(position);
holder.topText.setText(discourseItem.getTopText());
holder.bottomText.setText(discourseItem.getBottomText());
return convertView;
}
static class ViewHolder {
ImageView backgroundImage;
TextView topText; //This should display the text in the 'title' field
TextView bottomText; //This should display the text in the 'guid' field
}
}
目前,我可以使用普通的ArrayAdapter
在單獨的ListView
中分別顯示兩者。 這是我編寫的代碼。
XMLParser.java
public class XMLParser extends AsyncTask {
private URL url;
public ArrayList<String> title = new ArrayList<>();
public ArrayList<String> guid = new ArrayList<>();
@Override
protected Object doInBackground(Object[] objects) {
try {
url = new URL(removed);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), null);
boolean insideItem = false;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")){
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")){
if (insideItem)
title.add(xpp.nextText());
} else if (xpp.getName().equalsIgnoreCase("guid")){
if (insideItem)
guid.add(xpp.nextText());
}
} else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem = false;
}
eventType = xpp.next();
}
} catch (MalformedURLException e){
e.printStackTrace();
} catch (XmlPullParserException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
public InputStream getInputStream(URL url){
try {
return url.openConnection().getInputStream();
} catch (IOException e){
e.printStackTrace();
}
return null;
}
public ArrayList<String> titles(){
return title;
}
public ArrayList<String> guids(){
return guid;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parser = new XMLParser();
parser.execute();
title = parser.titles();
guid = parser.guids();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
discourseList = getDiscourseList();
}
}, 2000);
}
});
ListView listView = (ListView) findViewById(R.id.discourseList);
listView.setAdapter(new CustomAdapter(this, discourseList));
}
private ArrayList<DiscourseItem> getDiscourseList() {
ArrayList<DiscourseItem> listData = new ArrayList<DiscourseItem>();
String[] topText = new String[title.size()];
topText = title.toArray(topText);
String[] bottomText = new String[guid.size()];
bottomText = guid.toArray(bottomText);
for (int i = 0; i <= title.size(); i++) {
try {
DiscourseItem discourseItem = new DiscourseItem();
discourseItem.setTopText(topText[i]);
discourseItem.setBottomText(bottomText[i]);
listData.add(discourseItem);
} catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
}
return listData;
}
}
編輯:
我進行了上述更改。 現在,當解析器運行並調用getDiscourseList()
,它將在discourseItem.setTopText(topText[i]);
引發以下錯誤discourseItem.setTopText(topText[i]);
:
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
您可以為上限值和下限值創建包裝器類(類似這樣,請考慮其他名稱,因為我不知道數據的上下文):
public class TitleGuidPair {
private final String title;
private final String guid;
public TitleGuidPair(String title, String guid) {
this.title = title;
this.guid = guid;
}
//getters
}
然后將您的結果解析為TitleGuidPair的ArrayList。 如果要保留解析算法,則可以進行一些后處理,然后從已有的兩個列表中構建TitleGuidPair。
落后一步只是傳遞到您的適配器TitleGuidPairs列表,並在getView方法中設置頂部和底部文本,例如
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.layout_list_row, null);
holder = new ViewHolder();
holder.backgroundImage = (ImageView) convertView.findViewById(R.id.backgroundImage);
holder.topText = (TextView) convertView.findViewById(R.id.topText);
holder.bottomText = (TextView) convertView.findViewById(R.id.bottomText);
TitleGuidPair titleGuidPair = list.get(position);
holder.topText.setText(titleGuidPair.getTitle());
holder.bottomText.setText(titleGuidPair.getGuid());
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.