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.
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) {
Then from your Activity, use this: