Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:41:27+00:00 2026-05-29T17:41:27+00:00

I am new to android coding and was making a project with ListView. I

  • 0

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){

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:41:30+00:00Added an answer on May 29, 2026 at 5:41 pm

    you are calling the ReadFileAsList() method before onCreate().
    this is not allowed.
    change to the following:

    public class HelloListViewActivity extends ListActivity {
        /** Called when the activity is first created. */
    
        static FileOperations fileop=null;
        static final String[] file=null; 
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.fileop = new FileOperations();
            this.file = fileop.ReadFileAsList("Installed_packages.txt");
    
       setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));
    
            ListView lv = getListView();
            lv.setTextFilterEnabled(true);
    
            lv.setOnItemClickListener(new AdapterView.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();
                 }
            });
        }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an android application that will play an mp3 file once a
I'm fairly new to Android/Java and wanted to make an app that displays info
I know that eclipse can do this, can Intellij via the new Android support
I'm moving a project to the new Android Native Development Kit (i.e. JNI) and
1st. i'am new to Android coding :) What I do is I load an
I am new to android development. I'm trying to have my string.xml file add
I am new to Android. I am implementing coding related to tab host. I
Quite new to coding for android but this issue has me tearing my hair
So I'm making this android application, that needs to read data from user-provided CSV
I just started coding a new application for Android and stumble over a warning.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.