[在Android中更新CSV文件后更新Listview

来源:爱站网时间:2021-09-25编辑:网友分享
[我的Android应用当前正在从CSV文件读取数据并显示在列表视图中,但是,一旦我向csv文件中添加了另一行,我希望该应用在...上更新新添加的值...

问题描述


我的Android应用当前正在从CSV文件读取数据并显示在列表视图中,但是一旦我将另一行添加到csv文件,我希望该应用程序在单击时更新新添加的值刷新按钮。怎么做 ?任何帮助,将不胜感激

下面是代码

MainActivity Class

```public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private ItemArrayAdapter itemArrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //init();
        listView = (ListView) findViewById(R.id.list_View);
        itemArrayAdapter = new ItemArrayAdapter(getApplicationContext(), R.layout.activity_premier_rostatus);

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

        InputStream inputStream = getResources().openRawResource(R.raw.stats);
        CSVFile csvFile = new CSVFile(inputStream);
        List scoreList = csvFile.read();

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

    }
}```

ItemArrayAdapter类

    private List scoreList = new ArrayList();

    static class ItemViewHolder {
        TextView name;
        TextView score;
    }

    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.activity_premier_rostatus, parent,false);
            viewHolder = new ItemViewHolder();
            viewHolder.name =  row.findViewById(R.id.name);
            viewHolder.score = 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;
    }
}```


**CSVFile Class**

```public class CSVFile {
    InputStream inputStream;

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

    public List read(){
        List resultList = new ArrayList();
        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;
    }
}```

**activity_main.xml**



 ```android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list_View"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/margin_top"```



思路:


根据需要,如果您只想添加一项,则可以使用现有的适配器功能add,也可以实现另一种适配器功能来一次设置所有列表。

[需要时,可以使用新项目或列表执行此add或新功能,然后需要调用notifyDataSetChanged

itemArrayAdapter.add( NewItem); itemArrayAdapter.notifyDataSetChanged();

itemArrayAdapter.addAllItemsInATime( NewArray); itemArrayAdapter.notifyDataSetChanged();

在后一种情况下,您需要像这样实现新的addAllItemsInATime(或所需名称)功能:

public void addAllItemsInATime( List newList) { this.scoreList = newList; }

考虑将Adapter类作为保存数据并知道如何显示此数据的对象。因此,任何更改的数据都必须保存在适配器中并重新绘制。

上一篇:我的代码太复杂了,还是我的笔记本电脑不能运行它?

下一篇:为什么我在Java中遇到堆空间错误?

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载