0

I want to develop an application to show the captured data of third party application with list and do further action when selecting the file item in list. I don not why setListAdapter is not function and I try to change extends ListActivity but it cause crash on my application below is my code:

package com.example.long123.testlistfile;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.nfc.Tag;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.io.File; 
import java.io.FilenameFilter; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List;


public class listfile extends ListActivity {
    /*ArrayList<String> files = new ArrayList<String>();*/
    private List<String> mainListView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listfile);
        mainListView = (List<String>) findViewById(R.id.mainListView);
        Button btnRefresh = (Button) findViewById(R.id.btnRefresh);

    }


    public void btnRefresh(View v){
        File mfile=new File("/storage/emulated/0/Android/data/jp.co.taosoftware.android.packetcapture/files");
        File[] list=mfile.listFiles();
        mainListView = new ArrayList<String>();
        for( int i=0; i< list.length; i++) {
            mainListView.add(list[i].getName());
        }
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, mainListView));
        /* File[] rootFiles = File.listRoots();
        File storage = Environment.getExternalStorageDirectory();
        for(int i = 0; i <rootFiles.length; i++){
            File[] directory = rootFiles[i].listFiles();
            for (int j = 0; j< directory.length; j++){
                files.add(directory[j].getAbsolutePath());
            }
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(listfile.this,
            android.R.layout.simple_expandable_list_item_1, files);
        mainListView.setAdapter(adapter);*/

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_listfile, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45

1 Answers1

0

This line of code seems to be wrong:

mainListView = (List<String>) findViewById(R.id.mainListView);

First I can see is: List<String> isn't a View. So application probably crash with error of casting type.

Here should be usual ListView as type - in result of findViewById like this:

ListView myListView =  (ListView) findViewById(R.id.mainListView);

When you want to extend ListActivity you should Yous this kind of id declaration inside Your xml layout file.

android:id="@android:id/list"

and you will get your ListView as

ListView listview = this.getListView();

Detailed information can be found here.

Community
  • 1
  • 1
madoxdev
  • 3,770
  • 1
  • 24
  • 39