Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-每行的ListView动态按钮调用动态侦听器_Android_Listview_Button - Fatal编程技术网

Android-每行的ListView动态按钮调用动态侦听器

Android-每行的ListView动态按钮调用动态侦听器,android,listview,button,Android,Listview,Button,我是android新手,在过去的两天里我一直在尝试以前的示例和在线解决方案,但我似乎无法理解:( 我可以显示一个列表视图,从联机解析一些json,存储图书标题、图书描述和图书ID,并在listview中显示这些数据。我希望能够在listview的每一行中放置一个“下载”按钮,每个按钮将对应于单击时的图书ID()动作监听器将通过将该ID附加到url来下载该书。 e、 g www.books.com/download_book1或/download_book2 这是我的code.catalog.ja

我是android新手,在过去的两天里我一直在尝试以前的示例和在线解决方案,但我似乎无法理解:(

我可以显示一个列表视图,从联机解析一些json,存储图书标题、图书描述和图书ID,并在listview中显示这些数据。我希望能够在listview的每一行中放置一个“下载”按钮,每个按钮将对应于单击时的图书ID()动作监听器将通过将该ID附加到url来下载该书。 e、 g www.books.com/download_book1或/download_book2

这是我的code.catalog.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);  
公共类目录扩展了ListActivity{
私有JSONObject json;
私有ListView lv;
private ArrayList alKey=new ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);//冰柱
setContentView(R.layout.shelflist);
ArrayList mylist=新的ArrayList();
....
试一试{
JSONArray entries=json.getJSONArray(“entries”);

对于(int i=0;i您可以创建自己的类扩展ArrayAdapter,它将保存列表并将onClickListener设置为每行中的按钮

但在ArrayAdapter的getView方法中,每次都必须创建一个新视图

例如,行布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="110dp"
    android:background="#FFF"
    android:layout_width="fill_parent">

<LinearLayout
              android:layout_width="fill_parent"
              android:background="#FFF"
              android:orientation="vertical"
              android:padding="2dp"
              android:layout_height="110dp">
    <TextView android:id="@+id/list_item_title"
              android:background="#FFF"
              android:layout_width="fill_parent"
              android:layout_height="40dp"/>
    <Button android:id="@+id/download_button"
            android:gravity="center"
            android:text="Download"
            android:layout_height="35dp"/>
</LinearLayout>

</RelativeLayout>

基本上你需要学习的概念

下面是一个简短的故事:描绘一个对象,该对象包含要在列表中显示的数据,以及单独显示每一行的方式。这是您的ListAdapter。现在看每一行:它是一本带有标题和OnClickListener的书。它在一个带有TextView(标题)和按钮的视图中呈现(对于OnClickListener),您所需要做的就是为将用于每一行的适配器提供一个视图,并在列表中列出您想要的书籍

这是一些示例代码。我希望它能让事情变得更清楚一点

private class MyItemModel{ //that's our book
    String title; // the book's title
            String description;
            long id;
    OnClickListener listener = new OnClickListener(){ // the book's action
        @Override
        public void onClick(View v) {
                            // the default action for all lines
            doSomethingWithTheBookTitleOrUniqueId(this);
        }
    };
}

private class MyListAdapter extends BaseAdapter{
    View renderer;
    List<MyItemModel> items;

            // call this one and pass it layoutInflater.inflate(R.layout.my_list_item)
    public MyListAdapter(View renderer) {
        this.renderer = renderer;
    }

            // whenever you need to set the list of items just use this method.
            // call it when you have the data ready and want to display it
    public void setModel(List<MyItemModel> items){
        this.items = items;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return items!=null?items.size():0;
    }
    @Override
    public Object getItem(int position) {
        return items!=null?items.get(position):null;
    }
    @Override
    public long getItemId(int position) {
        return items!=null?items.get(position).id:-1;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = renderer;
        }
        MyItemModel item = items.get(position);
             // replace those R.ids by the ones inside your custom list_item layout.
        TextView label = (TextView)convertView.findViewById(R.id.item_title);
        label.setText(item.label);
        Button button = (Button)convertView.findViewById(R.id.item_button);
        button.setOnClickListener(item.listener);
        return convertView;
    }
}
私有类MyItemModel{//这是我们的书
String title;//书名
字符串描述;
长id;
OnClickListener=new OnClickListener(){//书本的操作
@凌驾
公共void onClick(视图v){
//所有行的默认操作
有书名或唯一标识的内容(本);
}
};
}
私有类MyListAdapter扩展BaseAdapter{
视图渲染器;
清单项目;
//调用此项并将其传递给LayoutFlater。充气(R.layout.my_列表项)
公共MyListAdapter(视图渲染器){
this.renderer=渲染器;
}
//每当需要设置项目列表时,只需使用此方法。
//当您准备好数据并希望显示它时调用它
公共void集合模型(列表项){
这个项目=项目;
notifyDataSetChanged();
}
@凌驾
public int getCount(){
返回项!=null?项。大小():0;
}
@凌驾
公共对象getItem(int位置){
返回项目!=null?项目。获取(位置):null;
}
@凌驾
公共长getItemId(int位置){
返回项目!=null?项目。获取(位置)。id:-1;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=渲染器;
}
MyItemModel item=items.get(位置);
//将这些R.id替换为自定义列表项布局中的R.id。
TextView标签=(TextView)convertView.findViewById(R.id.item_title);
label.setText(item.label);
按钮按钮=(按钮)convertView.findViewById(R.id.item_按钮);
button.setOnClickListener(item.listener);
返回视图;
}
}
例如,为了传递列表,您可以这样做,而不是将数据放在hashmaps列表中(注意,我还根据需要更新了MyItemModel和MyListAdapter,添加了id和description属性):

List myListModel=new ArrayList();
试一试{
JSONArray entries=json.getJSONArray(“entries”);

对于(int i=0;iI)我有点理解您在排序故事中所说的内容,但我仍然无法正确编码我正在将ListAdapter adapter=new SimpleAdapter更改为MyListAdapter在我的活动(shelf类)中我如何将hashmap数据(标题和说明)传递到my adapter中?有两种可能性:1)您可以从hashmap(或直接从JSONArray)创建一个列表,完成后在适配器上调用setModel(),或者2)您可以更改MyListAdapter,以便它直接使用您的列表…将编辑答案,看起来您需要更多的java实践:)谢谢。现在这对我来说更清楚了。我已经在ListItemModel类中添加了一些setter/getter。我在行中遇到了一个错误:MyListAdapter=new MyListAdapter(getLayout.Inflater()。inflate(R.layout.shelf),它是LayoutFlater类型中的方法inflate(int,ViewGroup),不适用于参数(int)
    CustomAdapter listAdapter = new CustomAdapter(this, android.R.layout.simple_list_item_single_choice, jsonMapList);
    setListAdapter(listAdapter);
private class MyItemModel{ //that's our book
    String title; // the book's title
            String description;
            long id;
    OnClickListener listener = new OnClickListener(){ // the book's action
        @Override
        public void onClick(View v) {
                            // the default action for all lines
            doSomethingWithTheBookTitleOrUniqueId(this);
        }
    };
}

private class MyListAdapter extends BaseAdapter{
    View renderer;
    List<MyItemModel> items;

            // call this one and pass it layoutInflater.inflate(R.layout.my_list_item)
    public MyListAdapter(View renderer) {
        this.renderer = renderer;
    }

            // whenever you need to set the list of items just use this method.
            // call it when you have the data ready and want to display it
    public void setModel(List<MyItemModel> items){
        this.items = items;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return items!=null?items.size():0;
    }
    @Override
    public Object getItem(int position) {
        return items!=null?items.get(position):null;
    }
    @Override
    public long getItemId(int position) {
        return items!=null?items.get(position).id:-1;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = renderer;
        }
        MyItemModel item = items.get(position);
             // replace those R.ids by the ones inside your custom list_item layout.
        TextView label = (TextView)convertView.findViewById(R.id.item_title);
        label.setText(item.label);
        Button button = (Button)convertView.findViewById(R.id.item_button);
        button.setOnClickListener(item.listener);
        return convertView;
    }
}
List<MyItemModel> myListModel = new ArrayList<MyItemModel>();
try{
    JSONArray entries = json.getJSONArray("entries");
    for(int i=0;i<entries.length();i++){                        
        MyItemModel item = new MyItemModel();    
        JSONObject e = entries.getJSONObject(i);
        alKey.add(e.getInt("key")); 
        item.id = i;
        item.title = e.getString("title");
        item.description = e.getString("description");
        // you can change the button action at this point: 
        // item.onClickListener = new OnClickListener(){...};
        myListModel.add(item);
    }

}catch(JSONException e)        {
    Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new MyListAdapter(getLayoutInflater().inflate(R.layout.shelfrow, this));
adapter.setModel(myListModel);
setListAdapter(adapter);
lv = getListView();
lv.setTextFilterEnabled(true);