Hesam
Hesam

Reputation: 53610

android, Array adapter doesn't show contents of rows

In my application i have a String array that i store my strings in it.

private static String[] tokens = new String[1024];

Format of my row which i create it in xml file is:

<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 defined:

lvOutstanding = (ListView) findViewById(R.id.outstanding_list);
        myListAdapter = new MyListAdapter();
        lvOutstanding.setAdapter(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;

            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);

            if(position == 0)
                count = 0;
            else
                count = 3;

            if(arrayLoop>0){
                tvContDate.setText(tokens[position * count]);
                Log.i("Count:", tokens[position * count]);

                tvContNo.setText(tokens[position * count + 1]);
                Log.i("Count:", tokens[position * count +1]);

                tvContAmount.setText(tokens[position * count + 2]);
                Log.i("Count:", tokens[position * count+2]);

                arrayLoop -= 3;
                Log.i("Count:", String.valueOf(arrayLoop));
            }

            return(row);
        }
    }

as you saw, in each row i have three textView and i want to show each three items of array in each row. "arrayLoop" is and int variable that saved number of items in the "tokens[]" array.

Now, when i run the application, emulator shows 1024 rows without any data. where is my mistakes? when i check the logcat (Thanks google for new beautiful design of logCat!), there is no problem and first 12 items in array has parameters and there for (12/3=4), i should see 4 rows with information. However, i have 1024 rows without information :(

enter image description here

Upvotes: 0

Views: 494

Answers (2)

Vivek
Vivek

Reputation: 4260

You need to write as follow if tokens tokens array is not string type.

    if(arrayLoop>0){

        tvContDate.setText(String.valueOf(tokens[position * count]));
        Log.i("Count:", tokens[position * count]);

        tvContNo.setText(String.valueOf(tokens[position * count]));
        Log.i("Count:", tokens[position * count +1]);

        tvContAmount.setText(String.valueOf(tokens[position * count]));
        Log.i("Count:", tokens[position * count+2]);

        arrayLoop -= 3;
        Log.i("Count:", String.valueOf(arrayLoop));
    }

Because when you use this tvContAmount.setText(tokens[position * count]); it will think that it is resource id. It will not get this and hence it won't show any output.

Upvotes: 1

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30845

Your arrayLoop variable must be 0 or less. I think that your problem is where you've put arrayLoop in the if statement you meant to put count. But I can't see where you've defined arrayLoop or what you've assigned to it.

Upvotes: 0

Related Questions