如何使结果在 ListView 上可见?

Posted

技术标签:

【中文标题】如何使结果在 ListView 上可见?【英文标题】:How can I make the result to function to be visible on ListView? 【发布时间】:2015-09-27 15:01:13 【问题描述】:

我想问一下,我怎样才能使函数read() 的结果在活动上对ListView 可见。我尝试了一些适配器,但仍然没有结果。这是我的代码:

public class ScanFileResultsActivity extends ListActivity implements View.OnClickListener 

private static final String TAG = "BC FileManipulator";
String[][] arrays = read();


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan_file_results);


public String[][] read() 
    try 
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/barcodedata");
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        final String filename = prefs.getString("prefdbname", null) + ".xls";
        File myFile = new File(myDir, filename);

        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        Workbook workbook = Workbook.getWorkbook(myFile);

        Sheet s = workbook.getSheet(0);
        int rows = s.getRows();

        String[][] result = new String[rows][];
        for (int i = 0; i < rows; i++) 
            Cell[] row = s.getRow(i);

            result[i] = new String[row.length];
            for (int j = 0; j < row.length; j++) 
                result[i][j] = row[j].getContents();
            
        
        return result;

     catch (IOException | BiffException e) 
        Log.v(TAG, "Error");
    
    return null;

...

【问题讨论】:

还有一个问题:您希望如何将 mathix 显示为列表?将数据格式化为 List 或简单数组... 发布您的适配器代码 【参考方案1】:

您应该使用ListAdapter 之一。尝试实现 BaseAdapter。类似的东西:

private class ActivityMy extends ListActivity 
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);
        setListAdapter(new CustomAdapter(this, readData()));

    

    private List<DataItem> readData() 
        //TODO: Your read method....
        return null;
    

    private class DataItem 
        public String name; //For example
        public String age;
    

和适配器:

    public class CustomAdapter extends BaseAdapter 
        private Context context;
        private List<DataItem> items;

        public CustomAdapter(Context context, List<DataItem> items) 
            this.context = context;
            this.items = items;
        

        @Override
        public int getCount() 
            return items.size();
        

        @Override
        public Object getItem(int position) 
            return items.get(position);
        

        @Override
        public long getItemId(int position) 
            return position;
        

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
            ViewHolder viewHolder;

            if(convertView==null)
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                convertView = inflater.inflate(listItemLayoutId, parent, false);

                viewHolder = new ViewHolder();
                viewHolder.nameField = (TextView) convertView.findViewById(R.id.nameField);
                viewHolder.ageField = (TextView) convertView.findViewById(R.id.ageField);
                convertView.setTag(viewHolder);

            else
                viewHolder = (ViewHolder) convertView.getTag();
            

            DataItem objectItem = (DataItem) getItem(position);

            if(objectItem != null) 
                viewHolder.nameField.setText(objectItem.name);
                viewHolder.ageField.setTag(objectItem.age);
            
            return convertView;
        

        private class ViewHolder 
            private TextView nameField;
            private TextView ageField;
        
    

【讨论】:

以上是关于如何使结果在 ListView 上可见?的主要内容,如果未能解决你的问题,请参考以下文章

如何滚动 ListView 中的项目使其可见?

ListView 使 textview 在超出屏幕可见性时可见

如何在自定义 ListView 中设置按钮的可见性?

带有自定义 CellFactory 的 ListView 修剪不可见节点

Android ListView 不会在滚动时重绘

如何在同一个 Activity onCreate() 中执行 WebView 和 ListView?