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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:14:29+00:00 2026-05-26T08:14:29+00:00

I have a String array and I want to show its items in a

  • 0

I have a String array and I want to show its items in a list.
I have a row.xml which contains a table and in each row i have three TextView.
The things that i want is, showing each three items of array in each row.

sample: String[] str = {"1", "2", "3", "4", "5", "6"};
Each row should be like this:

str[1]----str[2]----str[3]
str[4]----str[5]----str[6]

my xml code is:

<?xml version="1.0" encoding="utf-8"?>

<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"  
android:stretchColumns="*" >
    <TableRow 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:layout_marginBottom="10dip" >
        <TextView
            android:id="@+id/outstanding_contractdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:textSize="15sp" />
        <TextView
            android:id="@+id/outstanding_contractno"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:textSize="15sp" />
        <TextView
            android:id="@+id/outstanding_contractamount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:textColor="#ffffff"
            android:textSize="15sp" />
    </TableRow>

</TableLayout>

in OnCreate() i put(I have some other views and a listView):

lvOutstanding = (ListView) findViewById(R.id.outstanding_list);
lvOutstanding.setAdapter(new MyListAdapter());

and this is my adapter:

class MyListAdapter extends ArrayAdapter<String>{
        MyListAdapter(){
            super(MSOutStanding.this, R.layout.list_outstanding, tokens);
        }

        public View getView(int position, View convertView, ViewGroup parent){
            View row = convertView;

            //String str = llData.get(position);
            //String[] tokens = str.split(",");

            if(row == null){
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.list_outstanding, parent, false);
            }

            TextView tvContDate = (TextView) row.findViewById(R.id.outstanding_contractdate);
            TextView tvContNo = (TextView) row.findViewById(R.id.outstanding_contractno);   
            TextView tvContAmount = (TextView) row.findViewById(R.id.outstanding_contractamount);

            tvContDate.setText(tokens[0]);
            tvContNo.setText(tokens[1]);
            tvContAmount.setText(tokens[2]);

            return(row);
        }
    }

My first problem is, i don’t see result as i want. and the second is, i have defined “tokens” as string array in top of my class (private static String[] tokens = {"", "", ""};) in this way, when i run the application, it shows me three rows with same results.
but if i change definition such as Private static String[] tokens; the program crashes.
I got headache 🙁 if it’s possible tell me where is my fault?

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-26T08:14:30+00:00Added an answer on May 26, 2026 at 8:14 am

    When you use:

    static String[] tokens;
    

    instead of:

    static String[] tokens = { "", "", "" };
    

    tokens isn’t initialised, meaning tokens[n] is null, causing a crash.
    In your comments, doing tokens[position * count + 1] will quickly go out of the bounds of the array – if you used tokens = { “”, “”, “” } as above, there are only 3 elements inside the array.

    When you pass tokens into the super constructor with this line:

    super(MSOutStanding.this, R.layout.list_outstanding, tokens);
    

    You’re saying that there should be 3 rows in the list, since there are 3 elements in the array. For the 3rd item, position = 3 * count = 3 == boom goes the array.

    One way to solve:
    Pass an array (or ArrayList) of objects into the super constructor:

    public class Contract {
            String date;
            String no;
            String amount;
    
            Contract(String d, String n, String a) {
                date = d;
                no = n;
                amount = a;
            }    
        }
    
    Contract[] contracts = { new Contract("1", "2", "3"), new Contract("4", "5", "6") };
        class MyListAdapter extends ArrayAdapter<Contract>{
             MyListAdapter(){
                    super(MSOutStanding.this, R.layout.list_outstanding, contracts);
                }
        }
        ...
    
        public View getView(int position, View convertView, ViewGroup parent){
            ...
            Contract c = getItem(position);
            tvContDate.setText(c.date);
            tvContNo.setText(c.no);
            tvContAmount.setText(c.amount);
    ...
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string that I want to chop to array of substrings of given
I have an array of strings plus one additional string. I want to use
I need a dynamic string-array. All the examples I have seen show hard-coded string-arrays.
I have an application which downloads some data and I want to show that
I have an array: string[] exceptions = new string[] { one, two, one_1, three
I have an array of strings: @array I want to concatenate all strings beginning
I have an array of strings that I want to use for button titles
Specifically, I have an array of strings called val, and want to replace all
I have a string array in C# and I intend to copy it in
I have a string array: string[] authors = new string[3]; authors[0] = Charles Dickens;

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.