0

是否可以从Textview充气@android:id/text1?我不想创建自己的布局,我只想得到一些修改过的文本。

这是我的代码:

首先,我创建了变量来存储数据

private List<HashMap<String, String>> dataCities = new ArrayList<HashMap<String, String>>();
//Hashmap with keys and values
//id - 0
//name - default

其次,我在 onCreate 中创建了自定义适配器

@Override
protected void onCreate(Bundle savedInstanceState) {
//...
Spinner citiesSpinner = (Spinner) findViewById(R.id.city_sp);
citiesAdapter = new CustomArrayAdapter(this, R.layout.sherlock_spinner_item, dataCities);
citiesSpinner.setAdapter(citiesAdapter);
}

第三,我创建了我的听众。它可以工作,但在调用 notifyDataSetChanged 后什么也没有发生。为什么?

@Override
public void onRequestJsonResponded(RequestType type, JSONArray array) {
    //my enum type
    switch (type) {
        case cities:
            //returns hashmap in arraylist, ArrayList<HashMap<String,String>> ...
            dataCities = parseJSonArray(array);
            Log.d(TAG, "End of parsing");
            citiesAdapter.notifyDataSetChanged();
            break;
        case mark:
            //...
            break;
        case model:
            break;
    }
}

这是我的自定义阵列适配器

private class CustomArrayAdapter extends ArrayAdapter<HashMap<String, String>> {

    public CustomArrayAdapter(Context context, int textViewResourceId, List<HashMap<String, String>> objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        View v = layoutInflater.inflate(R.layout.sherlock_spinner_item, null);
        TextView tv = (TextView)v.findViewById(android.R.id.text1);
        tv.setText(getItem(position).get("name"));

        return v;

    }

}

有人能告诉我为什么我会得到空白微调器数据吗?(微调器是空的)。以及如何在不创建新布局的情况下获得修改后的文本?我只想使用夏洛克微调器项目布局。请帮忙。

4

1 回答 1

0

很简单,适配器中的列表与您检索到的列表不同:

        //In here you update your activity's list to the returned values
        dataCities = parseJSonArray(array);
        Log.d(TAG, "End of parsing");
        //But the adapter is using the original value of dataCities (new ArrayList<HashMap<String, String>>() )
        citiesAdapter.notifyDataSetChanged();

由于您的适配器依赖于 ArrayAdapter,因此最简单的解决方案可能是在接收到数据时创建一个新适配器。

于 2013-05-19T21:42:52.767 回答