列表视图中的微调器

时间:2012-11-20 14:40:01

标签: android listview android-listview spinner android-checkbox

我有一个listview(多选)。 它完美无缺。我使用了自定义适配器。每行都有一个文本和一个复选框。 标记的项目将在textview中显示为结果。

我想在listview行中添加一个微调器。我得到零点异常错误。 提前谢谢。

public class MainActivity extends Activity {

ListView myListView;
Button getResult;
Spinner spinner;

private ArrayList<String> dayOfWeekList = new ArrayList<String>();

private void initDayOfWeekList(){
    dayOfWeekList.add("Sunday");
    dayOfWeekList.add("Monday");
    dayOfWeekList.add("Tuesday");
    dayOfWeekList.add("Wednesday");
    dayOfWeekList.add("Thursday");
    dayOfWeekList.add("Friday");
    dayOfWeekList.add("Saturday");

}

String[] androidBooks = 
    {
    "Hello",
    "Professional",
    "Unlocking"
    };

MyArrayAdapter myArrayAdapter;
ArrayAdapter<String> spinnerAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initDayOfWeekList();
    setContentView(R.layout.activity_main);
    spinnerAdapter = new ArrayAdapter<String> (this, 
            android.R.layout.simple_spinner_item, androidBooks);




    myListView = (ListView)findViewById(R.id.list);

    myArrayAdapter = new MyArrayAdapter(
            this,
            R.layout.row,
            android.R.id.text1,
            dayOfWeekList
            );

    myListView.setAdapter(myArrayAdapter);
    myListView.setOnItemClickListener(myOnItemClickListener);

    getResult = (Button)findViewById(R.id.getresult);
    getResult.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            String result = "";

            /*
            //getCheckedItemPositions
            List<Integer> resultList = myArrayAdapter.getCheckedItemPositions();
            for(int i = 0; i < resultList.size(); i++){
                result += String.valueOf(resultList.get(i)) + " ";
            }
            */

            //getCheckedItems
            List<String> resultList = myArrayAdapter.getCheckedItems();
            for(int i = 0; i < resultList.size(); i++){
                result += String.valueOf(resultList.get(i)) + "\n";
            }

            myArrayAdapter.getCheckedItemPositions().toString();
            Toast.makeText(
                    getApplicationContext(), 
                    result, 
                    Toast.LENGTH_LONG).show();
        }});

}

OnItemClickListener myOnItemClickListener
= new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        myArrayAdapter.toggleChecked(position);

    }};

private class MyArrayAdapter extends ArrayAdapter<String>{

    private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();

    public MyArrayAdapter(Context context, int resource,
            int textViewResourceId, List<String> objects) {
        super(context, resource, textViewResourceId, objects);

        for(int i = 0; i < objects.size(); i++){
            myChecked.put(i, false);
        }
    }

    public void toggleChecked(int position){
        if(myChecked.get(position)){
            myChecked.put(position, false);
        }else{
            myChecked.put(position, true);
        }

        notifyDataSetChanged();
    }

    public List<Integer> getCheckedItemPositions(){
        List<Integer> checkedItemPositions = new ArrayList<Integer>();

        for(int i = 0; i < myChecked.size(); i++){
            if (myChecked.get(i)){
                (checkedItemPositions).add(i);
            }
        }

        return checkedItemPositions;
    }

    public List<String> getCheckedItems(){
        List<String> checkedItems = new ArrayList<String>();

        for(int i = 0; i < myChecked.size(); i++){
            if (myChecked.get(i)){
                (checkedItems).add(dayOfWeekList.get(i));
            }
        }

        return checkedItems;
    }

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

        spinner = (Spinner) row.findViewById(R.id.planets_spinner);
        spinner.setAdapter(spinnerAdapter);

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

        CheckedTextView checkedTextView =    (CheckedTextView)row.findViewById(R.id.text1);
        checkedTextView.setText(dayOfWeekList.get(position));

        Boolean checked = myChecked.get(position);
        if (checked != null) {
            checkedTextView.setChecked(checked);
        }

        return row;
    }

}

}

1 个答案:

答案 0 :(得分:1)

在getView(..)

中,您的代码无序

你试图在给视图充气之前检索你的微调器..如果convertView为null(它将是第一次通过你的列表)你的微调器将无法找到..你不能在某些东西上调用findViewById null ..将操作的顺序更改为以下

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

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

        spinner = (Spinner) row.findViewById(R.id.planets_spinner);
        spinner.setAdapter(spinnerAdapter);
        CheckedTextView checkedTextView =    (CheckedTextView)row.findViewById(R.id.text1);
        checkedTextView.setText(dayOfWeekList.get(position));

        Boolean checked = myChecked.get(position);
        if (checked != null) {
            checkedTextView.setChecked(checked);
        }

        return row;
    }

注意我是如何移动检索微调器后检查视图是否为null并在需要时进行膨胀