Android - dynamic ListView buttons for each row invoking dynamic listeners

I'm new to android, I spent the last 2 days trying the previous examples and online solutions, but I just can't think about it :(

I can display the list view, parse json from the Internet and save the book title, book description and book ID and display this data in the list. I want the “download” button to be entered on each row for the ListView, each button will correspond to its book ID in Click (), and the action listener will load the book by adding that ID to the URL. e.g. www.books.com/download_book1 or / download _book2 ....

Here is my code. Class 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);  

.....

. , 1 . shelfrow.xml(textView, textView item_title item_subtitle) shelflist.xml(ListView). shelf.xml

+3
2

ListAdapter.

: , , , . . : OnClickListener. TextView ( ) ( OnClickListener). , , , , , .

. , .

private class MyItemModel{ //that our book
    String title; // the book title
            String description;
            long id;
    OnClickListener listener = new OnClickListener(){ // the book 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 MyListAdapter , id ):

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); 
+8

, ArrayAdapter, onClickListener .

getView ArrayAdapter .

-

<?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>

getView ArrayAdapter

private List<Map<String, String>> jsonMapList;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.list_item, null);
    // here you set textview values (title and description)
    // TextView title = (TextView) v.findViewById(R.id.list_item_title);
    // title.setText('bla');

    // and set OnClickListener
    Button button = (Button) v.findViewById(R.id.download_button);                
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            downloadFile(getUrl(position));
        }
    });

    return v;
}

// method that downloads file
private void downloadFile(String url) {}

// get url from your list by index
private String getUrl(int index) {
    return jsonMapList.get(index).get("url");
}

, , .

    CustomAdapter listAdapter = new CustomAdapter(this, android.R.layout.simple_list_item_single_choice, jsonMapList);
    setListAdapter(listAdapter);
+1

All Articles