Good day! Recently I started learning programming in android, and decided to start working with apache poi. The data is excellent, everything is gorgeous. But there is one problem - the data is displayed via system.out.println. And it is necessary that they were on the textView. What should I do, how should I be? I understand that I am very, very noob in this, but I can’t do anything. I really want to know. Help me please. Here is the code:

package com.example.tests_excel; import android.support.v7.app.ActionBarActivity; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { Workbook wb = WorkbookFactory.create(getAssets().open("test.xls")); Sheet sheet = wb.getSheetAt(0); for(int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) { Row row = sheet.getRow(i); Cell name = row.getCell(0); Cell age = row.getCell(1); System.out.println(name.getStringCellValue() +" "+ age.getNumericCellValue()); } //TextView = (TextView)findViewById(R.id.TextView1); //textView.setText(name.getStringCellValue() +" "+ age.getNumericCellValue()); } catch(Exception ex) { return; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } } 

    2 answers 2

    In standard Android, the System.out system stream is redirected to LogCat, although this is not true for all devices. Sometimes it goes straight to /dev/null

    In principle, you can redirect the system stream to another device / buffer. This is done via setOut () , although I'm not sure that the axis will not throw an exception - of type SecurityException .

    If the redirection works out, then you need to create your own class that inherits from PrintStream and has an internal buffer, where your output will be saved from System.out , then the buffer can be output to TextView .

    But in general, these are crutches.

      System.out.println accepts regular strings, as does the setText method of TextView . Moreover, the code that you commented out in the onCreate method does just about what you need. You only need to put the entire output together using StringBuilder and display it on the UI:

       @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try { StringBuilder output = new StringBuilder(); Workbook wb = WorkbookFactory.create(getAssets().open("test.xls")); Sheet sheet = wb.getSheetAt(0); for(int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) { Row row = sheet.getRow(i); Cell name = row.getCell(0); Cell age = row.getCell(1); output .append(name.getStringCellValue()) .append(' ') .append(age.getNumericCellValue()) .append('\n'); } TextView = (TextView)findViewById(R.id.TextView1); textView.setText(output.toString()); } catch(Exception ex) { return; } } 

      Provided, of course, that your activity_main.xml markup file really contains the TextView with android:id="@+id/TextView1" visible to the user android:id="@+id/TextView1" .