Search code examples
androidlistviewandroid-activityandroid-arrayadaptersamsung-mobile

Java activity names when getApplicationContext().fileList()


On this activity in my app,it will call geApplication().fileList() to list all the files in the apps file.

In my Elephone P8000 phone, I manage to create new files without any error, but when I tried it on my Samsung phone, this problem pops up

enter image description here

The files with the "rList-asia.sumikawa" are files I did not create.

In fact, judging on the names at the end of the file, "MainActivity", those are my activity's name.

How do I fix this?

Edit

my full-ish Java codes

public class addressActivity extends Activity {

ListView listView;

String[] SavedFiles;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_address);

    listView = (ListView) findViewById(R.id.listView);

    addData();
    editData();
    ShowSavedFiles();
    editContent();
    setMultiDelete();
    listView.setOnItemClickListener(getURLOnItemClickListener);
}

void ShowSavedFiles() {
    //SavedFiles = getApplicationContext().fileList();
    //SavedFiles = getApplication().fileList();
    SavedFiles = addressActivity.this.fileList();
    ArrayAdapter<String> adapter
            = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            SavedFiles);

    listView.setAdapter(adapter);
    if (adapter.isEmpty()) {
        Toast.makeText(getBaseContext(), "There is no Address !", Toast.LENGTH_LONG).show();
    }
}

OnItemClickListener getURLOnItemClickListener
        = new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        // TODO Auto-generated method stub
        String clickedFile = (String) parent.getItemAtPosition(position);
        getURL(clickedFile);
    }

};

void getURL(final String file){
    if (clickAble == true){
        FileInputStream fis;
        String content = "";
        try {
            fis = openFileInput(file);
            byte[] input = new byte[fis.available()];
            while (fis.read(input) != -1) {}
            content += new String(input);
            //Toast.makeText(getBaseContext(),content,Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Intent i = new Intent(addressActivity.this, liveActivity.class);
        String strName = content.toString();
        i.putExtra("urlAddress", strName);
        startActivity(i);
    }
}

OnItemClickListener getFileEditContent = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String clickedFile = (String)parent.getItemAtPosition(position);
        setMultiEdit(clickedFile);
        // change the checkbox state
        CheckedTextView checkedTextView = ((CheckedTextView) view);
        if (checkedTextView.isChecked()) {

            ((CheckedTextView)view).setChecked(true);
            checkedTextView.setChecked(!checkedTextView.isChecked());
            utils.myPosInt.clear();
            utils.myPos.clear();
            utils.myPosTitle.clear();

        } else {
            for(int i=0; i<listView.getChildCount();i++)
            {
                view = listView.getChildAt(i);
                ((CheckedTextView)view).setChecked(false);
            }

            checkedTextView.setChecked(!checkedTextView.isChecked());
            utils.myPosTitle.add(clickedFile);
        }

    }
};

Solution

  • here's my code

    Test.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ListView
            android:id="@+id/test_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

    Test.java

    public class Test extends AppCompatActivity
    {
        @Bind(R.id.test_list)
        ListView listView;
        String[] SavedFiles;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test);
            ButterKnife.bind(this);
    
            ShowSavedFiles();
        }
        void ShowSavedFiles()
        {
            SavedFiles = getApplicationContext().fileList();
            ArrayAdapter<String> adapter
                    = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,
                    SavedFiles);
    
            listView.setAdapter(adapter);
            if (adapter.isEmpty()) {
                Toast.makeText(getBaseContext(), "There is no Address !", Toast.LENGTH_LONG).show();
            }
        }
    
    }
    

    Output

    output

    from docs

    fileList()

    fileList() : Returns an array of strings naming the private files associated with this Context's application package.

    Alternate

    String[] iFileList = YourActivityName.this.fileList();
    

    Hope this helps..

    UPDATE

    Try this alternate solution

        String dataDr=getApplicationInfo().dataDir;
        Log.i(TAG,"data dir"+dataDr);
    
        showDirFile(dataDr);
    
    
        void showDirFile(String dirpth)
        {
            String path = dirpth+"/files";
            Log.d("Files", "Path: " + path);
            File f = new File(path);
            File file[] = f.listFiles();
            Log.d("Files", "Size: "+ file.length);
    
            SavedFiles=new String[file.length];
            for (int i=0; i < file.length; i++)
            {
                Log.d("Files", "FileName:" + file[i].getName());
    
                SavedFiles[i]=file[i].getName();
            }
    
            ArrayAdapter<String> adapter
                    = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,
                    SavedFiles);
    
            listView.setAdapter(adapter);
    
    
        }