Java - TextView引用可扩展列表视图子中的另一个TextView

时间:2013-05-17 12:12:05

标签: java android textview

我有疑问。我在Java / Android中编写List Adapter。我重写了返回View的方法(它是Clicked项目中的消耗列表视图)。问题是:

Log.i告诉我:

  • 05-17 14:04:50.494:I / createTextView(21790):0 06 Salame
  • 05-17 14:04:50.504:I / createTextView(21790):1 11 Pancetta

但TextViews看起来都是:

  • 1 11 Pancetta
  • 1 11 Pancetta

看起来TextView引用了最后一个TextView。如何解决?

@Override
              public View getGroupView(int groupPosition, boolean isExpanded,
                           View convertView, ViewGroup parent) {
                  Log.e("SecondLevelAdapter", "getGroupView");
                  TextView tv = new TextView(OrderFirstGridPage.this);

                   if((myArrayProductList.get(groupPosition).get(0).getFlgZlozony() == 1)) {

                       Log.i("createTextView", String.valueOf(counterProductName)+" "+myArrayProductList.get(groupPosition).get(counterProductName).toString());
                       tv.setText(myArrayProductList.get(groupPosition).get(counterProductName).toString());
                       counterProductName++;
                           }
                   tv.setPadding(22, 7, 7, 7);
                   tv.setGravity(Gravity.LEFT); 
                   tv.setTextAppearance(OrderFirstGridPage.this, R.style.TextLarge);
                   tv.setTextSize(25);

                     return tv;
              } 

1 个答案:

答案 0 :(得分:0)

我认为使用LayoutInflater会更好:

      @Override
  public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
    ViewGroup parent) {

   if (convertView == null) {

    LayoutInflater inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   convertView = inf.inflate(R.layout.yourLayout, null);

  }

  TextView tv = (TextView) convertView.findViewById(R.id.yourTextViewId);
   if((myArrayProductList.get(groupPosition).get(0).getFlgZlozony() == 1)) {

                   Log.i("createTextView", String.valueOf(counterProductName)+" "+myArrayProductList.get(groupPosition).get(counterProductName).toString());
                   tv.setText(myArrayProductList.get(groupPosition).get(counterProductName).toString());
                   counterProductName++;
                       }
  tv.setPadding(22, 7, 7, 7);
               tv.setGravity(Gravity.LEFT); 
               tv.setTextAppearance(OrderFirstGridPage.this, R.style.TextLarge);
               tv.setTextSize(25);

 return convertView;
}