如何与列表视图中的按钮进行交互

时间:2011-07-06 12:06:51

标签: android listview button onclick

我创建了一个customListview,我已经插入了一个按钮..现在点击那个button我想开始新的activity ..如何才能完成代码创建custom listview如下所示。任何人都可以告诉我如何与该按钮互动以启动新的activity ..

代码:

public static class ViewHolder
{
    Button butAddNew;
    TextView txtViewHeading;
    TextView  txtViewTitle;
    TextView  txtViewDescription;

}
public View getView(int position, View convertView, ViewGroup parent) 
{
    ViewHolder holder;

    if(convertView==null)
    {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.histryfrm_listview, null);
        holder.butAddNew =(Button) convertView.findViewById(R.id.butAddNew);
        holder.txtViewTitle =(TextView) convertView.findViewById(R.id.txtViewTitle);
        holder.txtViewDescription =(TextView) convertView.findViewById(R.id.txtViewDescription);
        holder.txtViewHeading =(TextView) convertView.findViewById(R.id.txtViewHeading);

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

    holder.txtViewTitle.setText(title[position]);
    holder.txtViewDescription.setText(description[position]);
    holder.txtViewHeading.setText(heading[position]);
    return convertView;
} 
}

3 个答案:

答案 0 :(得分:1)

创建它时,您始终可以将Activity中的onClickListener发送到适配器。

否则在适配器中设置一个监听器,或者在项目的xml中使用onClick。

当我做类似的事情时,我将我的活动onclicklistener传递给我的适配器。

编辑: 示例:

http://androidforbeginners.blogspot.com/2010/03/clicking-buttons-in-listview-row.html

或更好:

Android: ListView elements with multiple clickable buttons

这完全取决于你将如何做到这一点。 但另一个答案可能会奏效,只需传递正确的背景。

答案 1 :(得分:1)

在getView中设置holder.butAddNew.setOnClickListener非常简单,如

holder.butAddNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent code for Start Activity
            }
        });

创建您的customeadapter类的构造函数,并在该构造函数中传递活动或上下文,并将其用于启动活动。

更新

public View getView(int position, View convertView, ViewGroup parent) 

{
    ViewHolder holder;

if(convertView==null)
{
    holder = new ViewHolder();
    convertView = inflater.inflate(R.layout.histryfrm_listview, null);
    holder.butAddNew =(Button) convertView.findViewById(R.id.butAddNew);
    holder.txtViewTitle =(TextView) convertView.findViewById(R.id.txtViewTitle);
    holder.txtViewDescription =(TextView) convertView.findViewById(R.id.txtViewDescription);
    holder.txtViewHeading =(TextView) convertView.findViewById(R.id.txtViewHeading);

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

holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
holder.txtViewHeading.setText(heading[position]);
holder.butAddNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Context, activity.class));
            }
        });

return convertView;
} 

答案 2 :(得分:1)

你需要做这样的事情..

public static class ViewHolder
    {
        Button butAddNew;
        TextView txtViewHeading;
        TextView  txtViewTitle;
        TextView  txtViewDescription;

    }
public View getView(int position, View convertView, ViewGroup parent) 
{
    ViewHolder holder;

    if(convertView==null)
    {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.histryfrm_listview, null);
        holder.butAddNew =(Button) convertView.findViewById(R.id.butAddNew);
        holder.txtViewTitle =(TextView) convertView.findViewById(R.id.txtViewTitle);
        holder.txtViewDescription =(TextView) convertView.findViewById(R.id.txtViewDescription);
        holder.txtViewHeading =(TextView) convertView.findViewById(R.id.txtViewHeading);

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

    holder.txtViewTitle.setText(title[position]);
    holder.txtViewDescription.setText(description[position]);
    holder.txtViewHeading.setText(heading[position]);
    return convertView;
} 

    @Override
        protected void onListItemClick(ListView listView, View view, int position, long id) {
            super.onListItemClick(listView, view, position, id);
            LinearLayout l = (LinearLayout) listView.getChildAt(position);
                    //your code
                    //even this works fine if u don't have too many buttons
                      //u can call your activity here

}