Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6817945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:07:00+00:00 2026-05-26T21:07:00+00:00

I have listView in which I inflate a row contain textview and button now

  • 0

I have listView in which I inflate a row contain textview and button now I want to handle click event of button which coded in adapter and I am able to get the position of button too. But here when I click on button I am changing a image of button its works fine problem is when I scroll the view it again change the image of button as it was before because of getview() Method its recycle view every time.

How to handle above functionality in adapter?

Below is the button event code which is coded in adpater.

holder.download.setTag(position) ;

        holder.download.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                pos =(Integer) v.getTag() ; 
                Log.d("*******************************", ""+pos);
                Constant.buttonStates[pos]=1;   
        pref = activity.getSharedPreferences("status", MODE_PRIVATE);
                SharedPreferences.Editor edit=pref.edit();
                edit.putInt("s",Constant.buttonStates[pos]);
                //edit.putString("s",  Constant.buttonStates.toString());
                edit.commit();

                check[pos]=pref.getInt("s", 1);

                v.setBackgroundResource(R.drawable.right);
                try {
                    new PubDetailsTask().execute("");
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        });

Below is the adapter in which I am handling a button events.

public class PublicationAdapter extends BaseAdapter implements
    DownloadThreadListener {

    private LayoutInflater mInflater;
    private SQLiteDBAssistant sqlliteCity;
    private DownloadThread downloadThread;
    private Handler handler;
    public ImageLoader imageLoader;
    static String videoUrl, downloadImage, downloadVideo, name, url,
        output = "", selId = "";
    int pos, btnpos, tempos;
    JSONArray globalData, arrListFinal;
    Activity activity;
    Bitmap bbicon;
    ArrayList<String> imageUrl = new ArrayList<String>();
    AlertDialog.Builder downloadDialog;
    AlertDialog downloadMsg;
    ViewHolder holder;
    String author[];
    HttpManager httpObject;
        String pub_id = "", title = "", avgrat = "", content = "", authorname = "",
        mediafile = "", docurl = "", thumburl = "", peerrat = "",
        profrat = "", comrat = "", peerrank = "", profrank = "",
        comrank = "", urid = "", uravgrat = "", urrole = "",
        imagePath = "";
    String video_SDpath = "";
    int[] buttonStates;

    public PublicationAdapter(Activity a, JSONArray arrList) {

               httpObject = new HttpManager();
       globalData = arrList;
       activity = a;

       mInflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       imageLoader = new ImageLoader(activity.getApplicationContext());
       Constant.publicationIdArraylist = new ArrayList<String>();
       downloadThread = new DownloadThread(PublicationAdapter.this, activity);
       downloadThread.start();
       handler = new Handler();

       buttonStates = new int[globalData.length()];
       for (int i = 0; i < globalData.length(); i++) {
            buttonStates[i] = 0;
       }

       downloadDialog = new AlertDialog.Builder(a);
       downloadDialog.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    downloadMsg.dismiss();
                }
       });
       downloadMsg = downloadDialog.create();

   }
   public int getCount() {
    return globalData.length();
   }
   public Object getItem(int postionitem) {
    return postionitem;
   }
   public long getItemId(int positionitemId) {
    return positionitemId;
   }
   public void setSelectedPosition(int pos) {
    tempos = pos;
    // inform the view of this change
    notifyDataSetChanged();
       }
   public int getSelectedPosition() {
    return tempos;
   }
   public class ViewHolder {
    TextView twtdata;
    ImageView twtimg;
    TextView twtnm;
    Bitmap image;
    Button download;
   }

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

          View vi = convertView;
          if (convertView == null) {

               vi = mInflater.inflate(R.layout.listrowplaylists, null);

               holder = new ViewHolder();
               holder.twtdata = (TextView) vi.findViewById(R.id.txtauthor);
               holder.twtnm = (TextView) vi.findViewById(R.id.txtlisttitle);
               holder.twtimg = (ImageView) vi.findViewById(R.id.avatar);
               holder.download = (Button) vi.findViewById(R.id.btn_download);

               if (buttonStates[position] == 0)
                 holder.download.setBackgroundResource(R.drawable.downloadicon); //button not clicked   
                           else 
                holder.download.setBackgroundResource(R.drawable.right); // button clicked
               vi.setTag(holder);
        }
        else 
        {
              holder = (ViewHolder) vi.getTag();
        }

        try {

              JSONObject firstObject = globalData.optJSONObject(position);
              Log.i("Publication", firstObject.toString());

              holder.twtnm.setText(firstObject.getString("Title"));
              imageUrl.add(firstObject.optString("ThumbnailUrl"));
              holder.twtimg.setTag(imageUrl.get(position));
              imageLoader.DisplayImage(imageUrl.get(position), activity,
                holder.twtimg);

              Constant.publicationIdArraylist.add(firstObject.getString("Id"));
              Log.i("ID IN LIST", "=================="
                + Constant.publicationIdArraylist.get(position));

             JSONArray firstarray = firstObject.getJSONArray("Authors");
             int len = firstarray.length();
             author = new String[len];

             for (int j = 0; j < firstarray.length(); j++) {
                JSONObject third = firstarray.getJSONObject(j);
                author[j] = authorname = third.getString("AuthorName");
             }
             try {
                holder.twtdata.setText(author[0] + ", " + author[1]);
             } 
             catch (Exception e) {              
             }
       } 
       catch (Exception e) {
               e.printStackTrace();
       }
       holder.download.setTag(position);

       holder.download.setOnClickListener(new OnClickListener() {

              public void onClick(View v) {

                 pos = (Integer) v.getTag();
                 Log.d("*******************************", "" + pos);
                 buttonStates[pos] = 1;
                 v.setBackgroundResource(R.drawable.right);
                 selId = Constant.publicationIdArraylist.get(position);
                 System.out.println("buton clicked id  " + selId);
                 try {
                     new PubDetailsTask().execute("");
                 }
                                 catch (Exception e) {
                 }
             }
       });
       return vi;
}

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:07:01+00:00Added an answer on May 26, 2026 at 9:07 pm

    Lets say you have CustomAdapter.class to populate your listview,then:

    public class CustomAdapter extends ArrayAdapter<String> {
    
            String[] array;
        Context mContext;
        LayoutInflater mInflater;
        int[] buttonStates;
    
        public CustomAdapter(Context context, int textViewResourceId,
                String[] objects)
        {
            super(context, textViewResourceId, objects);
            array=objects;
            mContext=context;
            mInflater = LayoutInflater.from(context);
            //save all buttons state as 0(not clicked) initially
            buttonStates=new int[objects.length];
                for(int i=0;i<objects.length;i++)
                { 
                       buttonStates[i]=0;
                }  
        }  
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ...
             if(buttonStates[position]==0)
             {
                 holder.button.setBackgroundResource(R.drawable.bg_normal);  //set background for button "not clicked"          
             }
             else
             {
                 holder.button.setBackgroundResource(R.drawable.bg_pressed);  //set background for button "clicked"                 
             }
    
             final int pos=position;
             holder.download.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
    
                    buttonStates[pos]=1;                
                    holder.button.setBackgroundResource(R.drawable.bg_clicked);                
                }
            });         
        }  
    }
    

    I have tested this one and it works fine for me!

    EDIT :

    You need to add these lines of code:

    if (buttonStates[position] == 0)
             holder.download.setBackgroundResource(R.drawable.downloadicon); // set background for button "not clicked"             
    else
             holder.download.setBackgroundResource(R.drawable.right); // set background for button "clicked"
    

    out side if(convertView == null)...else.... So put that code just before you write:

    holder.download.setOnClickListener(new OnClickListener() {
    ...}
    

    That will solve your issue.

    When you put it into if(convertView==null),it will called only once.so you are getting the problem you described to me in your comment!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one listview.which contain one image ,text and one button .i had creted
I want to create a layout which contains TextView , Button and ListView here
Hallo all, I have a ListView which contains a Button in each line. The
I have got ListView with a custom Adapter. Every row contains clickable buttons and
I have a listview which includes 2 textviews and 1 imageview. Now, the image
Hi i want to set different images to each row of a listview which
I have a ListView which uses a CursorAdatper as its adapter. I would like
I have a ListView in my ListActivity which is inflated with an adapter. There
I have a listactivity which contains a listview. i want to show another layout
I have ListView which shows images from an ImageList. Now wanted to get index

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.