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<MessageToAccount> maVec;
	private ListView list;
	private HashMap<String, Object> listItem;

	private int selectItem = -1;

	private MessageAdapter messageAd;
	private List<HashMap<String, Object>> maps = new ArrayList<HashMap<String, Object>>();

	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<MessageToAccount> 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<HashMap<String, Object>> getData(
			LinkedList<MessageToAccount> maVec2)
			throws UnsupportedEncodingException {
		maps.removeAll(maps);

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

			maVec2.get(i);

			listItem = new HashMap<String, Object>();

			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<Map<String, Object>>() {

			@Override
			public int compare(Map<String, Object> lhs, Map<String, Object> 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<Integer> pList = new ArrayList<Integer>();

		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);
			<span style="color:#ff0000;">messagetitle.setTextColor(android.graphics.Color.WHITE);
			messagetime.setTextColor(android.graphics.Color.WHITE);
			convertView.findViewById(R.id.expandable1).setBackgroundResource(
					R.drawable.messageshape);//这是重点,防止服务器发送消息来,导致listview item状态错乱</span>

			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;
	}

}
有问题交流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值