1

我有一个列表视图(多选)。它工作完美。我为此使用了自定义适配器。每行都有一个文本和一个复选框。标记的项目将作为结果显示在文本视图中。

我想在 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;
    }

}

}

4

1 回答 1

1

您的代码在 getView(..) 中出现故障

您正在尝试在膨胀视图之前检索您的微调器。如果 convertView 为空(这将是您的列表中的第一次),您的微调器将不会被发现..您不能在为空的东西上调用 findViewById..将您的操作顺序更改为以下

 @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 并在需要时充气之后,我如何将检索微调器移动到

于 2012-11-20T14:52:49.927 回答