0

I have one activity/java file (Browsefile.java) that would obtain the absolute path of the file. I want to pass this information to another java file for further processing (Sqlitefun.java). In the first stage, I just want to make sure the variable of file path is passed from Browsefile.java to Sqlitefun.java so I just create an alertdialog in the Sqlitefun.java file to test it. However, I have some issue on the context of the alertdialog object.

(As Sqlitefun.java would further perform i/o and Sqlite processing tasks, I prefer to put this in another file.)

Here are the codes for the files:

Browsefile.java

public class Browsefile extends ListActivity {
....

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.browsefile);
    findViews(); 
    getDir(root);

}
....
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {

    File file = new File(path.get(position));

    if (file.isDirectory())
    {
        if(file.canRead()) 
        {
            getDir(path.get(position));
        }
        else
        {
            selectpath = file.getAbsolutePath();
            fpath.setText(selectpath);  
        }
    }
    else
    {
        selectpath = file.getAbsolutePath();
        fpath.setText(selectpath);
    }       
}       

private Button.OnClickListener importcsv = new Button.OnClickListener() {
    public void onClick(View v) {
        Sqlitefun firstClass = new Sqlitefun();
            firstClass.getsAlertDialog(selectpath); 
    }
};
....
}

Sqlitefun.java

public class Sqlitefun {

private Context context;

public void getsAlertDialog(String filepath) {

    new AlertDialog.Builder(context)
    .setMessage(filepath)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
        }
    })
    .show();
}

}

I have tried to use this, Sqlitefun.this to replace context in the line new AlertDialog.Builder(context) but none of this works. (Eclipse said The constructor AlertDialog.Builder(Sqlitefun) is undefined and did not allow me to compile. The above code did not have any error and allow me to compile, but there is a nullpointer exception for the Context.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kwytse
  • 113
  • 2
  • 15

5 Answers5

1

I believe you need to show dialogs from the Activity you are currently in, so you would need to the put AlertDialog code in BrowserFile.

I'm not sure if this would work, but you could try passing the context from BrowserFile to SqliteFun and showing it there.

Additionally, if you're not set on using an AlertDialog, trying using a Toast notification instead. They generally do better when used outside of an Activity.

Edit: I don't think the following is the best way to implement what you are trying to do, but here is a code sample I wrote

In SqliteFun, modify your method as such:

public void getsAlertDialog(String filepath, Context mContext) {

new AlertDialog.Builder(mContext)
.setMessage(filepath)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
    }
})
.show();
} 

Then from your Activity, use this:

private Button.OnClickListener importcsv = new Button.OnClickListener() {
public void onClick(View v) {
    Sqlitefun firstClass = new Sqlitefun();
        firstClass.getsAlertDialog(selectpath, v.getContext()); 
}
};
coder
  • 10,460
  • 17
  • 72
  • 125
  • I have replaced the AlertDialog to Toast as u suggested but the same problem exist. Eclipse suggested the error "The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (Sqlitefun, String, int) arguments (Sqlitefun, String, int) – kwytse Dec 20 '11 at 15:51
  • You still need a context - trying passing the context from BrowserFile to Sqlitefun – coder Dec 20 '11 at 16:10
  • Is it possible that you can give me some sample code on passing the context. My Browsefile is suggested as above and you can expect a single line for the Toast as follows: static String filepath; public void getsAlertDialog() { filepath = Browsefile.selectpath; Toast.makeText(Sqlitefun.this, filepath, Toast.LENGTH_SHORT).show(); } – kwytse Dec 20 '11 at 16:18
  • Great! The above code works. I will do more research on context. I will accept your answer. Many thanks again. – kwytse Dec 20 '11 at 16:47
0

Try this:

private Context context = getApplicationContext()
DoDu
  • 44
  • 2
  • Dodu: I change to your code, the error is now: The method getApplicationContext() is undefined for the type Sqlitefun – kwytse Dec 20 '11 at 15:21
  • LalaRaider: Can I ask why it is a must to extends from Activity for Sqlitefun? (You can point to the reference material and I can read on this.) – kwytse Dec 20 '11 at 15:23
  • Like LalaRaider said, I did not see that Sqlitefun does not extend from activity. An explanation about what Context is, can be found here: http://stackoverflow.com/questions/3572463/what-is-context-in-android – DoDu Dec 20 '11 at 15:30
  • thanks Dodu and LalaRaider. My intention is to pass the variable from 1 file/ java class to another. If it is the case, I would not use alertdialog to test whether the variable is passed or not. – kwytse Dec 20 '11 at 15:34
0

You need to give contex from activity you execute this method. So I suggest you to add constructor to Sqlitefun class and when you create object of this class add context to arguments.

Greg
  • 1,152
  • 1
  • 12
  • 28
0

First create a static variable in Sqlitefun.java.

Static String path;

After that you can access this variable from any class so you can directly store that path from Browse.class

Ex: You got the fullpath="XXXX" in browse.class. To store the fullpath variable in Sqlitefun.java use:

Sqlitefun.path=fullpath;
Chris
  • 8,030
  • 4
  • 37
  • 56
Reddy
  • 140
  • 1
  • 5
0

There is something wrong in your app design.

You cannot really show a Dialog from another class than an activity class (well, you can, but it's very dangerous because the activity context can change anytime. For example when the device is rotated your app will crash because the activity context changed, since in Sqlitefun your reference still points to the old context).

So you should find another way to show your dialog. For example, you can create some getters to retrieve the values to show on your AlertDialog, and create it inside your activity.

Dalmas
  • 26,409
  • 9
  • 67
  • 80