我正在使用材料设计标签。我在每个标签上都有不同的片段。在一个片段中,我有编辑文本和按钮,下面是列表视图。 ListView从api填充,并且使用api(POST)创建消息。
用户点击createMessageBtn后,它会向服务器创建消息。但是在点击事件发生后不更新列表视图。直到Fragment Message选项卡不重新加载新显示的消息未显示。当我移动到不同的选项卡然后再次进入消息选项卡时,新创建的消息在listView中。我想在单击createMessageBtn或重新加载片段后更新listview。
这是fragment_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/top" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Create Message"
android:id="@+id/createMessagelbl"
android:textAlignment="center"
android:textStyle="bold"
android:textSize="14sp"
android:layout_marginTop="6dp"
android:gravity="center_horizontal"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="@+id/createMessagetxt"
android:layout_below="@+id/createMessagelbl"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="5dp"
android:textColor="#ff7e7e7e"
android:inputType="text" />
<Button
android:layout_width="80dp"
android:layout_height="30dp"
android:text="Post"
android:id="@+id/createMessagebtn"
android:layout_below="@+id/createMessagetxt"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:background="@drawable/button"
android:layout_marginBottom="10dp"
android:layout_marginTop="1dp"
android:textColor="#ff4a8aff" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_below="@+id/top" android:padding="8dp"
android:layout_marginTop="6dp">
<ListView
android:id="@+id/messagesListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
/>
</RelativeLayout>/
</RelativeLayout>
此处消息Fragment.java
public class Message extends Fragment implements View.OnClickListener {
private ArrayList<Messages> mMessagesList = new ArrayList<>();
private MessagesListAdapter mAdapter;
ListView mListView;
EditText mMessageText;
Button mMessagePostBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getMessages();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_messages, container, false);
mListView = (ListView) view.findViewById(R.id.messagesListView);
mMessageText = (EditText) view.findViewById(R.id.createMessagetxt);
mMessagePostBtn = (Button) view.findViewById(R.id.createMessagebtn);
mMessagePostBtn.setOnClickListener(this);
mAdapter = new MessagesListAdapter(getActivity().getApplicationContext(), mMessagesList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
return view;
}
private void getMessages() {
String URL ="Url";
JsonObjectRequest req = new JsonObjectRequest(URL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// Here I am adding the result to mMessagesList
} catch (JSONException e) {
e.printStackTrace();
}
mAdapter.setData(mMessageList);
mAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
}) {
// Here is my token
String token = "Token " + getToken;
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", token);
return headers;
}
};
AppController.getInstance().addToRequestQueue(req);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.createMessagebtn:
if (mMessageText.getText().toString().length() == 0) {
mMessageText.setError("Please Enter the Message");
} else {
String url = "Url";
postMessage(url, mMessageText.getText().toString());
mMessageText.setText("");
getMessages();
/*FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MessagesFragment messageFragment = new MessagesFragment();
ft.detach(messageFragment);
ft.attach(messageFragment);
ft.commit();*/
}
}
}
public void postMessage(String url, final String message) {
ToastMessage.showToastMessage(getActivity().getApplicationContext(), message);
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
// Here Message is post Successfully
getMessages();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", error.getMessage());
}
}
) {
// Here is my token
String token = "Token " + getToken;
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("message", message);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<String, String>();
header.put("Authorization", token);
return header;
}
};
AppController.getInstance().addToRequestQueue(postRequest);
}
}
这是我的PageAdapter.java
public class PagerAdapter extends FragmentStatePagerAdapter {
final int PAGE_COUNT = 5;
private String tabTitles[] = new String[] { "Some", "Some", "Some","Messages", "Some" };
private Context context;
public PagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
SomeFragment some = new SomeFragment ();
return some ;
case 1:
SomeFragment some = new SomeFragment ();
return market;
case 2:
SomeFragment some = new SomeFragment ();
return some ;
case 3:
MessagesFragment messages = new MessagesFragment();
return messages;
case 4:
SomeFragment some = new SomeFragment ();
return some ;
default:
return null;
}
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
这里是MessageListAdapter.java
public class MessagesListAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Messages> messagesItems;
private LayoutInflater mInflater;
public MessagesListAdapter(Context mContext, ArrayList<Messages> messagesItems) {
this.mContext = mContext;
this.messagesItems = messagesItems;
}
public void setData(ArrayList<Messages> messagesItems) {
this.messagesItems = messagesItems;
notifyDataSetChanged();
}
public int getCount() {
return messagesItems.size();
}
@Override
public Object getItem(int position) {
return messagesItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (mInflater == null)
mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = mInflater.inflate(R.layout.messages_list_row, null);
//here my view from xml files
Messages m = messagesItems.get(position);
//here My populate xml file with data
return convertView;
}
}