Skip to content

How can I get a FileChooser to populate a TextView during a TableRow creation?

Problem: I’m having trouble getting a FileChooser class to populate a TextView during a TableRow creation. I receiving a Invocation Exception in the Android created “looper.java” which appears to be caused by a variable tagTrace=0 being read as “!=0”. So, I’m not sure how I may be able to workaround it.

What I’m trying to do: I’m trying to build on to an existing process. When a user clicks on a “+” button on the header row of a TableLayout, it creates a row with two views: a “Delete” (-) Button in row.child(0) and a TextView in row.child(1). It does this successfully. There is a Singleton class that manages various types of TableRow creations for all the app Actiities.

On one particular Activity exists a Files TableLayout. I want the user, when clicking on the “+” buttion I described above, to launch a FileChooser to capture a file path and populate that path to the TextView child of the row it is creating. However, I’m running into the issue above.

The Looper.java Bug (I think) causing the invocation exception

Looper Bug

The FileChooser

    public class FileChooser extends AppCompatActivity {
        private String fileName;
        private String filePath;
        private final ActivityResultLauncher<Intent> resultLauncher;
    
        public FileChooser(){
            //if(intent==null) Toast.makeText(null, "Intent is Null", Toast.LENGTH_SHORT).show();
            this.resultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
                if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null){
                    Uri uri = result.getData().getData();
                    filePath = uri.getPath();
                }
            });
        }
    
        public String getFileName() {
            return fileName;
        }
    
        public String getFilePath() {
            return filePath;
        }
    
        public ActivityResultLauncher<Intent> getResultLauncher() {
            return resultLauncher;
        }

}

The Method within the Singleton creating the TableRow The “!bold”

public static TableRow setupFilesTableRow(Context context, TableLayout table, String fileID, String fileName, boolean bold) {
    TableRow row = new TableRow(context);
    if(bold) {
        row.addView(setupFilesAddRowButton(context, table));
        row.addView(addRowTextViewToTable(context, fileName, true));
    }
    if (!bold) {
        row.addView(setupDeleteRowButton(context, table));
        
            // Intent and FileChooser to capture a filePath
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");
            FileChooser fileChooser = new FileChooser();
            fileChooser.getResultLauncher().launch(intent);

            // Adding a TextView child to the new TableRow with the captured filePath from the FileChooser
            row.addView(addRowTextViewToTable(context, fileChooser.getFilePath(), false));
            //row.setClickable(true);
        
    }
    return row;
}

Answer

Rather than use an embedded Button within the TableLayout which was causing issues (see above), I simply created an “ADD” button outside the tablelayout to launch the FileChooser and add a TableRow with the captured information. Due to being unfamiliar with Android and still learning the platform, this was my solution, and was probably more logically understood from a user perspective.

    private void setupOnClickActions() {
        btnAddFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                resultLauncher.launch(intent);

            }
        });
}

    intentLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result ->{
                if(result.getResultCode() == Activity.RESULT_OK) {
                    assert result.getData() != null;
                    Sources src = result.getData().getParcelableExtra("source");
                    if(src == null){
                        PopupDialog.AlertMessage(AddNote.this, "Error: Author Choice",
                                "An issue occurred and an author choice was not returned.");
                        return;
                    }
                    setSourceDetails(src);
                    selectedSourceID = src.getSourceID();
                }

            }
    );