package com.bdyjy.fragment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONException;
import com.alibaba.fastjson.JSON;
import com.bdyjy.R;
import com.bdyjy.activity.MainActivity;
import com.bdyjy.adapter.NoticeListViewAdapter;
import com.bdyjy.constants.HandlerOrder;
import com.bdyjy.custom.view.XListView;
import com.bdyjy.custom.view.XListView.IXListViewListener;
import com.bdyjy.entity.attArryData;
import com.bdyjy.entity.notice.Notice;
import com.bdyjy.entity.notice.NoticeContent;
import com.bdyjy.entity.notice.NoticeContentById;
import com.bdyjy.entity.notice.NoticeResultBean;
import com.bdyjy.util.OkHttpUtils;
import com.bdyjy.util.SPUtils;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView.OnEditorActionListener;
/**
* ����֪ͨ�б�fragment
*
* @author cuicui create at 2016-04-04 12:10
*
*/
public class NoticeListFragment extends Fragment implements IXListViewListener
{
Handler handler = null;
String toastMsg = null;
private EditText et_search; // ����״̬�µ������
private TextView tv_search_cancel;// �ر���������
private RelativeLayout rl_searchbar_off; // �ر�״̬�µ������
private RelativeLayout rl_searchbar_on;// ����״̬�µ������
private MainActivity ctx;
// �Զ������֪ͨ�б����
private XListView noticeListView;
private NoticeListViewAdapter noticeListViewAdapter;
private List<Map<String, Object>> noticeListItems;
private TextView tv_notice_back;
private int pageSize = 5;// ҳ�����֪ͨ����-����
private int sizeStep = 5;// ÿ�μ��ص���Ŀ
public NoticeListFragment(MainActivity ctx)
{
this.ctx = ctx;
initHandler();
}
private void initHandler()
{
handler = new Handler(ctx.getMainLooper())
{
@Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case HandlerOrder.TOAST:
// TODO
Toast.makeText(ctx, toastMsg, Toast.LENGTH_LONG).show();
break;
case HandlerOrder.UPDATE_LISTVIEW:
try
{
loadData();
} catch (JSONException e)
{
e.printStackTrace();
}
break;
case HandlerOrder.PROCESSBAR_SHOW:
ctx.showRoundProcessDialog();
break;
case HandlerOrder.PROCESSBAR_HIDE:
ctx.hideRoundProcessDialog();
break;
}
}
};
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.noticelist_fragment, null);
tv_notice_back = (TextView) view.findViewById(R.id.tv_notice_back);
tv_notice_back.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
ctx.jumpToFirstPageFregment();
}
});
view.findViewById(R.id.ll_back).setOnClickListener(
new OnClickListener()
{
@Override
public void onClick(View v)
{
ctx.jumpToNoticeListFragment();
}
});
noticeListView = (XListView) view.findViewById(R.id.notice_listview);
noticeListView.setPullLoadEnable(true);
noticeListView.setXListViewListener(this);
noticeListItems = new ArrayList<Map<String, Object>>();
// ��ʼ�������
rl_searchbar_off = (RelativeLayout) view
.findViewById(R.id.searchbar_off);
rl_searchbar_on = (RelativeLayout) view.findViewById(R.id.searchbar_on);
// ����������
rl_searchbar_off.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
rl_searchbar_off.setVisibility(View.INVISIBLE);
rl_searchbar_on.setVisibility(View.VISIBLE);
et_search.setFocusable(true);
et_search.requestFocus();
// �����Զ��������뷨
InputMethodManager imm = (InputMethodManager) et_search
.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run()
{
InputMethodManager inputManager = (InputMethodManager) et_search
.getContext().getSystemService(
Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(et_search, 0);
}
}, 400);
}
});
tv_search_cancel = (TextView) view.findViewById(R.id.tv_search_cancel);
// �ر�������
tv_search_cancel.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
rl_searchbar_off.setVisibility(View.VISIBLE);
rl_searchbar_on.setVisibility(View.INVISIBLE);
et_search.setText("");
}
});
// �������������
et_search = (EditText) view.findViewById(R.id.et_search);
et_search.setOnEditorActionListener(new OnEditorActionListener()
{
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event)
{
// Toast.makeText(ctx, "�������ܣ���δ���.", Toast.LENGTH_SHORT).show();
searchNoticeList();
return false;
}
});
// ѡ��ĵ���¼�
noticeListView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
// ����ѡ��ĵ���¼�
TextView tv = (TextView) view.findViewById(R.id.tv_notice_id);
// �����������֪ͨid������ѯ����Ľ���֪ͨ���ݣ���ʾ����
// ��ѯ��̨
getNoticeContent(tv.getText().toString());
}
});
if (null == MainActivity.noticeList)
getNoticeList();
else{
try
{
loadData();
} catch (JSONException e)
{
e.printStackTrace();
}
}
return view;
}
private void loadData() throws JSONException
{
// ���ԭ������
noticeListItems.clear();
Map<String, Object> map = null;
if (MainActivity.noticeList == null)
{
System.out.println("����֪ͨnoticeListδ��ȡ");
}
// �����Ƕ�Ӧ�ĵ�һ��JSON������ʽ
// for (int i = 0; i < MainActivity.noticeList.length(); i++) {
//
// JSONObject listObj = MainActivity.noticeList.getJSONObject(i);
// map = new HashMap<String, Object>();
// map.put("thumb", R.drawable.news_1); // ͼƬ��Դ,
// // ��ʱ��ʹ��ͬһ��ͼƬ��Ϊ����֪ͨ
// map.put("title", listObj.getString("title")); // ��Ʒ����
// map.put("id", listObj.getString("id"));
// map.put("sourcefrom", listObj.getString("sourcefrom")); // ��Ʒ����
//
// map.put("date", listObj.getString("date"));
// noticeListItems.add(map);
//
// }
// �����Ƕ�Ӧ�ĵڶ���JSON������ʽ
for (int i = 0; i < MainActivity.noticeList.size(); i++)
{
map = new HashMap<String, Object>();
map.put("title", MainActivity.noticeList.get(i).getTitle()); // ��Ʒ����
map.put("id", MainActivity.noticeList.get(i).getId());
map.put("sourcefrom", MainActivity.noticeList.get(i)
.getSourcefrom()); // ��Ʒ����
map.put("date", MainActivity.noticeList.get(i).getDate());
// List<attArryData> attArry =
// MainActivity.noticeList.get(i).getAttArry();
// String http = "";
// String http2 = "";
// String http1 = "";
// try {
// http1 = attArry.get(0).getFilePath().toString().trim();
// http2 = MainActivity.secondMarket.get(i).getAttachmentPrefix();
// } catch (Exception e) {
// e.printStackTrace();
// }
// http = http2 + http1;
//
// map.put("http", http);
noticeListItems.add(map);
}
if (null == noticeListViewAdapter)
{
noticeListViewAdapter = new NoticeListViewAdapter(ctx,
noticeListItems); // ����������
noticeListView.setAdapter(noticeListViewAdapter);
} else
{
noticeListViewAdapter.refresh(noticeListItems);
}
}
// ��������
private void searchNoticeList()
{
new Thread()
{
@Override
public void run()
{
// ����get����
String res = null;
// ��sharePreference��ȡ��֮ǰ�洢�IJ���
String token = (String) SPUtils.get(ctx, "token", "");
String singnature = (String) SPUtils.get(ctx, "singnature", "");
String st = (String) SPUtils.get(ctx, "st", "");
String searchtitle = et_search.getText().toString().trim();
// ����������
try
{
handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW);
res = OkHttpUtils.getInstance().Get(
"http://portal.pkusz.edu.cn/api/get_jiaowu.php?pageNo=1&pageSize=30"
+ "&searchtitle=" + searchtitle);
handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_HIDE);
} catch (Exception e)
{
e.printStackTrace();
toastMsg = "��ȡ����ʧ��";
handler.sendEmptyMessage(HandlerOrder.TOAST);
handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_HIDE);
return;
}
System.out.println("��������֪ͨ�����صĽ���ǣ�" + res.trim());
if (res.trim().equals("error"))
{
toastMsg = "��ȡ����ʧ�ܣ�������;res=" + res.trim();
handler.sendEmptyMessage(HandlerOrder.TOAST);
return;
}
// ���Խ�json��ת����bean����
Notice bean = JSON.parseObject(res, Notice.class);
String code = bean.getCode();
if ("OK".equals(code))
{
// ��������˵���������������ݣ���Ҫ�������ȡ���ŵı���
List<NoticeContent> list = bean.getList();
// ����Щ���Ŷ���洢��sp��
MainActivity.noticeList = list;
// ʹ��handlerȥ֪ͨ���̸߳���listview
handler.sendEmptyMessage(HandlerOrder.UPDATE_LISTVIEW); // ������Ϣ�ɹ�
// ��ʱ����
} else
{
toastMsg = "��ȡ����ʧ�ܣ����Ժ�����...";
handler.sendEmptyMessage(HandlerOrder.TOAST);
return;
}
}
}.start();
}
// �������װ�Ӻ�̨һ����ȡ����֪ͨ�б�ķ���
private void getNoticeList()
{
new Thread()
{
@Override
public void run()
{
// ����get����
String res = null;
try
{
handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_SHOW);
res = OkHttpUtils.getInstance().Get(
"http://portal.pkusz.edu.cn/api/get_jiaowu.php");
handler.sendEmptyMessage(HandlerOrder.PROCESSBAR_HIDE);
} catch (Exception e)
{
e.printStackTrace();
toastMsg = "��ȡ����֪ͨ����ʧ��";
handler.sendEmptyMessage(HandlerOrder.TOAST);
return;
}
System.out.println("�������֪ͨ�����صĽ���ǣ�" + res.trim());
// ��һ�ַ��������Խ�json�����ν�����JSONObject��JSONArray��JSONObject
// try {
// JSONObject obj = new JSONObject(res.trim());
// JSONArray list = obj.getJSONArray("list");
// MainActivity.noticeList = list;
// } catch (JSONException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// �ڶ��ַ�������JSON������bean����
Notice noticeObj = JSON.parseObject(res, Notice.class);
// ����ֵ������JSON��ʽ�����ݣ���Ҫ���������
if (res.trim().length() == 0)
{
toastMsg = "��ȡ����֪ͨ����ʧ��";
handler.sendEmptyMessage(HandlerOrder.TOAST);
return;
}
List<NoticeContent> list = new ArrayList<NoticeContent>();
list = noticeObj.getList();
// ����Щ����֪ͨ����洢��sp��
MainActivity.noticeList = list;
// ʹ��handlerȥ֪ͨ���̸߳���noticeListView
handler.sendEmptyMessage(HandlerOrder.UPDATE_LISTVIEW);
// }
}
}.start();
}
// �������װ�Ӻ�̨һ����ȡ����֪ͨ���ݵķ���
private void getNoticeContent(final String id)
{
new Thread()
{
@Override
public void run()
{
// ����get����
String res = null;
try
{
res = OkHttpUtils.getInstance().Get(
"http://portal.pkusz.edu.cn/api/get_jiaowu.php?id="
+ id);
} catch (Exception e)
{
e.printStackTrace();
}
System.out.println("�������֪ͨ���飺���صĽ���ǣ�" + res.trim());
NoticeResultBean bean = JSON.parseObject(res,
NoticeResultBean.class);
// ����ֵ������JSON��ʽ�����ݣ���Ҫ���������
if (res.trim().length() == 0)
{
toastMsg = "��ȡ����֪ͨ����ʧ��";
handler.sendEmptyMessage(HandlerOrder.TOAST);
return;
}
String code = bean.getCode();
if ("OK".equals(code)) // �����������˽���֪ͨ������,
{
NoticeContentById b = bean.getDetail();// ��ô��Ҫ������ȡ�ý���֪ͨ�ı��⣬���ݣ���ʾ�ڽ�����
MainActivity.notice = b;
}
ctx.jumpToNoticeContentFragment();
}
}.start();
}
@Override
public void onRefresh()
{
// TODO Auto-generated method stub
getNoticeList();
}
@Override
public void onLoadMore()
{
// TODO Auto-generated method stub
pageSize += sizeStep;
getNoticeList();
}
}