我如何使用两个csv文件来填充android中的两个视图

时间:2014-11-25 20:51:55

标签: android csv view

我有两个执行以下操作的csv文件: 1.第一个填充显示问题的列表视图。这些行是一个onlcick监听器,当按下时,会产生第二个视图。 2.第二个CSV文件在第一个视图中有问题的答案。

我的问题是:我无法得到第二个视图中显示的答案。 弹出第二个视图,但它没有数据。

以下是我的答案适配器代码:

public class AnswerArrayAdapter extends ArrayAdapter<String[]> {
    private List<String[]> answerList = new ArrayList<String[]>();

    static class AnswerViewHolder {
        TextView name2;
        TextView answertext;
    }

    public AnswerArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    @Override
    public void add(String[] object) {
        answerList.add(object);
        super.add(object);
    }

    @Override
    public int getCount() {
        return this.answerList.size();
    }

    @Override
    public String[] getItem(int index) {
        return this.answerList.get(index);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        AnswerViewHolder viewHolder;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.screen_popup, parent, false);
            viewHolder = new AnswerViewHolder();
            viewHolder.name2 = (TextView) row.findViewById(R.id.name2);
            viewHolder.answertext = (TextView) row.findViewById(R.id.answertext);
            row.setTag(viewHolder);
        } else {
            viewHolder = (AnswerViewHolder)row.getTag();
        }
        String[] stat = getItem(position);
        viewHolder.name2.setText(stat[0]);
        viewHolder.answertext.setText(stat[1]);
        return row;
    }
}

以下是主要活动:

    public class MainActivity extends Activity {
        private ListView listView;
        private ItemArrayAdapter itemArrayAdapter;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = (ListView) findViewById(R.id.listView);
            itemArrayAdapter = new ItemArrayAdapter(getApplicationContext(), R.layout.item_layout);

            Parcelable state = listView.onSaveInstanceState();
            listView.setAdapter(itemArrayAdapter);
            listView.onRestoreInstanceState(state);

            listView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {

                    // selected item
                    //String answer = ((TextView) view).getText().toString();

                    View textview = view.findViewById(R.id.score);

                    String answer = textview.getContext().toString();
                    // Launching new Activity on selecting single List Item
                    Intent i = new Intent(getApplicationContext(), AnswerPopupWin.class);
                    // sending data to new activity
                    i.putExtra("answer", answer);
                    startActivity(i);


                }
            });

            InputStream inputStream = getResources().openRawResource(R.raw.islamicfatwahuthameen);
            CSVFile csvFile = new CSVFile(inputStream);
            List<String[]> scoreList = csvFile.read();

            for(String[] scoreData:scoreList ) {
                itemArrayAdapter.add(scoreData);
            }

        }

}

以下是答案的课程:

public class AnswerPopupWin extends Activity {

    private ListView answerView;
    private AnswerArrayAdapter answerArrayAdapter;
    private View view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.screen_popup);
        answerView = (ListView) findViewById(R.id.answerView);
        answerArrayAdapter = new AnswerArrayAdapter(getApplicationContext(), layout.screen_popup);

        //View textview = view.findViewById(id.name2);

    }
}

非常感谢任何帮助。 我想问更好的问题。对不起,如果这看起来像一个愚蠢的问题。

ironmantis7x

----更新----- 这是另一个数组适配器文件::

ublic class ItemArrayAdapter extends ArrayAdapter<String[]> {
    private List<String[]> scoreList = new ArrayList<String[]>();

    static class ItemViewHolder {
        TextView name;
        TextView score;
        TextView answer;
    }

    public ItemArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    @Override
    public void add(String[] object) {
        scoreList.add(object);
        super.add(object);
    }

    @Override
    public int getCount() {
        return this.scoreList.size();
    }

    @Override
    public String[] getItem(int index) {
        return this.scoreList.get(index);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ItemViewHolder viewHolder;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.item_layout, parent, false);
            viewHolder = new ItemViewHolder();
            viewHolder.name = (TextView) row.findViewById(R.id.name);
            viewHolder.score = (TextView) row.findViewById(R.id.score);
            row.setTag(viewHolder);
        } else {
            viewHolder = (ItemViewHolder)row.getTag();
        }
        String[] stat = getItem(position);
        viewHolder.name.setText(stat[0]);
        viewHolder.score.setText(stat[1]);
        return row;

    }
}

1 个答案:

答案 0 :(得分:1)

将Answers添加到适配器时,似乎缺少代码片段。你应该有类似的东西:

AnswerArrayAdapter answerArrayAdapter = 
    new AnswerArrayAdapter(getApplicationContext(), layout.screen_popup);

InputStream inputStream = getResources().openRawResource(R.raw.my_answers_file_resource);
CSVFile csvFile = new CSVFile(inputStream);
List<String[]> answerList = csvFile.read();
for(String[] answer : answerList ) {
    answerArrayAdapter.add(answer);
}