我是学生,是Android新手,我从课程中获得了这段代码,该代码显示了已插入资源(xml文件)的数组到2个片段。但是,如何显示数据库中的数据?
我想显示由失败状态造成短路的课程数据,如果数据库中的状态课程为1,则该课程显示在“通过”选项卡中,其他所有内容都将进入“失败”选项卡
这是 CourseListAdapter :
public class CourseListAdapter extends RecyclerView.Adapter<CourseListAdapter.ViewHolder> {
private Context cCtx;
private List<Course> cData = new ArrayList<>();
private OnItemClickListener cOnItemClickListener;
public interface OnItemClickListener {
void onItemClick(View view, Course crs, int position);
}
public void setOnItemClickListener(final OnItemClickListener cItemClickListener) {
this.cOnItemClickListener = cItemClickListener;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView status;
public TextView cname;
public TextView courseid;
public View layout_cparent;
public ViewHolder(View v) {
super(v);
status = (TextView) v.findViewById(R.id.status);
cname = (TextView) v.findViewById(R.id.cname);
courseid = (TextView) v.findViewById(R.id.courseid);
layout_cparent = (View) v.findViewById(R.id.layout_cparent);
}
}
public CourseListAdapter(Context cCtx, List<Course> cData) {
this.cCtx= cCtx;
this.cData= cData;
}
@Override
public CourseListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_booking, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final Course c = cData.get(position);
holder.cname.setText(c.cname);
holder.courseid.setText(c.courseid);
holder.status.setText(c.status);
if (c.status.equals("0")) {
holder.status.setBackgroundDrawable(cCtx.getResources().getDrawable(R.drawable.cfail));
} else if (c.status.equals("1")) {
holder.status.setBackgroundDrawable(cCtx.getResources().getDrawable(R.drawable.credo));
} else if (b.status.equals("2")) {
holder.status.setBackgroundDrawable(cCtx.getResources().getDrawable(R.drawable.cpass));
}
holder.layout_cparent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (cOnItemClickListener != null) {
cOnItemClickListener.onItemClick(view, c, position);
}
}
});
}
@Override
public int getItemCount() {
return data.size();
}
@Override
public long getItemId(int position) {
return position;
}
}
这是模型课程:
public class Course implements Serializable {
public String status;
public String cname;
public String courseid;
}
这是获取数据的常量类,我假设这是从虚拟数组字符串中获取数据的类。我试图在此类中添加我的JSON
请求,但出现“静态”错误。
常数:
private static List<Course> getCourse(Context cCtx) {
List<Course> data = new ArrayList<>();
String[] status = cCtx.getResources().getStringArray(R.array.dummy_course_status);
String[] cname = cCtx.getResources().getStringArray(R.array.dummy_course_name);
String[] courseid = cCtx.getResources().getStringArray(R.array.dummy_course_id);
for (int i = 0; i < status.length; i++) {
Course item = new Course();
item.status = status[i];
item.cname= cname[i];
item.courseid = courseid[i];
data.add(item);
}
return data;
}
public static List<Course> getCoursePass(Context ctx) {
return getCourse(ctx).subList(0, 1);
}
public static List<Course> getCourseFailed(Context ctx) {
String[] status = ctx.getResources().getStringArray(R.array.dummy_course_status);
return getCourse(ctx).subList(1, status.length);
}
这是 FragmentCoursePass :
public class FragmentCoursePass extends Fragment {
private View rootView;
private RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_course_pass, container, false);
initComponent();
return rootView;
}
private void initComponent() {
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setHasFixedSize(true);
List<Course> courseList = Constant.getCoursePass(getActivity());
CourseListAdapter cAdapter = new CourseListAdapter(getActivity(), courseList);
recyclerView.setAdapter(cAdapter);
cAdapter.setOnItemClickListener(new CourseListAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, Course crs, int position) {
ActivityCoursePassDetails.navigate(getActivity(), crs);
}
});
}
@Override
public void onResume() {
super.onResume();
}
}
FragmentCourseFailed 几乎相同,只是变量不同
此外,这是 ActivityCourse :
public class ActivityCourse extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
initComponent();
initToolbar();
}
private void initComponent() {
ViewPager view_pager = (ViewPager) findViewById(R.id.view_pager);
FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager());
FragmentCoursePass fragmentCoursePass = new FragmentCoursePass();
FragmentCourseFailed fragmentCourseFailed = new FragmentCourseFailed();
adapter.addFragment(fragmentCoursePass, "PASS");
adapter.addFragment(fragmentCourseFailed, "FAIL");
view_pager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(view_pager);
}
private void initToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Order");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Tools.setCompleteSystemBarLight(this);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
} else {
Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
public class FragmentAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
这是布局,我有几个具有textview来容纳字符串的布局
activity_course.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_bg"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/white"
app:contentInsetStartWithNavigation="0dp"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/Toolbar.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@android:color/white"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorPrimary"
app:tabIndicatorHeight="3dp"
app:tabSelectedTextColor="@color/grey_very_hard"
app:tabTextAppearance="@style/CustomTabTextAppearance"
app:tabTextColor="@color/grey_very_hard" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
这是 fragment_course_pass.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_bg">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="@dimen/spacing_middle"
android:paddingBottom="@dimen/spacing_middle"
android:scrollbars="none"
android:scrollingCache="true" />
</RelativeLayout>
我在哪里以及如何放置“我的提取数据”方法,并将其发送到recyclerview? 我试图从谷歌搜索并尝试几次。因此以为我在尝试搜索其他方法时会问这里。
答案 0 :(得分:1)
可能是封闭线程,但我回答了您的问题。希望我能帮忙
我在哪里以及如何放置“我的提取数据”方法,并将其发送到 recyclerview?
您可能需要制作一个List
-ArrayList
(或任何获取数据的对象)的对象,以便在其他活动中使用多个RecyclerView
。 / p>
就像你到目前为止所做的一样:
List<Course> courseList = Constant.getCoursePass(getActivity());
CourseListAdapter cAdapter = new CourseListAdapter(getActivity(), courseList);
recyclerView.setAdapter(cAdapter);
在第一行中,有一个对象List
,在此之后,您设置了Adapter
,RecyclerView
负责显示该列表中的数据。但是,如果您看到constant class
,则将其定义为:
private static List<Course> getCourse(Context cCtx)
这意味着,您可能需要摆脱static
才能使用多次。因此,第一行是您的答案。您只需要在具有该行的另一个活动上设置另一个RecyclerView
的{{1}}。