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 7997841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:03:51+00:00 2026-06-04T15:03:51+00:00

I have recently started making an Android app that requires a list of 20

  • 0

I have recently started making an Android app that requires a list of 20 names and counters for each individual name, both displayed on the screen. Every time someone taps a name, the counter for that name increases by one. This means that the name and the counter need to be ‘connected’ in some way.

I started off by creating a basic list of names using a ListView. This is my code:

public class ExampleActivity extends ListActivity { 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] NAMES = getResources().getStringArray(R.array.names_array);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, NAMES));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) //Shows toast with name
                Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Here’s the list_item.xml file:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

Now I’m looking for a way to display the counters in the list. The most user friendly one in my opinion would be a list in which the names are displayed on the left (which is currently the case) and their counters on the right (in one line with the names; alignment in a list is not my problem).

However, since I’m just a beginner in Android, I’m not sure how to pull this off. My first problem is that I don’t know how to somehow merge my list of 20 names with a list of 20 integers and make these integers specific for each name. My second issue is how to display all this on the screen.

  • 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-04T15:03:52+00:00Added an answer on June 4, 2026 at 3:03 pm

    Organize your row data(name + counter) in a simple Model class:

    class Model {
        String name;
        int counter;
    
        Model(String name, int counter) {
            this.name = name;
            this.counter = counter;
        }
    
        @Override
        public String toString() {
           return name;
        } 
    }
    

    Then construct a row layout to show the name and counter:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/counter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />
    
    </RelativeLayout>
    

    Finally bind the Model data to the row layout:

    public class ModelAdapter extends ArrayAdapter<Model> {
    
            private ArrayList<Model> data;
    
            public ModelAdapter(Context context, int resource,
                    int textViewResourceId, ArrayList<Model> objects) {
                super(context, resource, textViewResourceId, objects);
                data = objects;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // let the adapter bind the name to the list
                View v = super.getView(position, convertView, parent);
                // find the counter TextView so we can update it's value
                TextView counterTv = (TextView) v.findViewById(R.id.counter);
                //get the data from the list for this row.
                Model obj = data.get(position);
                //set the counter value for this row
                counterTv.setText(String.valueOf(obj.counter));
                return v;
            }       
    
        }
    

    To use your new adapter:

       //...
        String[] NAMES = getResources().getStringArray(R.array.names_array);
        // construct the list of model object for your rows:
        ArrayList<Model> items = new ArrayList<Model>();
        for (int i = 0; i < 20; i++) {
            items.add(new Model(NAMES[i], 0));// I guess the counter starts at 0
        }
        mAdapter = new ModelAdapter(this, R.layout.list_item, R.id.name, items);
        setListAdapter();
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);      
    
    //...
    

    The ListActivity already has the OnItemClickListener implemented, is the onListItemClick method:

    public void onListItemClick(ListView parent, View view, int position, long id) {
                            Model clickedRowData = parent.getItemAtPosition(position);
                            clickedRowData.counter++;
                            // notify the adapter that something was updated
                            mAdapter.notifyDataSetChanged();
                        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started making my app last year, but have recently had that HOLY ****
I have recently started to learn Objective-C and write my tests using OCUnit that
I have recently started to feel that I need to greatly improve my C++
I recently started making a month view similar to Android 4.0's Calendar View. I
My wife recently started a business making soap bars and the soap labels have
I've just recently started programming for the iPhone and I'm making an application that
I have recently started making a C++ Wrapper of GTK+ (Nothing special, just wrapping
I have an application developed on the iphone. I have recently started making it
I recently started making web-based apps with JQTouch. In this app, when a button
I have recently started making (and am still making) the transition from ActionScript 2

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.