Acejakk
Acejakk

Reputation: 63

Button on Custom ListView

I have a custom listview from JSON with some text and a button. How Can I make it so when I click the button from one of the items on the listview I go to the url I passed from JSON ( string "url_info" in this case )? Here's the code for the adapter.

EDIT: I tried the suggested code for my button but it doesn't work.

error compiling

public class ListViewEventosAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> resultp = new HashMap<String, String>();

    public ListViewEventosAdapter(Context context,
                                  ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // Declare Variables
        TextView titulo;
        TextView dias ;
        TextView descricao;
        TextView url_info;
        Button btn_maisinfo;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_eventositem, parent, false);
        // Get the position
        resultp = data.get(position);

        // Locate the TextViews in listview_item.xml
        titulo = (TextView) itemView.findViewById(R.id.titulo);
        dias = (TextView) itemView.findViewById(R.id.dias);
        descricao = (TextView) itemView.findViewById(R.id.descricao);
        url_info = (TextView) itemView.findViewById(R.id.url_info);

        btn_maisinfo = (Button)itemView.findViewById(R.id.btn_maisinfo);

        btn_maisinfo.setOnClickListener (new View.OnClickListener(){
             Uri uri = Uri.parse(url_info(position));
             Intent intent = new Intent(Intent.ACTION_VIEW, uri);
             startActivity(intent);
         }

        // Capture position and set results to the TextViews
        titulo.setText(resultp.get(EventosSearchActivity.TITULO));
        dias.setText(resultp.get(EventosSearchActivity.DIAS));
        descricao.setText(resultp.get(EventosSearchActivity.DESCRICAO));
        url_info.setText(resultp.get(EventosSearchActivity.URL_INFO));

        return itemView;
    }
}

In the listviewsingleitem.xml I have the Button for it, of course, in a regular relative view layout.

<Button
    android:id="@+id/btn_maisinfo"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:background="#3BABDC"
    android:textColor="#FFFFFF"
    android:textStyle="bold"
    android:textSize="12sp"
    android:text="MAIS INFORMAÇÃO" />

Upvotes: 0

Views: 138

Answers (2)

Hirak Chhatbar
Hirak Chhatbar

Reputation: 3181

In the getView method :

Button xyz = (Button)itemView.findViewById(R.id.abc)
xyz.setOnClickListener (new View.OnClickListener(){
   Uri uri = Uri.parse(url_info(position));
   Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
   startActivity(intent);
}

pass the position from the getView method to the onClickListener method

Upvotes: 1

spurthi
spurthi

Reputation: 173

I have created a sample code , Please check with the below code. It works ,

@Override public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getApplicationContext()).inflate(
                    R.layout.appointment_list_content, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mAppointmentStatus.setText(context.getResources().getString(R.string.visited));  
            holder.mAppointmentStatus.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                    // Use Intent to move 
                }
            });

        }





        return convertView;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 100;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    class ViewHolder {

        Button mAppointmentStatus;



        public ViewHolder(View convertView) {
                mAppointmentStatus = (Button) convertView.findViewById(R.id.app_list_status);


        }
    }

Upvotes: 0

Related Questions