0

当单击按钮时,我试图在列表视图中添加行,但我的应用程序每次都崩溃。这是我的代码:

public class MTCRichGraphicsActivity extends Activity {
int ELEMENT_COUNT = 3;
int position=0;
Button bAddView;
String[] elements = new String[ELEMENT_COUNT];

int r =0 ;   


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    bAddView = (Button) findViewById(R.id.bNewEvent);
   final ListView list = (ListView) findViewById(R.id.list3d);
    bAddView.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch(v.getId())
            {
            case R.id.bNewEvent :
                for (int i = 0; i< ELEMENT_COUNT; i++) {
                    elements[i] = String.valueOf(i);
                }
                final MyAdapter adapter = new MyAdapter(MTCRichGraphicsActivity.this,elements);
                adapter.notifyDataSetChanged();
                //list.setScrollY(currentPosition);
                //list.setTranslationY(currentPosition);
                list.setDivider( null ); 
                list.setAdapter(adapter);

                adapter.notifyDataSetChanged();

                ELEMENT_COUNT = ELEMENT_COUNT + 3;
            }

        }
    });

}

}

在这里,我想在每次单击按钮(即 bNewEvent)时添加 3 行,所以我每次都将 ELEMENT_COUNT 增加 3。第一次运行正常,但是第二次按下按钮时它崩溃了。

这是我的适配器类:

public class MyAdapter extends BaseAdapter {

private final LayoutInflater mInflater;
private final String[] mItems;
TextView t;
ViewHolder holder;
public MyAdapter(Activity c,String[] objects) {
    mInflater = c.getLayoutInflater();
    mItems = objects;
}

public int getCount() {
    return mItems.length;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView t;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.listitem2, parent,false);
}
    holder = new ViewHolder();
    holder.t1 = (TextView) convertView.findViewById(R.id.tv1); 
    holder.t2 = (TextView) convertView.findViewById(R.id.tv2);
    holder.t3 = (TextView) convertView.findViewById(R.id.tv3);
    holder.t4 = (TextView) convertView.findViewById(R.id.tv4);
    holder.t1.setText("Title"+position);
    position = position + 3;

    //((ImageView)convertView).setTextAlignment(1);

    return convertView;
}

@Override
public Object getItem(int position) {
    return mItems[position];
}

@Override
public long getItemId(int position) {
    return position;
}

}

4

2 回答 2

0

你定义

int ELEMENT_COUNT = 3;
String[] elements = new String[ELEMENT_COUNT];

但是你不能调整数组大小

 for (int i = 0; i< ELEMENT_COUNT; i++) {
                elements[i] = String.valueOf(i);
            }

这所以你有例外

将数组更改为列表

喜欢

List<String> element =new ArrayList<String>();



   for (int i = 0; i< ELEMENT_COUNT; i++) {
                element.add("String.valueOf(i)")
            }

和更换适配器也这样

于 2013-03-25T12:29:57.003 回答
0

你不能使用这样的数组..

String[] elements = new String[ELEMENT_COUNT];

导致数组具有与 ELEMENT_COUNT 的初始值相同数量的元素,然后您不能仅将 ELEMENT_COUNT 增加 3 并尝试

        for (int i = 0; i< ELEMENT_COUNT; i++) {
            elements[i] = String.valueOf(i);
        }

因为数组只包含最初定义的元素。您需要将“元素”更改为 ArrayList。

ArrayList<string> elements;
于 2013-03-25T12:24:18.130 回答