ListView 中的 ArrayAdapter

ArrayAdapter in ListView

我有下面的代码,这个 ListActivity 不起作用,当我使用这种格式的数组 String aaaa = {"...","somthing"}; 时,我知道我确定问题出在我的数组上作品!但我需要使用像我的代码显示的格式 怎么了?

public class ff extends ListActivity {

private String[] aaaa;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.favorite_test);
    setListAdapter(new AA());

    aaaa = new String[10];
    for (int i = 0; i < 10; i++) {

        aaaa[i] = "string" + i;
    }

}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    Intent myIntent = new Intent(ff.this, TestActivity.class);
    startActivity(myIntent);
}


class AA extends ArrayAdapter<String> {

    public AA() {

        super(ff.this, R.layout.games_list_layout, aaaa);

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.games_list_layout, parent, false);
        }

        return row;

    }
}

}

您做错的一件事是,您没有初始化 String[] aaaa 数组。所以在 ArrayAdpater 中使用它之前先初始化它。

setContentView(R.layout.favorite_test);
aaaa = new String[10];
for (int i = 0; i < 10; i++) {
    aaaa[i] = "string" + i;
}
setListAdapter(new AA());