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

  • SEARCH
  • Home
  • 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 7836385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:07:18+00:00 2026-06-02T14:07:18+00:00

I have designed a listview by inflating each row using inflater and I have

  • 0

I have designed a listview by inflating each row using inflater and I have changed color of convertView’s elements on their click events. But after scrolling the list the elements loss these changes. How can I “preserve” their state and represent them?

And there is one more query in my mind, when clicking the submit button, how can I calculate the total number of “listitems” which had not been clicked?

before scroll http://69.imagebam.com/download/l4tJZG08i5tM_7SV3K_fHQ/18631/186307764/snap1.png
after scroll http://17.imagebam.com/download/CC3cRv0ITi42Fo8WWUs86g/18631/186307781/snap4.png

Here is my code:

package scf.login;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class firstsubActivity extends Activity
{

//variable declaration
ListView lvSubCat;
Intent action;
Button btnBack,btnSubmit,btnNext;
Boolean submission=false;
Integer unchecked=0;
String[] subStatus = {"","Ok","","","","Ok","","","Alert","","","","Checked","","","Ok","","","Alert",""};

// defining on click listener
android.view.View.OnClickListener lstnr = new android.view.View.OnClickListener() 
{

    @Override
    public void onClick(View v) 
    {
    switch (v.getId()) {

    case R.id.btnBack:
            Toast.makeText(getApplicationContext(), "Back button clicked", Toast.LENGTH_SHORT).show();
        break;
    case R.id.btnSubmit:
            if(!submission)
            {

                checkcompletion();

                if(unchecked>0)
                {
                    Toast.makeText(firstsubActivity.this, "Complete the precheking of "+unchecked+" intervention(s) highlighted by Gray colour", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(firstsubActivity.this,"Successfully submitted !!",Toast.LENGTH_SHORT).show();                        
                    submission=true;
                }

            }
            else
            {
                updatecofirmation();
            }
        break;
    case R.id.btnNext:
            if(submission)
            {
                nextconfirmation();
            }
            else
            {
                Toast.makeText(firstsubActivity.this,"Submit the answers",Toast.LENGTH_SHORT).show();
            }
        break;

    }

}

    private void nextconfirmation() 
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(firstsubActivity.this);
        builder.setCancelable(false);
        builder.setTitle("Confirmation message");
        builder.setMessage("Do you want to leave this page ?");
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

                Intent action = new Intent(firstsubActivity.this,cat_subcatActivity.class);
                startActivity(action);      
                }
        });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(firstsubActivity.this, "No clicked", Toast.LENGTH_SHORT).show();

            }
        });

        builder.create().show();
    }


    private void updatecofirmation() 
    {
        AlertDialog.Builder builder2 = new AlertDialog.Builder(firstsubActivity.this);
        builder2.setCancelable(false);
        builder2.setTitle("Confirmation message");
        builder2.setMessage("Do you want to update answers ?");
        builder2.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                submission=false;
                btnSubmit.performClick();
            }
        });
        builder2.setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(firstsubActivity.this, "No clicked", Toast.LENGTH_SHORT).show();

            }
        });

        builder2.create().show();
    }


    private void checkcompletion() 
    {
        unchecked=subStatus.length;
        try
        {
            for(Integer j=0;j<subStatus.length;j++)
            {
                // CONFUSED
            }

        }


        catch (Exception e) {
            e.printStackTrace();
        }                   
        finally
        {

        }



    }


};

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.firstsub);

    //Binding
    lvSubCat = (ListView)findViewById(R.id.lvSubCat);
    btnBack = (Button)findViewById(R.id.btnBack);
    btnSubmit=(Button)findViewById(R.id.btnSubmit);
    btnNext = (Button)findViewById(R.id.btnNext);

    //setting listeners
    btnBack.setOnClickListener(lstnr);
    btnSubmit.setOnClickListener(lstnr);
    btnNext.setOnClickListener(lstnr);


    //data of subcategories and their status from database
    String[] subCat = {"zero","first","second","third","forth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth","thirteenth","forteenth","fifteenth","sixteenth","seventeenth","eighteenth","ninteenth"};
    String[] subStatus = {"","Ok","","","","Ok","","","Alert","","","","Checked","","","Ok","","","Alert",""};
    /*
    for(Integer i=0;i<subStatus.length;i++)
    {
        set the status of each subcategory
    }
    */

    //setting adapter and binding adapter to the views
    MySimpleAdapter adapter1 = new MySimpleAdapter(this,subCat);
    lvSubCat.setAdapter(adapter1);
}

//defining customised MySimpleAdapter class inside firstsubActivity class
public class MySimpleAdapter extends BaseAdapter 
{
    private final Activity context;
    private final String[] subCat;


    public MySimpleAdapter(Activity context, String[] subCat) 
    {
        super();
        this.context = context;
        this.subCat = subCat;
    }
/*      
    protected class viewHolder
    {
        TextView tvOk;
        Button btnChecked,btnAlert;
    }
*/
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return subCat.length;
    }
    @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;
    }

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

//      final viewHolder holder = new viewHolder();

        //inflating
        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.lstinfl,null);


        //binding
            final TextView tvOk=(TextView)convertView.findViewById(R.id.tvOk);
            final Button btnChecked=(Button)convertView.findViewById(R.id.btnChecked);
            final Button btnAlert=(Button)convertView.findViewById(R.id.btnAlert);

        //inserting subcategories
        tvOk.setText(subCat[position]);

        //representing subcategories according to their status
        try
        {
            for(Integer k=0;k<subStatus.length;k++)
            {
                // CONFUSED

            }

        }
        catch (Exception e) {
            // TODO: handle exception
        }
        finally
        {

        }

        //listeners to change the Color and Status
        tvOk.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                tvOk.setClickable(false);
                btnChecked.setClickable(true);
                btnAlert.setClickable(true);
                tvOk.setBackgroundColor(Color.GREEN);
                btnChecked.setBackgroundColor(Color.WHITE);
                btnAlert.setBackgroundColor(Color.WHITE);
                subStatus[position]="Ok";
            }
        });

        btnChecked.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tvOk.setClickable(true);
                btnChecked.setClickable(false);
                btnAlert.setClickable(true);
                tvOk.setBackgroundColor(Color.BLACK);
                btnChecked.setBackgroundColor(Color.YELLOW);
                btnAlert.setBackgroundColor(Color.WHITE);
                subStatus[position]="Checked";
//                  action = new Intent(getApplicationContext(), subdescActivity.class);
//                  startActivity(action);

            }
        });

        btnAlert.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tvOk.setClickable(true);
                btnChecked.setClickable(true);
                btnAlert.setClickable(false);
                tvOk.setBackgroundColor(Color.BLACK);
                btnChecked.setBackgroundColor(Color.WHITE);
                btnAlert.setBackgroundColor(Color.RED);
                subStatus[position]="Alert";
//                  action = new Intent(getApplicationContext(), subdescActivity.class);
//                  startActivity(action);
            }
        });


        return convertView;


    }   

}
}
  • 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-06-02T14:07:19+00:00Added an answer on June 2, 2026 at 2:07 pm

    Well, you’re saving both states (text and status) in arrays, so at least that part is good. Now you need to check the states[] in your getView() to set the correct look and feel of your buttons according to it, something like:

              if (subStatus[position].equals("Ok")) {
                tvOk.setClickable(false);
                btnChecked.setClickable(true);
                btnAlert.setClickable(true);
                tvOk.setBackgroundColor(Color.GREEN);
                btnChecked.setBackgroundColor(Color.WHITE);
                btnAlert.setBackgroundColor(Color.WHITE);
                subStatus[position]="Ok";
              }
              ....
    

    Then when you submit you can go through these status items and see what was clicked or not.

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

Sidebar

Related Questions

I have designed an xml file contain nine images i.e.., 3 rows each row
I have designed a standalone application using swings which can read data from HID
I have designed a form where the user can enter his comment using Add
I have designed some classes using Visual Studio class diagramming. Now I would like
I have designed a weighted graph using a normalized adjacency list in mysql. Now
I have designed an activity in which there is a listView. I want that
i have designed an form in that form contain button when i click on
I have designed a dataset using VS2008 dataset designer. In one of the datatables,
I have designed an ERD for a website I am about to build. This
I have designed what is essentially a propertychanged listener - i.e. when Instance.A changes,

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.