public class HomeFragmentextends Fragment {
Viewroot;
LinearLayoutlinearLayout;
Listlist =new ArrayList<>();
APPDataappData;
@NonNull
public View onCreateView(@NonNull LayoutInflater inflater,
@NonNull ViewGroup container,@NonNull Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_home, container,false);
linearLayout =root.findViewById(R.id.addli);
appData = (APPData) getActivity().getApplicationContext();
try {
//显示需要显示的按钮
assert appData.logisticsName !=null;
list.addAll(Arrays.asList(appData.logisticsName).subList(0,appData.s_length));
setButton(list);
}catch (Exception e) {
e.printStackTrace();
}
@SuppressLint("CutPasteId")
final ImageView im =root.findViewById(R.id.img_add);
im.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
showSingleAlertDialog();
}catch (Exception e) {
e.printStackTrace();
}
}
});
return root;
}
/**
* @param list data list
* @serialData show select button
**/
private void setButton(final List list) {
if (list.size() >0) {
for (int i =0; i < list.size(); i++) {
Button btn =new Button(getActivity());
//btn.setLayoutParams(linearLayout.getLayoutParams());
btn.setText(list.get(i));
btn.setBackgroundColor(Color.WHITE);
btn.setTag(i);
final int finalI = i;
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(getContext(), YTActivity.class);
assert appData.logisticsName !=null;
intent.putExtra("companyName",appData.logisticsName[finalI]);
startActivity(intent);
}
});
linearLayout.addView(btn);
}
}
}
/**
* show dialog
*
* @param
* @return null
*/
public void showSingleAlertDialog() {
final APPData appData = (APPData) getContext().getApplicationContext();
final String[] items = appData.logisticsNames;
final AlertDialog.Builder alertBuilder =new AlertDialog.Builder(getContext());
alertBuilder.setTitle("请选择快递公司");
alertBuilder.setSingleChoiceItems(items,0,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,int i) {
assert items !=null;
Toast.makeText(getContext(),items[i], Toast.LENGTH_SHORT).show();
final LinearLayout linearLayout =root.findViewById(R.id.addli);
final Button button =new Button(getContext());
assert appData.logisticsNames !=null;
String cm =appData.logisticsNames[i];
int logisticsId =appData.id;
assert appData.idss !=null;
int servicesUserCpId =appData.idss[i];
APPData appData1 = (APPData) getActivity().getApplicationContext();
appData1.index[i] +=1;
int id =0;
int smsCount =0;
addCompany(id, logisticsId, servicesUserCpId, smsCount);
if (appData1.index[i] >1) {
Toast.makeText(getContext(),"已经添加", Toast.LENGTH_SHORT).show();
// button.setVisibility(View.INVISIBL
if (getActivity().isFinishing()) {
System.exit(0);
}
}else {
//第一次添加
button.setText(cm);
linearLayout.addView(button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO other
Intent i =new Intent(getContext(), YTActivity.class);
i.putExtra("companyName",button.getText().toString());
startActivity(i);
}
});
}
}
});
alertBuilder.show();
}