2011-10-19 85 views
0

坏resaults我有一个String数组,我想显示其在列表中的项目。 我有一个row.xml包含一个表,并在每一行中我有三个TextView。 我想要的是,显示每一行中的每个三项数组。Android上,使用阵列适配器

样本:String[] str = {"1", "2", "3", "4", "5", "6"}; 每行应该是这样的:

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

我的XML代码:

<?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> 
中的OnCreate(),我把(我有一些其他的意见和列表视图)

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

,这是我的适配器:

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

我的第一个问题是,我没有看到我想要的结果。第二个是,我已定义的“令牌”作为字符串数组在我以这种方式类(private static String[] tokens = {"", "", ""};)的顶部,当我运行应用程序,它显示我三行用相同的结果。 但如果我改变定义如Private static String[] tokens;程序崩溃。 我被头痛:(如果可能的话告诉我,是我的错

感谢

+0

的第一个解决方案,我认为你应该尝试类似,如果(位置= 0){数= 0} tvContDate.setText(count); tvContNo.setText(count + 1); tvContAmount.setText(count + 2);计数+ = 3;并声明count为int count = 0;作为类(您的活动)数据成员。如果我错了,让我知道它。谢谢。:-) – user370305

+0

谢谢。这是一个棘手的想法。我测试应用程序崩溃。我写的代码:如果(位置== 0) \t \t \t \t计数= 0; \t \t \t tvContDate.setText(tokens [position * count]); \t \t \t tvContNo。setText(tokens [position * count + 1]); \t \t \t tvContAmount.setText(tokens [position * count + 2]); \t \t \t count = 3; – Hesam

+0

我认为每次迭代后我们都不能添加3个计数,但如果我们将3分配给它,就没关系。 thans再次 – Hesam

回答

1

当你使用?的

static String[] tokens; 

代替:

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

令牌不初始化,这意味着令牌[n]是零,导致飞机坠毁。 在您的意见,做记号[*位计数+ 1]将很快走出数组的边界的 - 如果您像上面那样使用了tokens = {“”,“”,“”},则数组中只有3个元素。

当您通过令牌进入这一行的超级构造函数:

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

你说应该有清单3排,因为有数组中的3个元素。对于第三个项目,position = 3 * count = 3 == boom将进入阵列。 传递对象的数组(或ArrayList中)进入超级构造函数:解决

一种方式

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