简单列表项多项选择而不选择项 [英] Simple list item multiple choice not selecting items

查看:60
本文介绍了简单列表项多项选择而不选择项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个数组列表,并在列表视图中以简单的列表项多项选择显示它,但是我无法检查或打勾列表中的项目,当我单击项目时什么也没有发生.请检查下面的代码,并告诉我我做错了.

I've created an array list and display it in a list view with simple list item multiple choice but i cannot check or tick the items on the list, when i click on the items nothing happens. Please check my code below and tell me what i am doing wrong.

package com.example.arrays;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

public class MainActivity extends Activity {
ListView showList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView show = (TextView)findViewById(R.id.txtShow);
    final Random generate = new Random();
    showList = (ListView)findViewById(R.id.listView1);
    final String[] myAttraction = new String[4];
    myAttraction[0]= "Walter Sisulu National Botanical Garden ";
    myAttraction[1]= "Coca-Cola Dome";
    myAttraction[2]= "Promusica Theatre";
    myAttraction[3]= "Unisa Science Campus";
    Button arrays = (Button)findViewById(R.id.button1);
    arrays.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            /*int random = generate.nextInt(4);
            String display = myAttraction[random];
            show.setText(display);*/

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, myAttraction);
            showList.setAdapter(adapter);

        }
    });
    showList.setOnItemLongClickListener(new OnItemLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), "long clicked pos: " + pos, Toast.LENGTH_LONG).show();

            return true;
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

 }

推荐答案

添加这样的OnItemClickListener以便在用户单击某项时选中/取消选中CheckedTextView

Add a OnItemClickListener like this to check/uncheck the CheckedTextView when user click on an item

showList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // change the checkbox state
            CheckedTextView checkedTextView = ((CheckedTextView)view);
            checkedTextView.setChecked(!checkedTextView.isChecked());
        }
    });

这篇关于简单列表项多项选择而不选择项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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