我有一些数据,我试图使用ParseQuery从Parse.com获取,然后我想使用A Fragment在A RecyclerView中显示该数据。 问题是没有任何东西在显示。我该如何解决这个问题?
我的RecyclerView适配器:
public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<HomeRecyclerViewAdapter.ViewHolder> {
//private List<Developer> developerList;
private List<Information> informationList;
public static class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
//public final ImageView mImageView;
public final TextView mTextView;
public ViewHolder(View view) {
super(view);
mView = view;
//mImageView = (ImageView) view.findViewById(R.id.txt_photo);
mTextView = (TextView) view.findViewById(R.id.txt_status);
}
@Override
public String toString() {
return super.toString() + " '" + mTextView.getText();
}
}
public HomeRecyclerViewAdapter(List<Information> informationList) {
this.informationList = informationList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_recyclerview_list_items, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
final Information information = informationList.get(position);
holder.mTextView.setText(information.getStatus());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO MOVE THIS CODE TO A DELEGATE, THIS CAST IS EVIL
Context context = v.getContext();
Intent intent = new Intent(context,DetailActivity.class);
intent.putExtra(Config.EXTRA_NAME, information.getStatus());
//intent.putExtra(Config.EXTRA_PHOTO, information.getFoto());
context.startActivity(intent);
}
});
/*Glide.with(holder.mImageView.getContext())
.load(developer.getFoto())
.fitCenter()
.into(holder.mImageView);*/
}
@Override
public int getItemCount() {
return informationList.size();
}
}
我的FragmentBase:
我的所有其他片段都扩展了这个
public abstract class FragmentBase extends
Fragment {
protected void settingsToolbar(View rootView) {
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
final ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
}
FragmentHome:
这是包含我的RecyclerView和我的ParseQuery
的片段public class FragmentHome extends FragmentBase{
public static FragmentHome getInstance() {
FragmentHome fragmentHome = new FragmentHome();
Bundle extraArguments = new Bundle();
fragmentHome.setArguments(extraArguments);
return fragmentHome;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView rv_home = (RecyclerView) inflater.inflate(R.layout.home_recyclerview, container, false);
setupRecyclerView(rv_home);
return rv_home;
}
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
recyclerView.setAdapter(new HomeRecyclerViewAdapter(info()));
}
private List<Information> info() {
final List<Information> infoData = new ArrayList<>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Timeline");
query.setLimit(10);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(final List<ParseObject> objects, ParseException e) {
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
Information information = new Information();
information.setStatus(objects.get(i).getString("status"));
infoData.add(information);
}//end for loop
} else {
}//End else
}
});
return infoData;
}
}
我的数据模型:
包含Getters和Setter
public class Information {
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
我的RecyclerView项目列表XML文件:
这包含我想在RecyclerView
中填充的视图列表<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:gravity="center_vertical">
<TextView
android:id="@+id/txt_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?attr/textAppearanceListItem"/>
</LinearLayout>
我的RecyclerView XML文件:
这包含我的实际RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerview_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
答案 0 :(得分:0)
注意:上面的“FragmentHome”(在我的问题中)现在是“FragmentHome2”,如我的回答所示。
为可能在将来发现有用的人提交此答案
<强> FragmentmentHome2:强>
public class FragmentHome2 extends FragmentBase {
final List<Information> infoData = new ArrayList<>();
public static FragmentHome2 getInstance() {
// Required empty public constructor
FragmentHome2 fragmentHome = new FragmentHome2();
Bundle extraArguments = new Bundle();
fragmentHome.setArguments(extraArguments);
return fragmentHome;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/*@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView rv_home = (RecyclerView) inflater.inflate(R.layout.home_recyclerview, container, false);
setupRecyclerView(rv_home);
return rv_home;
}*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment_two, container, false);
//Using the "View" to obtain the WebView
final View view = inflater.inflate(R.layout.fragment_home, container, false);
//initializing the Recycler View
RecyclerView rvHome = (RecyclerView) view.findViewById(R.id.recyclerview_home);
//pass the new initialized variable to the method for use
setupRecyclerView(rvHome);
//ParseQuery();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Timeline");
query.setLimit(10);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(final List<ParseObject> objects, ParseException e) {
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
Information information = new Information();
information.setStatus(objects.get(i).getString("status"));
//information.setMainNewsStory(information.mNewsStory = objects.get(i).getString("shortinfo"));
infoData.add(information);
}//end for loop
} else {
// something went wrong
//Toast.makeText(getApplicationContext(), "A problem occurred. Could not display news feed", Toast.LENGTH_LONG).show();
}//End else
//progress.dismiss();
final HomeRecyclerViewAdapter mAdapter = new HomeRecyclerViewAdapter(getContext(), infoData);
//initialize the recycler view
RecyclerView rv = (RecyclerView) view.findViewById(R.id.recyclerview_home);
//use recycler view initialized variable to set adapter to current state
rv.setAdapter(mAdapter);
//notify data changes
mAdapter.notifyDataSetChanged();
}
});
return view;
}
//Set the recycler view adapter
private void setupRecyclerView(final RecyclerView recyclerViewMain) {
final LinearLayoutManager layoutManager = new LinearLayoutManager(recyclerViewMain.getContext());
recyclerViewMain.setLayoutManager(layoutManager);
recyclerViewMain.setAdapter(new HomeRecyclerViewAdapter(getContext(),infoData ));
recyclerViewMain.setHasFixedSize(true);
}
}
在片段调用的布局中包含RecyclerView布局:
<include layout="@layout/home_recyclerview" />