android listview 实现从数据库读取已读功能,服务器通知增加,刷新listview

   如题的需求:android listview 实现从数据库读取已读功能,服务器通知增加,刷新listview。当服务器端发送了一条资讯,手机客户端需要新增一条信息,并且每次点击了item之后,背景改变了颜色,并且保持,不废话了,直接上代码:

package client.verbank.mtp.allone.frame.message;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

**************
//消息类

public class MessageFragment extends ControlFragment {
	private Handler handler = new Handler();
	private LinkedList maVec;
	private ListView list;
	private HashMap listItem;

	private int selectItem = -1;

	private MessageAdapter messageAd;
	private List> maps = new ArrayList>();

	private ProgressDialog progressDialog;

	public MessageFragment(MainActivity activity) {
		super(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View vi = inflater.inflate(R.layout.activity_message, container, false);

		// 初始化适配器
		initAdapter(vi);
		// 初始化进度条
		initProgress();
		// 首次加载消息数据
		initMessageData();// 初始化消息

		return vi;
	}

	private void initAdapter(View vi) {
		list = (ListView) vi.findViewById(R.id.listmessage);
		// 刷新开仓单定义的变量
		messageAd = new MessageAdapter(this);

		list.setAdapter(messageAd);
		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView arg0, View arg1, int arg2,
					long arg3) {
				TradeAPI.getInstance().queryMessage(
						maps.get(arg2).get("guid").toString());

				messageAd.recordPosition(arg2);
				messageAd.notifyDataSetChanged(); // 通知adapter刷新数据

				Intent intent = new Intent();
				intent.setClass(getActivity(), MessageDescriptionActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("messagetitle",
						maps.get(arg2).get("messagetitle").toString());
				bundle.putString("messagetime",
						maps.get(arg2).get("messagetime").toString());
				bundle.putString("messagecontent",
						maps.get(arg2).get("messagecontent").toString());

				// 用android.intent.extra.INTENT的名字来传递参数
				intent.putExtra("android.intent.extra.messagedescription",
						bundle);
				startActivityForResult(intent, 0);
			}
		});

	}

	private void initProgress() {
		progressDialog = DialogManager.showProgressDialog(this.getActivity(),
				"", getString(R.string.load_data_ing), DialogType.CloseHis);
	}

	// 首次加載頁面
	private void initMessageData() {
		progressDialog.show();
		new Thread() {
			@Override
			public void run() {
				try {
					initData();
				} finally {
					Runnable work = new Runnable() {

						@Override
						public void run() {
							progressDialog.dismiss();
						}
					};
					handler.post(work);
				}
			}
		}.start();
	}

	private void initData() {
		String aeid = ClientAPI.getAeid();
		TradeResult_SimpleReport result = TradeAPI
				.getInstance().report_MessageToAccount(aeid);

		if (!result.isSucceed()) {
			return;
		}
		maVec = result.getReportList();
		handler.post(new Runnable() {
			@Override
			public void run() {
				try {
					maps = getData(maVec);
					sortMessageTimeHByAll();
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				if (messageAd != null) {
					// messageAd.notifyDataSetChanged();
					messageAd.notifyDataSetInvalidated();
				}
			}
		});
	}

	// 服务端发送消息来
	private List> getData(
			LinkedList maVec2)
			throws UnsupportedEncodingException {
		maps.removeAll(maps);

		for (int i = 0; i < maVec2.size(); i++) {

			maVec2.get(i);

			listItem = new HashMap();

			listItem.put("messagetitle", maVec2.get(i).getTitle());
			listItem.put("messagetime", maVec2.get(i).getTime());
			listItem.put("messagecontent", maVec2.get(i).getContext());
			listItem.put("guid", maVec2.get(i).getGuid());
			listItem.put("isread", maVec2.get(i).getIsRead());
			maps.add(listItem);
		}

		return maps;
	}

	/**
	 * 通过系统设定传递的设定字段来进行倒序
	 */
	private void sortMessageTimeHByAll() {
		if (maps.isEmpty()) {
			return;
		}
		Collections.sort(maps, new Comparator>() {

			@Override
			public int compare(Map lhs, Map rhs) {
				Date d7 = ((Date) rhs.get("messagetime"));
				Date d8 = (Date) lhs.get("messagetime");
				if (d7 != null && d7 != null) {
					return d7.compareTo(d8);
				}
				return 0;
			}
		});
	}

	// 适配器
	public class MessageAdapter extends BaseAdapter {

		private List pList = new ArrayList();

		public MessageAdapter(Context myContext) {
		}

		public void recordPosition(int position) {
			pList.add(position);
		}

		public MessageAdapter(MessageFragment messageFragment) {
		}

		@Override
		public int getCount() {
			if (maps == null) {
				return 0;
			}
			return maps.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = null;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = getActivity().getLayoutInflater().inflate(
						R.layout.activity_message_item, null);

				viewHolder.messagetitle = (TextView) convertView
						.findViewById(R.id.messagetitle);
				viewHolder.messagetime = (TextView) convertView
						.findViewById(R.id.messagetime);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			TextView messagetitle = (TextView) convertView
					.findViewById(R.id.messagetitle);
			TextView messagetime = (TextView) convertView
					.findViewById(R.id.messagetime);
			viewHolder.messagetitle.setText(maps.get(position)
					.get("messagetitle").toString());

			Date messagetimeDate = (Date) (maps.get(position)
					.get("messagetime"));
			String messagetimestr = TimeUtil.formatTime(messagetimeDate);
			viewHolder.messagetime.setText(messagetimestr);
			messagetitle.setTextColor(android.graphics.Color.WHITE);
			messagetime.setTextColor(android.graphics.Color.WHITE);
			convertView.findViewById(R.id.expandable1).setBackgroundResource(
					R.drawable.messageshape);//这是重点,防止服务器发送消息来,导致listview item状态错乱

			if (Integer.parseInt(maps.get(position).get("isread").toString()) == 1) {
				messagetitle.setTextColor(android.graphics.Color.BLACK);
				messagetime.setTextColor(android.graphics.Color.BLACK);

				convertView.findViewById(R.id.expandable1).setBackgroundColor(
						android.graphics.Color.WHITE);
				convertView.findViewById(R.id.expandable1)
						.setBackgroundResource(R.drawable.menu_shapeclick);
			} else {
				messagetitle.setTextColor(android.graphics.Color.WHITE);
				messagetime.setTextColor(android.graphics.Color.WHITE);
				convertView.findViewById(R.id.expandable1)
						.setBackgroundResource(R.drawable.messageshape);
			}

			for (int i = 0; i < pList.size(); i++) {
				if (pList.get(i) == position) {
					messagetitle.setTextColor(android.graphics.Color.BLACK);
					messagetime.setTextColor(android.graphics.Color.BLACK);

					convertView.findViewById(R.id.expandable1)
							.setBackgroundColor(android.graphics.Color.WHITE);
					convertView.findViewById(R.id.expandable1)
							.setBackgroundResource(R.drawable.menu_shapeclick);
				}

			}

			return convertView;
		}

		private class ViewHolder {
			TextView messagetitle;
			TextView messagetime;

		}
	}

	@Override
	public String getFragmentTitle() {
		return getString(R.string.messages);
	}

	@Override
	public int getFramentKey() {
		return IBundleCommData.FRAGMENT_ID_Message;
	}

	@Override
	public void onMessageReceive(MessageToAccount arg0) {
		if (!isHidden()) {
			initData();
			// if (messageAd != null) {
			// messageAd.notifyDataSetInvalidated();
			// }
		}
	}

	@Override
	protected boolean onBackPressed() {
		return true;
	}

}
有问题交流

你可能感兴趣的:(Android)