I can't get a simple listview to display data. I know the listview is getting the data because empty listitems are displayed according to the number of data elements given to the arrayadapter. I am attempting to show the list in a alert dialog so maybe thats causing the problem. I don't know. Can anyone help me?
public class FileExplorer extends DialogFragment {
private AlertDialog.Builder builder;
private List<File> files;
private List<String>fileList;
private FileIO fIO;
ListActivity listActivity;
ListView lv;
ArrayAdapter<String> arrayAdapter;
Button btnFileNavigatorBack;
public FileExplorer(){
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
fIO = new FileIO();
fileList = new ArrayList<String>();
files = fIO.getDirectoryContents(null);
for(File f : files){
fileList.add(f.getName());
}
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_file, (ViewGroup)getActivity().getCurrentFocus());
lv = (ListView)view.findViewById(R.id.lvFileExplorer);
arrayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_expandable_list_item_1,
fileList);
builder = new AlertDialog.Builder(getActivity());
builder.setView(view)
.setPositiveButton(R.string.dialog_button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton(R.string.dialog_button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
btnFileNavigatorBack = (Button)view.findViewById(R.id.btnFileNavigatorBack);
btnFileNavigatorBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity().getApplicationContext(), ":D", Toast.LENGTH_LONG).show();
}
});
Dialog dialog = builder.create();
dialog.show();
lv.setAdapter(arrayAdapter);
return dialog;
}
}
XML file for listview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
<GridLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:rowCount="2">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/btnFileNavigatorBack"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView6"
android:layout_marginLeft="125dp" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lvFileExplorer"
android:layout_columnSpan="2" />
</GridLayout>
I'm new to android programming. Coding for listview seems like it should be intuitive but I can't make sense of this.
0 comments:
Post a Comment