这是我的代码:
Error using view (line 67)
Invalid input arguments
Error in Main (line 97)
view(mdl);
这会使用标签创建import ...;
public class CharSortFragment extends Fragment {
private final String TAG = "CharSortFragment";
private ViewPager viewPager;
private TabsPagerAdapter tabsAdapter;
private FragmentActivity activity;
public CharSortFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "OnCreate view...");
final View view = inflater.inflate(R.layout.char_sort_fragment, container, false);
activity = getActivity();
Log.d(TAG, "Init add button...");
FloatingActionButton addButton = (FloatingActionButton) view.findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Add button was clicked...");
startActivityForResult(new Intent(getActivity(), EditActivity.class), 1);
}
});
tabsAdapter = new TabsPagerAdapter(activity.getSupportFragmentManager());
viewPager = (ViewPager) view.findViewById(R.id.pager);
viewPager.setAdapter(tabsAdapter);
Log.d(TAG, "Connecting PagerSlidingTabStrip...");
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.tabs);
slidingTabLayout.setViewPager(viewPager);
Log.d(TAG, "OnCreate finished!");
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "On activity result: " + resultCode);
if(resultCode == 1) {
Log.d(TAG, "Notify data changes...");
tabsAdapter.updateCharSet();
viewPager.getAdapter().notifyDataSetChanged();
tabsAdapter.recyclerViewAdapter.updateDataSet();
tabsAdapter.recyclerView.getAdapter().notifyDataSetChanged();
Log.d(TAG, "Views was notified!");
}
//TODO: Fix it
}
public class TabsPagerAdapter extends PagerAdapter {
private final String TAG = "TabsPagerAdapter";
private final HashSet<TongueTwister> data = TongueTwistersStorageManager.getInstance().getData();
private final ArrayList<Character> characters = new ArrayList<>();
protected RecyclerViewAdapter recyclerViewAdapter;
protected LinearLayoutManager layoutManager;
protected RecyclerView recyclerView;
public TabsPagerAdapter(FragmentManager fm) {
updateCharSet();
}
protected void updateCharSet() {
this.characters.clear();
this.characters.addAll(generateCharList());
}
private ArrayList<Character> generateCharList() {
Log.i(TAG, "Generating char list...");
Log.i(TAG, "Total tongue twisters: " + data.size());
HashSet<Character> hashCharacters = new HashSet<>();
for (TongueTwister tongueTwister : data) {
for(char character : tongueTwister.getCharacters()) {
if(!hashCharacters.contains(character)) {
Log.i(TAG, "Char: " + character);
hashCharacters.add(character);
}
}
}
Log.i(TAG, "Sorting...");
ArrayList<Character> characters = new ArrayList<>(hashCharacters);
Collections.sort(characters);
Log.i(TAG, "Char list was generated!");
return characters;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Log.i(TAG, "Instantiating item with position: " + position);
RelativeLayout recyclerViewWrapper = (RelativeLayout) (activity.getLayoutInflater()
.inflate(R.layout.card_layout, container, false)
);
Log.d(TAG, "Init RecyclerView...");
recyclerView = (RecyclerView) recyclerViewWrapper.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
char charAtCurrentPos = characters.get(position);
layoutManager = new LinearLayoutManager(activity);
recyclerView.setLayoutManager(layoutManager);
recyclerViewAdapter = new RecyclerViewAdapter(charAtCurrentPos);
recyclerView.setAdapter(recyclerViewAdapter);
Log.i(TAG, "Instantiating item with position: " + position + " was finished!");
container.addView(recyclerViewWrapper);
return recyclerViewWrapper;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
Log.i(TAG, "Destroying item with position: " + position);
}
@Override
public int getCount() {
return characters.size();
} // characters.size()
@Override
public boolean isViewFromObject(View view, Object o) {
return o == view;
}
@Override
public CharSequence getPageTitle(int position) {
return String.valueOf(characters.get(position));
}
}
}
并对其进行排序。
然后我放了一些ViewPager
:
CardViews
This就是这样。
在 public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private final String TAG = "RecyclerViewAdapter";
private ArrayList<TongueTwister> viewData = new ArrayList<>();
private char targetChar;
public static class ViewHolder extends RecyclerView.ViewHolder {
public CardView view;
public TextView title;
public TextView chars;
public ViewHolder(CardView view) {
super(view);
this.view = view;
title = (TextView) view.findViewById(R.id.tt_title);
chars = (TextView) view.findViewById(R.id.tt_chars);
}
}
public RecyclerViewAdapter(char targetChar) {
this.targetChar = targetChar;
updateDataSet();
}
protected void updateDataSet() {
Log.d(TAG, "Updating data set...");
HashSet<TongueTwister> rawData = TongueTwistersStorageManager.getInstance().getData();
viewData.clear();
viewData.addAll(sortDataByHardness(getDataWithChar(rawData, this.targetChar), false));
Log.d(TAG, "Data set was updated, size: " + viewData.size());
}
public ArrayList<TongueTwister> getDataWithChar(HashSet<TongueTwister> data, char targetChar) {
ArrayList<TongueTwister> viewData = new ArrayList<>();
for(TongueTwister tt : data) {
if(tt.getCharacters().contains(targetChar)) viewData.add(tt);
}
return viewData;
}
private ArrayList<TongueTwister> sortDataByHardness(ArrayList<TongueTwister> viewData, boolean up) { //TODO: Делал без интернета. Мб есть более адекватный способ? Сань?
TongueTwister[] dataArray = viewData.toArray(new TongueTwister[viewData.size()]);
for(int parser1 = 0; parser1 < dataArray.length; parser1++) {
TongueTwister firstParserEl = dataArray[parser1];
for(int parser2 = parser1; parser2 < dataArray.length; parser2++) {
TongueTwister secondParserEl = dataArray[parser2];
int firstHardness = firstParserEl.getHardness();
int secondHardness = secondParserEl.getHardness();
if((firstHardness > secondHardness && !up) || (firstHardness < secondHardness && up)) {
dataArray[parser1] = secondParserEl;
dataArray[parser2] = firstParserEl;
}
}
}
return new ArrayList<>(Arrays.asList(dataArray));
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Log.d(TAG, "Creating view holder...");
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view, parent, false);
ViewHolder vh = new ViewHolder((CardView)v);
Log.d(TAG, "View holder was created!");
return vh;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Log.d(TAG, "Binding view holder...");
TongueTwister tt = viewData.get(position);
holder.title.setText(tt.getTitle());
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "OnTabClick > CardView > pos=" + position);
}
});
ArrayList<Character> charArray = new ArrayList<>(tt.getCharacters());
StringBuilder charSetStringBuilder = new StringBuilder();
Collections.sort(charArray);
for(char character : charArray) {
charSetStringBuilder.append(character).append(", ");
}
int setLength = charSetStringBuilder.length();
charSetStringBuilder.delete(setLength - 2, setLength); // Удаляем запятую в конце
String charSetString = charSetStringBuilder.toString();
holder.chars.setText(charSetString);
Log.d(TAG, "Card Name:" + tt.getTitle() + "; Char set string: " + charSetString);
Log.d(TAG, "View holder was bind...");
}
@Override
public int getItemCount() {
Log.d(TAG, "getItemCount: " + viewData.size());
return viewData.size();
}
}
中,我使用以下代码向数据中添加新项:
EditorActivity
。
它会返回结果代码,然后重新计算数据,以便使用以下代码将新项目添加到数据中: TongueTwistersStorageManager.getInstance().getData().add(...)
和 RecyclerViewAdapter
TabsPagerAdapter
但似乎没有任何效果。
方法 tabsAdapter.updateCharSet();
viewPager.getAdapter().notifyDataSetChanged();
tabsAdapter.recyclerViewAdapter.updateDataSet();
tabsAdapter.recyclerView.getAdapter().notifyDataSetChanged();
和其他人被调用,当然数据已更新但onBindViewHolder
无法刷新。
我在这里做错了什么?