I am new to android coding and was making a project with ListView. I wrote a program that displays information in a text file in a listview. Right now I have my main file extending ListActivity and everything works. The main file is calling another class
fileop.ReadFileAsList(“Installed_packages.txt”);
which reads each line of the text file. What I want is to make this a method in a class called FileOperations and do exactly what it is doing now, but my main class in my other project Extends Activity and I do not know how to “call” Extend Listactivity AND Activity. I don’t think this can be done in Java probably for good reason. Could anyone show me how I would/should refactor it? Main file followed by code below:
Bottom line I am trying to make this a method in fileop and move fileop to a new project where the main extends Activity and not ListActivity.
MAIN:
package com.example.hellolistview;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class HelloListViewActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, file));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
static FileOperations fileop= new FileOperations();
static final String[] file =fileop.ReadFileAsList("Installed_packages.txt");
}
package com.example.hellolistview;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import android.os.Environment;
import android.util.Log;
public class FileOperations {
public String[] ReadFileAsList(String fileName){
try{
File f = new File(Environment.getExternalStorageDirectory()
+ "/Nullwall/" + fileName);
FileInputStream fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(
fileIS));
StringBuilder DbLines = new StringBuilder();
String line = buf.readLine();
while (buf.readLine() != null)
{
DbLines.append(line);
DbLines.append("\r\n");
}
String[] ListItems = DbLines.toString().split("\r\n");
return ListItems;
}
catch (FileNotFoundException e) {
e.printStackTrace();
Log.e("FileOp_ReadFileAsList","File Not Found in ReadFileAsList()");
}
catch (IOException e) {
e.printStackTrace();
Log.e("FileOp_ReadFileAsList","IOException in ReadFileAsList()");
}
Log.e("FileOp_ReadFileAsList","Probably an Error in ReadFileAsList()");
return null;
}
}
EDIT:
My new Main file (Where I want to go):
package com.IPR2.viewlog;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;
public class Main extends Activity {
private TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
FileOperations fileOperations = new FileOperations();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.TextView01);
//fileOperations.ClearFile("Installed_packages.txt");
ApplicationOperations.ListAllInstalledApplications(getApplicationContext());
ReadFileAsList("Installed_packages.txt"); <--What Im trying to be able to do. It wont let me
}
public String[] ReadFileAsList(String fileName){
}
you are calling the
ReadFileAsList()method beforeonCreate().this is not allowed.
change to the following: