自定义ListView上的按钮

时间:2014-11-05 16:30:45

标签: android listview android-listview

我有一个来自JSON的自定义列表视图,带有一些文本和一个按钮。当我从列表视图中的一个项目单击按钮时,我如何才能这样做?我转到从JSON传递的URL(在本例中为字符串“url_info”)?这是适配器的代码。

编辑:我尝试了按钮的建议代码,但它不起作用。

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

在listviewsingleitem.xml中,我有一个Button,当然是常规的相对视图布局。

<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" />

2 个答案:

答案 0 :(得分:1)

在getView方法中:

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

将位置从getView方法传递给onClickListener方法

答案 1 :(得分:0)

我已经创建了一个示例代码,请查看以下代码。它有效,

@覆盖         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);


        }
    }