Android listview适配器无法显示内容

时间:2015-10-22 21:22:08

标签: java android listview adapter

我不知道问题出在哪里因为一切都很好。没有错误显示或没有。我甚至在适配器数据上做了一个System.out.println()来获取它的计数并显示出来。问题是,listview没有显示任何内容。

这是我的代码:(非常感谢任何指针/帮助)需要帮助的适配器是 answers_adapter

InstanceUniforms

CustomListData

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_question);
        c = this;

        //define view controls
        question = (EditText) findViewById(R.id.question);
        answers = (ListView) findViewById(R.id.answers);
        type = (Spinner) findViewById(R.id.type);
        add_choice = (Button) findViewById(R.id.add_choice);
        info = (TextView) findViewById(R.id.info);
        optional = (CheckBox) findViewById(R.id.optional);

        //default data preparation
        add_choice.setEnabled(false);
        final List<String> types = new ArrayList<>();
        types.add("Free Text");
        types.add("Free Number");
        types.add("Date");
        types.add("MultiChoice Text");
        types.add("MultiChoice Images");
        ArrayAdapter<String> types_adapter = new ArrayAdapter<String>(c,android.R.layout.simple_spinner_item, types);
        types_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        type.setAdapter(types_adapter);
        type.setSelection(0);
        type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selection = types.get(position).trim().toLowerCase();
                if (selection.contains("multi")) {
                    add_choice.setEnabled(true);
                } else {
                    add_choice.setEnabled(false);
                }

                //reset the answer choices
                answers_adapter.clear();
                answers_adapter.notifyDataSetChanged();
                info.setText("Answer Choices (" + answers_adapter.getCount() + ")");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                type.setSelection(0);
            }
        });

        answers_data = new ArrayList<>(); //Initialize array adapter data

        //if this is a question edit, add question info
        String editpredata = getIntent().getStringExtra(NewQuestions.EXTRA_MESSAGE);
        editpredata_pos = Integer.parseInt(editpredata);
        if (editpredata_pos > -1){
            CustomListData epcd = NewQuestions.questions_adapter.getItem(editpredata_pos);
            String epcd_QJSON = epcd.getMoreinfo();
            try {
                JSONObject epcdqobj = new JSONObject(epcd_QJSON);
                question.setText(epcdqobj.getString("question"));
                int types_pos = 0;
                for (int i = 0;i<types.size();i++){
                    String type_str = types.get(i);
                    if (type_str.trim().toLowerCase().matches(epcdqobj.getString("type").trim().toLowerCase())){
                        types_pos = i;
                    }
                }
                type.setSelection(types_pos);
                optional.setChecked(epcdqobj.getString("optional").toLowerCase().trim().matches("yes"));
                if (epcdqobj.getString("multichoice_type").trim().length() > 0){
                    JSONArray epcdmulti = epcdqobj.getJSONArray("multichoices");
                    for (int i=0;i<epcdmulti.length();i++){
                        JSONObject spobj = epcdmulti.getJSONObject(i);
                        CustomListData epcdmcd = new CustomListData();
                        int epcdmcd_imgpos = isNumeric(spobj.getString("image").trim()) ? Integer.parseInt(spobj.getString("image")) : -1;
                        epcdmcd.setIcon(epcdmcd_imgpos < 0 ? R.drawable.choice : NewQuestion.ds[epcdmcd_imgpos]);
                        epcdmcd.setItem(spobj.getString("choice"));
                        epcdmcd.setDesc(spobj.getString("points"));
                        epcdmcd.setMoreinfo(spobj.getString("image").trim());
                        answers_data.add(epcdmcd);
                    }
                }
            }
            catch (Exception e){
                System.out.println("INTERNAL ERROR: --> (PREPARING DATA) " + t.exceptionString(e));
            }
        }

        System.out.println(answers_data.size());
        answers_adapter = new CustomListAdapter(((Activity)c), answers_data);
        answers.setAdapter(answers_adapter);
        answers.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showDialogChoice("", position);
            }
        });
        add_choice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String selection = type.getSelectedItem().toString().toLowerCase().trim();
                if (selection.contains("image")) {
                    showDialogChoice("image", -1);
                } else {
                    showDialogChoice("", -1);
                }
            }
        });
    }

0 个答案:

没有答案