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