如何向点击的列表项显示特定的CSV内容?

时间:2015-02-10 12:07:09

标签: android csv android-listview import

我尝试向点击的列表项显示特定的CSV内容,但使用我当前的代码,我只能显示整个CSV文件。例如,当我单击级别2时,将显示所有CVS列表,但我在此处尝试执行的操作仅显示CSV的2级元素。

这是我的第一个观点:

enter image description here
这是我点击第2级时获得的视图:

Exercises list

但是在这里,还会显示1级CSV元素,如何才能显示2级元素?当"等级1"单击,如何只显示1级元素?等...

MainActivity.java

public class MainActivity extends ListActivity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // storing string resources into Array
    // R.array.levels is in list_levels.xml
    String[] levels = getResources().getStringArray(R.array.levels);

    // Binding resources Array to ListAdapter

    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label_level, levels));

    ListView lv = getListView();

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

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

            // Launching new Activity on selecting single List Item
            Intent i = new Intent(getApplicationContext(), SingleListItem.class);
            // sending data to new activity
            i.putExtra("list", list);
            startActivity(i);
        }
    });
}

}

这是我的文件 CSVFile.java

    public class CSVFile {
    InputStream inputStream;

    public CSVFile(InputStream inputStream){
        this.inputStream = inputStream;
    }

    public List<String[]> read(){
        List<String[]> resultList = new ArrayList<String[]>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        try {
            String csvLine;
            while ((csvLine = reader.readLine()) != null) {
                String[] row = csvLine.split(",");
                resultList.add(row);
            }
        }
        catch (IOException ex) {
            throw new RuntimeException("Error in reading CSV file: "+ex);
        }
        finally {
            try {
                inputStream.close();
            }
            catch (IOException e) {
                throw new RuntimeException("Error while closing input stream: "+e);
            }
        }
        return resultList;
    }
}

我的 ItemArrayAdapter.java

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

    static class ItemViewHolder {
        TextView level_number;
        TextView stimuli_name;
    }

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

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

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

    @Override
    public String[] getItem(int index) {
        return this.stimuliList.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.single_list_item_view, parent, false);
            viewHolder = new ItemViewHolder();
            viewHolder.level_number = (TextView) row.findViewById(R.id.level_number);
            viewHolder.stimuli_name = (TextView) row.findViewById(R.id.stimuli_name);
            row.setTag(viewHolder);
        } else {
            viewHolder = (ItemViewHolder)row.getTag();
        }
        String[] stat = getItem(position);
        viewHolder.level_number.setText(stat[0]);
        viewHolder.stimuli_name.setText(stat[1]);
        return row;
    }
}

SingleListItem.java

    public class SingleListItem extends Activity{

    private ListView listView;
private ItemArrayAdapter itemArrayAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.single_list_item_view);


        TextView txtList = (TextView) findViewById(R.id.list_label);

        Intent i = getIntent();
        // getting attached intent data
        String list = i.getStringExtra("list");
        // displaying selected list name
        txtList.setText(list);


        listView = (ListView) findViewById(R.id.stimuli_list);
        itemArrayAdapter = new ItemArrayAdapter(getApplicationContext(), R.layout.single_list_item_view);

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

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

        for(String[] stimuliData:stimuliList ) {
            itemArrayAdapter.add(stimuliData );
        }

    }
}

最后,快速查看我的CSV文件 stimulis.csv

level 1,feu
level 1,fête
level 2,blanc
level 2,blague
...

这可能吗?

1 个答案:

答案 0 :(得分:1)

我担心这是不可能的。