0

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" />
Acejakk
  • 63
  • 1
  • 9

2 Answers2

1

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

Hirak Chhatbar
  • 3,159
  • 1
  • 27
  • 36
  • what stuff do I do in the OnClick exactly to pass?? – Acejakk Nov 05 '14 at 16:47
  • i dont exactly know how to go to certain url. update ur question with any simple example for going to url by clicking a button and i will show how to implement that in ur case – Hirak Chhatbar Nov 05 '14 at 16:51
  • Uri uri = Uri.parse("http://www.google.com"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); from -> http://stackoverflow.com/questions/4930228/open-a-url-on-click-of-ok-button-in-android This supposedly opens url on click! – Acejakk Nov 05 '14 at 16:52
  • Three errors.. in View.OnClickListener ( class must be declared abstract), in url_info(position) (Method call expected)and in startActivity(intent) (Missing method body) – Acejakk Nov 05 '14 at 17:02
  • update ur question with this code that is showing errors – Hirak Chhatbar Nov 05 '14 at 17:03
0

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


        }
    }
spurthi
  • 173
  • 1
  • 5
  • I wanted to know in my case what exactly do I put inside my onclick so I go to the url_info I passed from JSON.. – Acejakk Nov 05 '14 at 16:59