我是一名Android新手,过去两天尝试了之前的示例和在线解决方案,但似乎无法理解:(
我能够显示一个列表视图,解析一些来自在线的JSON并存储书籍标题、书籍描述和书籍ID,并在列表视图中显示这些数据。我希望能够在每一行的ListView中放置一个“下载”按钮,每个按钮都对应于其书籍ID,点击后操作监听器将通过将该ID附加到URL来下载该书籍。 例如www.books.com/download_book1或/download_book2....
这是我的代码。Catalogue.java类
public class Catalogue extends ListActivity {
private JSONObject json;
private ListView lv;
private ArrayList<Integer> alKey = new ArrayList<Integer>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //icicle
setContentView(R.layout.shelflist);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
....
try{
JSONArray entries = json.getJSONArray("entries");
for(int i=0;i<entries.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = entries.getJSONObject(i);
alKey.add(e.getInt("key"));
map.put("id", String.valueOf(i));
map.put("title", "Title:" + e.getString("title"));
map.put("description", "Description: " + e.getString("description"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.shelfrow,
new String[] { "title", "description" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
lv = getListView();
lv.setTextFilterEnabled(true);
我只能做到这一步,不知道如何在List中每行添加一个按钮并为每个按钮分配一个操作监听器。 我还有一个shelfrow.xml文件(textView,item_title和item_subtitle的textView)和一个shelflist.xml文件(ListView)。 我有一个包含
的shelf.xml文件。