我正在使用RecyclerViewAdapter
。它完美无缺。
当我尝试从服务器填充列表时,我的问题就出现了。
我的DataHandaling
类,我试图让数组从这里刷新ListView ...
void getEvents() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(enviroClass.url() + "/api/events/" + 7)
.build();
client.newCall(request)
.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
JSONObject eventObject = new JSONObject(response.body().string());
JSONArray eventJsonArray = eventObject.getJSONArray("events");
System.out.println(eventJsonArray.length());
for (int i = 0; i<2; i++) {
eventObject = eventJsonArray.getJSONObject(i);
eventObject = eventObject.getJSONObject("event");
eventArray.add(new Object_Event(eventObject.getString("name"), eventObject.getString("address"), eventObject.getString("image"), "100" ,eventObject.getString("start_date"), eventObject.getString("end_date"), eventObject.getInt("id")));
}
System.out.println("This is from the http request in the datahandeling class" + "\n" + eventArray.get(0).getName());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
Adapter Class
我这里有一个方法来刷新视图
(注意:我不知道交换方法是否有效,因为我无法尝试。)
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.app.garethbecker.socializer.EventListFragment.OnListFragmentInteractionListener;
import com.app.garethbecker.socializer.dummy.DummyContent.DummyItem;
import java.util.ArrayList;
import java.util.List;
import static android.R.attr.data;
/**
* {@link RecyclerView.Adapter} that can display a {@link DummyItem} and makes a call to the
* specified {@link OnListFragmentInteractionListener}.
* TODO: Replace the implementation with code for your data type.
*/
public class MyItemRecyclerViewAdapter extends RecyclerView.Adapter<MyItemRecyclerViewAdapter.ViewHolder> {
private final List<Object_Event> mValues;
private final OnListFragmentInteractionListener mListener;
public MyItemRecyclerViewAdapter(List<Object_Event> items, OnListFragmentInteractionListener listener) {
mValues = items;
mListener = listener;
}
public void swap(String hello){
// mValues.clear();
// DataHandaling dh = new DataHandaling();
// System.out.println("This is the name of the event from the adapter" + "\n" + dh.getEventArray().get(0).getName());
// mValues.addAll(eventArray);
// notifyDataSetChanged();
System.out.println(hello);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.fragment_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).getId() + "");
holder.mContentView.setText(mValues.get(position).getName());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != mListener) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mListener.onListFragmentInteraction(position);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public Object_Event mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.id);
mContentView = (TextView) view.findViewById(R.id.content);
}
@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}
Fragment
上课
(注意:这应该是正确的,因为它最初会填充ListView)
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.app.garethbecker.socializer.dummy.DummyContent;
import com.app.garethbecker.socializer.dummy.DummyContent.DummyItem;
import org.w3c.dom.Text;
import java.util.ArrayList;
import static android.R.attr.data;
/**
* A fragment representing a list of Items.
* <p/>
* Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
* interface.
*/
public class EventListFragment extends Fragment {
DataHandaling dh = new DataHandaling();
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public EventListFragment() {
}
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static EventListFragment newInstance(int columnCount) {
EventListFragment fragment = new EventListFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(new MyItemRecyclerViewAdapter(dh.getEventArray(), mListener));
}
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(int item);
}
我尝试(刷新列表)刷新数据,这是我从服务器获取的地方。我失败了。
任何人都可以帮助我吗?感谢。
答案 0 :(得分:0)
您必须通知更改。
success
答案 1 :(得分:0)
好的,所以我发现了我做错了什么......
我更改了MyItemRecyclerViewAdapter的构造函数(我应该重命名),以便检查值以检查它们是否为空。
public MyItemRecyclerViewAdapter(List<Object_Event> items, OnListFragmentInteractionListener listener) {
if (items != null) {
mValues = items;
}
if (listener != null ) {
mListener = listener;
}
}
然后我毫无困难地调用了交换方法
MyItemRecyclerViewAdapter adapter = new MyItemRecyclerViewAdapter(null, null);
adapter.swap(eventArray);