LifeCommander 2023-05-27 20:24 采纳率: 80%
浏览 11

Android的ArrayAdapter用法问题

代码:

img


图中ArrayAdapter(Context context…)的context在onCreate(Bundle savedInstanceState)里面用SelectionActivity.this还管用,一到自己写的方法里面就报错了。

应该怎么写呢?

  • 写回答

1条回答 默认 最新

  • uyoj661 2023-05-27 20:34
    关注

    1.最普通的添加文本;

    activity中的代码

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    ListView listView = (ListView) this.findViewById(R.id.listview2);

    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
    new String[]{"湖南","湖北","荆州","监利","岳阳","随手","北京","广州","上海","哈尔冰","大庆","黑龙江"});
    listView.setAdapter(adapter);

    }
    2,arraylistadapter实现的单选按钮
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_single_choice,//单选模式
    new String[]{"湖南","湖北","荆州","监利","岳阳","随手","北京","广州","上海","哈尔冰","大庆","黑龙江"});
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//单选模式
    listView.setAdapter(adapter);
    3.arrayadapter实现的多选按钮
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice,//多选
    new String[]{"湖南","湖北","荆州","监利","岳阳","随手","北京","广州","上海","哈尔冰","大庆","黑龙江"});
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//多选
    listView.setAdapter(adapter);

    评论

报告相同问题?

问题事件

  • 创建了问题 5月27日

悬赏问题

  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥15 clion的参数提示怎么关闭
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀
  • ¥15 mifare plus卡认证