是否可以通过点击片段列表视图上的元素来调用StringIndexOutOfBoundsException
中的方法来设置可见/已删除的复选框?我有两种方法:
CustomAdapter
但是如何从片段中访问它们?或者我是否选择了其他方式?
(我希望在&#34后设置复选框;长按"在其中一个ListView元素上) 片段:
public void setCheckBoxSelectItemVisible(){
checkBoxSelectItem.setVisibility(View.VISIBLE);
return;
}
public void setCheckBoxSelectItemGone(){
checkBoxSelectItem.setVisibility(View.GONE);
return;
}
CustomAdapter:
public class ListViewFragment extends Fragment {
[...]
@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);
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");
Log.d("DATALIST", dataListArray[0]);
final ListAdapter dataListAdapter = new CustomListAdapter(context, dataListArray);
final ListView dataListListView = (ListView) view.findViewById(R.id.listView_datalist);
dataListListView.setAdapter(dataListAdapter);
dataListListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
((MainActivity) getActivity()).fabmain.animate().translationY(300);
}
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
((MainActivity) getActivity()).fabmain.animate().translationY(0);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
listViewOk = true;
setFabsVisible(roomNrOk, locationOk, true);
setFabdeleteVisible(listViewOk);
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();
}
});
dataListListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
(CustomListAdapter).setCheckBoxSelectItemVisible();
return true;
}
});
[...]
return view;
}
public void reloadListViewFragment(){
Fragment fragment;
FragmentTransaction ft = getFragmentManager().beginTransaction();
fragment = new ListViewFragment();
ft.replace(R.id.container, fragment);
ft.commitAllowingStateLoss();
}
[...]
}
答案 0 :(得分:0)
首先使用以下
更改自定义适配器构造函数 ArrayList<Boolean> mIsVisible;
public CustomListAdapter(Context context, String[] dataListFinal,ArrayList<Boolean> isVisible) {
super(context, R.layout.list_item_datalist ,dataListFinal);
mIsVisible = isVisible;
}
然后修改getView()
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater iteminflater = LayoutInflater.from(getContext());
View customView = iteminflater.inflate(R.layout.list_item_datalist, parent, false);
ImageView image = (ImageView) customView.findViewById(R.id.list_icon_product);
TextView textViewlabel = (TextView) customView.findViewById(R.id.list_item_datalist_label_textview);
TextView textViewdetails1 = (TextView) customView.findViewById(R.id.list_item_datalist_textview_details_1);
TextView textViewdetails2 = (TextView) customView.findViewById(R.id.list_item_datalist_textview_details_2);
checkBoxSelectItem = (CheckBox) customView.findViewById(R.id.checkBox_Item);
String singleListItem = getItem(position);
String[] singleListItemArray = singleListItem.split("\t");
String id = singleListItemArray[0];
String product = singleListItemArray[1];
allElementsAdapter = product + label + serial + mac + daaid + bill;
switch (product) {
case "Pc":
image.setImageResource(R.drawable.icon_pc_circle);
break;
case "Laptop":
image.setImageResource(R.drawable.icon_laptop_circle);
break;
}
String details1 = serial +doubleTab+ mac;
String details2 = daaid +doubleTab+ bill;
textViewlabel.setText(label);
textViewdetails1.setText(details1);
textViewdetails2.setText(details2);
if(mIsVisible.get(position)){
checkBoxSelectItem.setVisibility(View.VISIBLE);
}else{
checkBoxSelectItem.setVisibility(View.GONE);
}
return customView;
}
之后修改您的活动CustomAdapter创建部分
final ArrayList<Boolean> isVisible=new ArrayList<Boolean>();
for(int count =0 ;count<dataListArray.length; count++){
isVisible.add(false);
}
final ListAdapter dataListAdapter = new CustomListAdapter(context, dataListArray, isVisible);
最后修改听众部分
dataListListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if(isVisible.get(position))
isVisible.set(position,false);
else
isVisible.set(position,true);
dataListAdapter.notifyDataSetChanged();
return true;
}
});
完成... !!!!!
答案 1 :(得分:0)
它适用于((BaseAdapter)dataListAdapter).notifyDataSetChanged();