我正在试验ViewPager和ORM(androrm)。我让它运行显示ListView中的所有项目,但是当我尝试过滤时它失败了。过滤器工作正常,因为我在另一个活动中测试了它。当我运行PagerActivity并实现过滤器时,我收到一个错误。 logCat如下。我不知道这是否可能。我想解决这个问题。如果您有任何其他问题或需要更多信息,我会更新问题
工作声明
logAdapter1 = new LogAdapter(PagerActivity.this, 0, Log.all());
错误陈述,适用于单独的活动
logAdapter1 = new LogAdapter(PagerActivity.this, 0, Log.logsByName("NAME0"));
工作PagerActivity(从MainActivity打开)
public class PagerActivity extends ActionBarActivity {
private ViewPager mViewPager;
LogAdapter logAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.viewPager);
setContentView(mViewPager);
logAdapter = new LogAdapter(PagerActivity.this, 0, Log.all());
MyPagerAdapter pagerAdapter = new MyPagerAdapter();
mViewPager.setAdapter(pagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i2) {
}
@Override
public void onPageSelected(int position) {
Log crime = logAdapter.getItem(position);
if (crime.getName() != null) {
setTitle(crime.getName());
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
}
private class MyPagerAdapter extends PagerAdapter {
LogAdapter logAdapter1;
TextView textView;
ListView list;
@Override
public Object instantiateItem(ViewGroup container, int position) {
Log log = logAdapter.getItem(position);
list = new ListView(PagerActivity.this);
// logAdapter1 = new LogAdapter(PagerActivity.this, 0, Log.logsByName("NAME0")); // This is what I am trying to filter
logAdapter1 = new LogAdapter(PagerActivity.this, 0, Log.all()); // Working with all list items
list.setAdapter(logAdapter1);
textView = new TextView(PagerActivity.this);
textView.setText(log.getName());
LinearLayout layout = new LinearLayout(PagerActivity.this);
layout.setOrientation(LinearLayout.VERTICAL);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(layoutParams);
list.setLayoutParams(layoutParams);
layout.addView(textView);
layout.addView(list);
container.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
@Override
public int getCount() {
return logAdapter.getCount();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
适配器
public class LogAdapter extends ArrayAdapter<Log> {
Context mContext;
public static List<Log> mLogs;
public LogAdapter(Context context, int textViewResourceId, List<Log> logs) {
super(context, textViewResourceId);
mContext = context;
mLogs = logs;
}
public void setLogs(List<Log> logs) {
mLogs = logs;
}
public List<Log> getLogs() {
return mLogs;
}
public void add(Log log) {
mLogs.add(log);
}
public void remove(Log log) {
LogAdapter.mLogs.remove(log);
}
public int getCount() {
return mLogs.size();
}
public Log getItem(int position) {
return mLogs.get(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
LogRow view = (LogRow) convertView;
if (view == null) {
view = new LogRow(mContext);
}
Log log = getItem(position);
view.setLog(log);
return view;
}
}
日志
public class Log extends Model {
protected CharField mName;
public Log() {
super(true);
mName = new CharField();
}
/* -G&S- */
public void setName(String name) {
mName.set(name);
}
public String getName() {
return mName.get();
}
/* -Saving & Setting id based on position- */
public boolean save() {
int id = Log.objects(context(), Log.class).all().count() + 1;
return this.save(context(), id);
}
/* -Filters the data based on mName- */
public static List<Log> logsByName(String name) {
String query_string = formatNameForQuery(name);
Filter filter = new Filter();
filter.contains("mName", query_string);
return Log.objects().filter(filter).toList();
}
private static String formatNameForQuery(String name) {
name = "" + name;
return name;
}
/* -Return all data items without any filter, orders based on saved position- */
public static List<Log> all() {
return Log.objects().all().toList();
}
public static QuerySet<Log> objects() {
return Log.objects(context(), Log.class);
}
private static Context context() {
return AppActivity.context();
}
}
logcat的
1581-1581/contractor.com.dudeee.eugene.testingandrormwithlist E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: contractor.com.dudeee.eugene.testingandrormwithlist, PID: 1581
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at contractor.com.dudeee.eugene.testingandrormwithlist.LogAdapter.getItem(LogAdapter.java:41) //At LogAdapter "return mLogs.get(position);"
at contractor.com.dudeee.eugene.testingandrormwithlist.PagerActivity$MyPagerAdapter.instantiateItem(PagerActivity.java:54) //At PagerActivity "Log log = logAdapter.getItem(position);"
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:837)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1053)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
at android.view.View.measure(View.java:17430)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:0)
片段共享适配器,在创建时,它导致适配器的单个实例触发不同的过滤器。我用FragmentStatePagerAdapter解决了它,并根据页面解析了适配器的各个实例(每个过滤器)。
<强>活动强>
private ViewPager mViewPager;
private void viewPagerConnections() {
mViewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
@Override
public Fragment getItem(int position) {
LogSecond log = logSecondAdapter.getItem(position);
return LogThirdFragment.newInstance(log.getDB_twoName(), position, log.getDB_twoKey());
}
@Override
public int getCount() {
return logSecondAdapter.getCount();
}
});
mViewPager.setCurrentItem(currentPageInt);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
LogSecond logThird = logSecondAdapter.getItem(position);
toolbar.setTitle(logThird.getDB_twoName());
}
@Override
public void onPageScrollStateChanged(int state) {}
});
}
<强>片段强>
public class LogThirdFragment extends android.support.v4.app.Fragment {
public static final String EXTRA_ID = "eugene_testing";
public static LogThirdFragment newInstance(String name, int page, int dbTwoKey) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_ID, name);
args.putInt("page", page);
args.putInt("dbTwoKey", dbTwoKey);
LogThirdFragment fragment = new LogThirdFragment();
fragment.setArguments(args);
return fragment;
}
View v;
public ListView listLogExpense;
TextView noLogsTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_three_layout, container, false);
listLogExpense = (ListView) v.findViewById(R.id.log_list);
noLogsTextView = (TextView) v.findViewById(R.id.no_logs);
return v;
}
LogThirdAdapter mThirdAdapter;
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mThirdAdapter = new LogThirdAdapter(getActivity(), 0, LogThird.logsByKey(getArguments().getInt("dbTwoKey")));
listLogExpense.setAdapter(mThirdAdapter);
updateList();
}
private void updateList() {
if (mThirdAdapter.getLogs().size() == 0) {
noLogsTextView.setVisibility(View.VISIBLE);
} else {
noLogsTextView.setVisibility(View.GONE);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}