我的CustomAdapter中有一个数组列表,其中填充了我选中元素的ID。
elementChecker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (elementChecker.isChecked()) {
checkedElements.add(id);
} else {
for (int i = 0; i < checkedElements.size() ; i++){
checkedElements.remove(id);
}
}
}
});
但是如何从我的片段中访问此列表?感谢您的帮助。
编辑:这是我的傻瓜但我不能从我的片段中调用它。
public List<String> getSelectedElements(){
return checkedElements;
}
适配器在我的DataListFragment中设置:
final ListAdapter dataListAdapter = new CustomListAdapter(context, dataListArray);
final ListView dataListListView = (ListView) view.findViewById(R.id.listView_datalist);
dataListListView.setAdapter(dataListAdapter);
EDIT2:添加了片段代码
public class ListViewFragment extends Fragment {
DbHelper mydb;
Spinner locationpicker;
LinearLayout qrStickerLayout;
ImageView qrCodeSticker;
TextView labelSticker;
TextView serialSticker;
private EditText roomnr;
public boolean roomNrOk = false;
public boolean locationOk = false;
public boolean listViewOk = false;
String actualLocation;
String dataList = "";
String selectedFromList;
String[] selectedElementArray;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_listview, container, false);
getActivity().setTitle(getString(R.string.listView));
registerForContextMenu(view);
final Context context = getContext();
mydb = new DbHelper(context);
locationpicker = (Spinner) view.findViewById(R.id.spinner_locations);
roomnr = (EditText) view.findViewById(R.id.editText_roomNr);
qrCodeSticker = (ImageView) view.findViewById(R.id.imageView_qrcode);
labelSticker = (TextView) view.findViewById(R.id.textView_QrSticker_label);
serialSticker = (TextView) view.findViewById(R.id.textView_QrSticker_serial);
qrStickerLayout = (LinearLayout) view.findViewById(R.id.linearLayout_QrCode);
Drawable drawableForFabSave = getResources().getDrawable(R.drawable.ic_save);
((MainActivity) getActivity()).fabsave.setImageDrawable(drawableForFabSave);
Drawable drawableForFabAdd = getResources().getDrawable(R.drawable.ic_add);
((MainActivity) getActivity()).fabmain.setImageDrawable(drawableForFabAdd);
((MainActivity) getActivity()).fabmain.animate().translationY(0);
((MainActivity) getActivity()).fabmain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity) getActivity()).scanQRCode();
((MainActivity) getActivity()).loadListViewFragment();
}
});
((MainActivity) getActivity()).fabdelete.animate().translationY(0);
final String locationNamesFromTableArray = mydb.getLocationNames();
if(!locationNamesFromTableArray.isEmpty()){
String[] roomNumbersFromTableArrayFinal = locationNamesFromTableArray.split("\t");
ArrayAdapter<String> adapter_locations;
adapter_locations = new ArrayAdapter<>(context, android.R.layout.simple_spinner_dropdown_item, roomNumbersFromTableArrayFinal);
locationpicker.setAdapter(adapter_locations);
setFabdeleteVisible(listViewOk);
}
locationpicker.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
actualLocation = locationpicker.getSelectedItem().toString();
locationOk = true;
setFabsVisible(roomNrOk, true, listViewOk);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
roomnr.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
Pattern p = Pattern.compile("^[0-9]{3}$");
Matcher m = p.matcher(s);
roomNrOk = m.find();
setFabsVisible(roomNrOk, locationOk, listViewOk);
}
});
dataList = mydb.getElementsWithoutRoom();
if (!dataList.isEmpty()) {
final String[] dataListArray = dataList.split("\n");
final ListAdapter dataListAdapter = new CustomListAdapter(context, dataListArray);
final ListView dataListListView = (ListView) view.findViewById(R.id.listView_datalist);
dataListListView.setAdapter(dataListAdapter);
dataListListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedFromList = (dataListListView.getItemAtPosition(position).toString());
selectedElementArray = selectedFromList.split("\t");
dataListListView.showContextMenu();
}
});
((MainActivity) getActivity()).fabdelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mydb.deleteAllWithoutRoom();
listViewOk = false;
dataListListView.setAdapter(dataListAdapter);
Toast toast = Toast.makeText(context, getResources().getString(R.string.toast_deleted), Toast.LENGTH_SHORT);
toast.show();
reloadListViewFragment();
}
});
}else{
TextView emptyElement = (TextView) view.findViewById(R.id.emptyElement);
emptyElement.setVisibility(View.VISIBLE);
}
((MainActivity) getActivity()).fabsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (roomNrOk && locationOk && !dataList.isEmpty()) {
String successfullSaved = getResources().getString(R.string.successfully)+ actualLocation + "-" + roomnr.getText().toString() + getResources().getString(R.string.saved);
mydb.setRoomNr(roomnr.getText().toString());
mydb.setRoomId(roomnr.getText().toString());
mydb.setLocationId(actualLocation);
Toast toast = Toast.makeText(getActivity(), successfullSaved, Toast.LENGTH_SHORT);
toast.show();
reloadListViewFragment();
} else {
Toast toast = Toast.makeText(getActivity(), getString(R.string.error_listView), Toast.LENGTH_SHORT);
toast.show();
}
}
});
((MainActivity) getActivity()).fabexport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
File myDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + getResources().getString(R.string.sdcard_path));
if (!myDir.exists()) {
myDir.mkdir();
}
String fileName = getResources().getString(R.string.roomname) + actualLocation + getResources().getString(R.string.underline) + roomnr.getText().toString() + getResources().getString(R.string.roomname_ending);
File file = new File(myDir, fileName);
file.createNewFile();
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter fos = new OutputStreamWriter(fOut);
fos.write(dataList);
fos.close();
fOut.close();
Toast toast = Toast.makeText(getActivity(), fileName + getResources().getString(R.string.saved), Toast.LENGTH_SHORT);
toast.show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
return view;
}
public void reloadListViewFragment(){
Fragment fragment;
FragmentTransaction ft = getFragmentManager().beginTransaction();
fragment = new ListViewFragment();
ft.replace(R.id.container, fragment);
ft.commitAllowingStateLoss();
}
}
答案 0 :(得分:2)
行。我明白了,问题是因为dataListAdapter
属于ListAdapter
类型,您使用CustomListAdapter
进行初始化。因此,它给出了错误无法解决方法。要解决此问题,您可以采用以下两种方式: -
final ListAdapter dataListAdapter = new CustomListAdapter(context, dataListArray);
到
final CustomListAdapter dataListAdapter = new CustomListAdapter(context, dataListArray);
getSelectedElements()
时,请像这样写 ((CustomListAdapter)dataListAdapter).getSelectedElements();
请查看当前的答案,并告知我们。