chechbox不显示在GridView的文本 [英] chechbox not displaying text in gridview

查看:160
本文介绍了chechbox不显示在GridView的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在网格视图中显示动态复选框,但没有显示文本。它只有当我检查/取消选择它突出显示文本。
我知道我的问题是不显示同样喜欢 Android的复选框文字。但我没有找到解决这个问题的方式。

下面是我的code。

活动code:

  {尝试
    JSONArray JA =新JSONArray(result3); // result3为响应
                                            //服务器
    JSONObject的JSON = NULL;
    最终的String [] = STR3新的String [JA.length()];
    的for(int i = 0; I< JA.length();我++){
        JSON = JA.getJSONObject(ⅰ);
        STR3 [I] = json.getString(名称);
    }    ArrayAdapter<串GT; ADP3 =​​新ArrayAdapter<串GT;(getApplicationContext(),android.R.layout.simple_list_item_multiple_choice,STR3);
    GridView控件的GridView =(GridView控件)findViewById(R.id.name);
    gridView.setAdapter(ADP3);}赶上(例外五){
    Log.e(故障3,e.toString());
}

布局XML:

 < GridView的机器人:ID =@ + ID /名称
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:为numColumns =auto_fit
        机器人:choiceMode =multipleChoice
        >< / GridView的>


如果你想用它来显示不是一个简单的TextView任何其他子类一个ArrayAdapter。

您可以然后初始化查看您的自定义适配器的GetView()方法。

下面是ArrayAdapter的一个基本的子类的实例。

 公共类CustomAdapter扩展ArrayAdapter<串GT;
{
    私人LayoutInflater吹气;
    私人诠释RESOURCEID;
    私人列表<串GT;数据;    公共CustomAdapter(上下文的背景下,诠释RESOURCEID,列表与LT;弦乐>数据)
    {
        超级(上下文,RESOURCEID,数据);
        充气= LayoutInflater.from(上下文);
        this.resourceId = RESOURCEID;
        this.data =数据;
    }    @覆盖
    公共查看getView(INT位置,查看视图的ViewGroup父)
    {
        如果(查看== NULL)
            鉴于= inflater.inflate(RESOURCEID,NULL);
        字符串data_item =(字符串)((ListView控件)母公司).getItemAtPosition(位置);
        复选框复选框=((复选框)view.findViewById(R.id.check_box));
        //做你的事
        返回视图。
    }
}

I am trying to display dynamic Checkbox in grid view but it not showing the text. It only highlight the text when I check/uncheck it. I know my question is same like Android CheckBox text not displaying. But I didn't find the way to solve it.

Here is my code.

Activity Code:

try {
    JSONArray JA = new JSONArray(result3);// result3 is response from
                                            // server
    JSONObject json = null;
    final String[] str3 = new String[JA.length()];
    for (int i = 0; i < JA.length(); i++) {
        json = JA.getJSONObject(i);
        str3[i] = json.getString("name");
    }

    ArrayAdapter<String> adp3 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_multiple_choice, str3);
    GridView gridView = (GridView) findViewById(R.id.name);
    gridView.setAdapter(adp3);

} catch (Exception e) {
    Log.e("Fail 3", e.toString());
}

Layout XML:

<GridView android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numColumns="auto_fit"
        android:choiceMode="multipleChoice"
        ></GridView>

解决方案

Subclass the ArrayAdapter if you want to use it to display anything other than a simple TextView.

You can then initialize the View in the GetView() method of your custom Adapter.

Here's an example of a basic subclassing of ArrayAdapter.

public class CustomAdapter extends ArrayAdapter<String>
{
    private LayoutInflater inflater;
    private int resourceId;
    private List<String> data;

    public CustomAdapter(Context context, int resourceId, List<String> data)
    {
        super(context, resourceId, data);
        inflater = LayoutInflater.from(context);
        this.resourceId = resourceId;
        this.data = data;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent)
    {
        if (view == null)
            view = inflater.inflate(resourceId, null);
        String data_item = (String) ((ListView) parent).getItemAtPosition(position);
        CheckBox checkBox = ((CheckBox) view.findViewById(R.id.check_box));
        // Do your thing
        return view;
    }
}

这篇关于chechbox不显示在GridView的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆