我是一名 iOS 开发人员,正在跳转到 Android 以移植我正在从事的项目,但我似乎未能掌握 Android 的自定义 BaseAdapter 的一些概念。我正在开发一个从站点解析 XML 并将 XML 元素中的文本填充到 ListView 中的应用程序。我的 XML 解析部分没有问题。我遇到的问题是在列表视图的每一行中添加一个按钮。我了解设置 OnClick 侦听器的概念,并且我需要实现一个 BaseAdapter,但我似乎无法将我的数据从我的主要活动获取到我设置的包含我的 BaseAdapter 的类。现在,启动应用程序时数据不会显示。我已经进行了一些调试并验证了 menuItems ArrayList 中的数据已正确发送到 BaseAdapter 类,我 我只是不知道如何让它显示在 ListView 中。这可能是非常小的东西,我想我只是看了太久,没有看到它。任何帮助将不胜感激。下面列出了代码和布局 XML。
public class Podcasts extends ListActivity{
private static final String HASHMAP_ID = "_id";
private CustomAdapter adap;
static final String URL = "http://www.undignified.podbean.com/feed";
//XML Node Keys
static final String KEY_ITEM = "item";
static final String KEY_TITLE = "title";
static final String KEY_DESCRIPTION = "itunes:subtitle";
static final String KEY_PODCASTURL = "enclosure";
public static final ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.podcasts);
//Instance of XML parser
UndigParser uparser = new UndigParser();
String xml = uparser.getXmlFromURL(URL);
Document doc = uparser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for(int i = 0; i < nl.getLength(); i++){
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_TITLE, uparser.getValue(e, KEY_TITLE));
map.put(KEY_DESCRIPTION, uparser.getValue(e, KEY_DESCRIPTION));
map.put(KEY_PODCASTURL, uparser.getValue(e, KEY_PODCASTURL));
menuItems.add(map);
}
adap = new CustomAdapter(this, menuItems, R.layout.list_item, new String[] {KEY_TITLE, KEY_DESCRIPTION, KEY_PODCASTURL}, new int[]{ R.id.title, R.id.description, R.id.podcasturl});
//adap = new CustomAdapter(this, menuItems);
setListAdapter(adap);
public static class CustomAdapter extends BaseAdapter implements Filterable{
private LayoutInflater mInflater;
private Context context;
public CustomAdapter(Context context, ArrayList<HashMap<String, String>> menuItems, int listItem, String[] strings, int[] is){
mInflater = LayoutInflater.from(context);
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent){
ViewHolder holder;
if(convertView==null){
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.description=(TextView)convertView.findViewById(R.id.description);
holder.podcasturl=(TextView)convertView.findViewById(R.id.podcasturl);
holder.title=(TextView)convertView.findViewById(R.id.title);
holder.downloadBTN=(Button)convertView.findViewById(R.id.btnStartDownload);
convertView.setOnClickListener(new OnClickListener(){
private int pos = position;
@Override
public void onClick(View v){
Toast.makeText(context, "Text-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});
holder.downloadBTN.setOnClickListener(new OnClickListener(){
private int pos = position;
@Override
public void onClick(View v){
Toast.makeText(context, "Button-"+String.valueOf(pos), Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
return convertView;
}
static class ViewHolder{
TextView description;
TextView title;
TextView podcasturl;
Button downloadBTN;
}
@Override
public int getCount() {
// Currently set to zero. If I set it to menuItems.size() it will crash the app.
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public Filter getFilter() {
return null;
}
}
XML 布局 - list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dip">
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/startPodcastDownload"
android:layout_width="50dip"
android:layout_height="50dip"
android:focusable="false"
android:text="Download Podcast">
</Button>
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/LinearLayout01"
android:textColor="#acacac"
android:paddingBottom="2dip">
</TextView>
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/LinearLayout01"
android:layout_below="@id/title"
android:textColor="#acacac"
android:paddingBottom="2dip">
</TextView>
<TextView
android:id="@+id/podcasturl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/LinearLayout01"
android:layout_below="@+id/description"
android:textColor="#acacac"
android:paddingBottom="2dip">
</TextView>
</RelativeLayout>