XML : Display selected ListView item into TextView

i'm new in stackoverflow as i'm new in android studio, i'm not good at code but i'm trying to,i know this is silly question maybe, but still i need help

so let's get to the point so i have listview to select ".txt" file and get content inside of it, i manage to get code from internet(forget the source) to select ".txt" file but it seems i can't display it on my TextView

so here's the code for my XML

Activity_main.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" >    <Button      android:id="@+id/opendialog"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="Browse" />  <TextView      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:text="input"      android:id="@+id/result"/>  <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="Save"/>    </LinearLayout>    

Here's My ListView

dialoglayout.xml

  <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:padding="20dp"  android:minWidth="300dp" >    <LinearLayout      android:orientation="horizontal"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:layout_margin="10dp" >        <ImageView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:src="@mipmap/ic_launcher" />        <Button          android:id="@+id/up"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:text="Parent folder" />    </LinearLayout>    <TextView      android:id="@+id/folder"      android:layout_width="wrap_content"      android:layout_height="wrap_content" />    <ListView      android:id="@+id/dialoglist"      android:layout_width="match_parent"      android:layout_height="wrap_content">    </ListView>    

MainActivity.java

  package com.android.fileexplorerdemo;    import android.app.Activity;  import android.app.Dialog;  import android.os.Bundle;  import android.os.Environment;  import android.view.View;  import android.widget.AdapterView;  import android.widget.ArrayAdapter;  import android.widget.Button;  import android.widget.ListView;  import android.widget.TextView;  import android.widget.Toast;    import java.io.File;  import java.util.ArrayList;  import java.util.List;      public class MainActivity extends Activity {    Button buttonSearch;  Button buttonBack;  TextView textFolder;    String KEY_TEXTPSS = "TEXTPSS";  static final int CUSTOM_DIALOG_ID = 0;  ListView dialog_ListView;    File root;  File curFolder;    private List<String> fileList = new ArrayList<String>();    @Override  protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);        buttonSearch = (Button) findViewById(R.id.opendialog);      buttonSearch.setOnClickListener(new View.OnClickListener() {          @Override          public void onClick(View v) {              showDialog(CUSTOM_DIALOG_ID);          }      });        root = new   File(Environment.getExternalStorageDirectory().getAbsolutePath());      curFolder = root;  }    @Override  protected Dialog onCreateDialog(int id) {        Dialog dialog = null;        switch (id) {          case CUSTOM_DIALOG_ID:              dialog = new Dialog(MainActivity.this);              dialog.setContentView(R.layout.dialoglayout);              dialog.setTitle("File Explorer");              dialog.setCancelable(true);              dialog.setCanceledOnTouchOutside(true);                textFolder = (TextView) dialog.findViewById(R.id.folder);              buttonBack = (Button) dialog.findViewById(R.id.up);              buttonBack.setOnClickListener(new View.OnClickListener() {                  @Override                  public void onClick(View v) {                      ListDir(curFolder.getParentFile());                  }              });                dialog_ListView = (ListView)   dialog.findViewById(R.id.dialoglist);              dialog_ListView.setOnItemClickListener(new   AdapterView.OnItemClickListener() {                  @Override                  public void onItemClick(AdapterView<?> parent, View view,   int position, long id) {                      File selected = new File(fileList.get(position));                      if(selected.isDirectory()) {                          ListDir(selected);                      } else {                          Toast.makeText(MainActivity.this,      selected.toString() + "selected",                                  Toast.LENGTH_LONG).show();                          dismissDialog(CUSTOM_DIALOG_ID);                      }                  }              });                break;      }      return dialog;  }    @Override  protected void onPrepareDialog(int id, Dialog dialog) {      super.onPrepareDialog(id, dialog);      switch (id) {          case CUSTOM_DIALOG_ID:              ListDir(curFolder);              break;      }  }    void ListDir(File f) {      if(f.equals(root)) {          buttonBack.setEnabled(false);      } else {          buttonBack.setEnabled(true);      }        curFolder = f;      textFolder.setText(f.getPath());        File[] files = f.listFiles();      fileList.clear();        for(File file : files) {          fileList.add(file.getPath());      }        ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,              android.R.layout.simple_list_item_1, fileList);      dialog_ListView.setAdapter(directoryList);  }  }    

so i want to put the content inside selected item ListView, into TextView with ID "result"(at Activity_main.xml), where do i getText and setText for it, thanks

No comments:

Post a Comment