Reputation: 21
I'm having trouble using the fragments , still I do not understand how they work the fragment .
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.caseb.case_beauty_.R;
import java.util.ArrayList;
import java.util.List;
public class MainFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main,
container, false);
Button btnVersao = (Button) view.findViewById(R.id.btnEnviaSolicita);
btnVersao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<String> listItems = new ArrayList<String>();
listItems.add("Item1");
listItems.add("Item2");
listItems.add("Item3");
final CharSequence[] list = listItems.toArray(new CharSequence[listItems.size()]);
View openDialog = (View) findViewById(R.id.openDialog);
openDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intialize readable sequence of char values
final CharSequence[] dialogList = list;
final AlertDialog.Builder builderDialog = new AlertDialog.Builder(MainFragment.this);
builderDialog.setTitle("Select Item");
int count = dialogList.length;
boolean[] is_checked = new boolean[count];
// Creating multiple selection by using setMutliChoiceItem method
builderDialog.setMultiChoiceItems(dialogList, is_checked,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int whichButton, boolean isChecked) {
}
});
builderDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListView list = ((AlertDialog) dialog).getListView();
// make selected item in the comma seprated string
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < list.getCount(); i++) {
boolean checked = list.isItemChecked(i);
if (checked) {
if (stringBuilder.length() > 0) stringBuilder.append(",");
stringBuilder.append(list.getItemAtPosition(i));
}
}
/*Check string builder is empty or not. If string builder is not empty.
It will display on the screen.
*/
if (stringBuilder.toString().trim().equals("")) {
((TextView) findViewById(R.id.text)).setText("Click here to open Dialog");
stringBuilder.setLength(0);
} else {
((TextView) findViewById(R.id.text)).setText(stringBuilder);
}
}
});
builderDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((TextView) findViewById(R.id.text)).setText("Click here to open Dialog");
}
});
AlertDialog alert = builderDialog.create();
alert.show();
}
});
}
});
return view;
}
}
Upvotes: 0
Views: 252
Reputation: 27515
Change
final AlertDialog.Builder builderDialog = new AlertDialog.Builder(MainFragment.this);
to
final AlertDialog.Builder builderDialog = new AlertDialog.Builder(getActivity());
as it AlertDialog.Builder needs Context
Upvotes: 1