开发者

How to interact with buttons present in list view

开发者 https://www.devze.com 2023-03-17 21:50 出处:网络
I have created an customListview where i have inserted an button..now on click of that button i want to start new activity..how that can be done the code for creating custom listview is given below..c

I have created an customListview where i have inserted an button..now on click of that button i want to start new activity..how that can be done the code for creating custom listview is given below..can anyone tell me how i interact with that button to start a new activity..

code:

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.txtViewD开发者_开发知识库escription =(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;
} 
}


You could always send a onClickListener from your Activity to your adapter when you create it.

Otherwise set a listener in your adapter or by using the onClick in the xml of the item.

When I did similar things I passed my activities onclicklistener to my adapter.

Edit: Examples:

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

Or better:

Android: ListView elements with multiple clickable buttons

It all depends on how you will do it. But the other answer will probably work, just pass the correct context.


u need to do something like this..

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

}


it is very simple set holder.butAddNew.setOnClickListener in getView like as

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

create constructor of your customeadapter class and pass activity or context in that constructor and use that for start activity.

updated

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

精彩评论

暂无评论...
验证码 换一张
取 消