Android - Eliminar mensajes en un listview con contentprovider

   
Vista:

Eliminar mensajes en un listview con contentprovider

Publicado por jorge ernesto (1 intervención) el 05/11/2015 19:46:16
Hola buenas tardes, estoy programando una aplicacion de mensajeria instantanea, cliente xmpp, con mensajes que se eliminan a determinado tiempo, el metodo para eliminar se llama deletime(), cuando se ejecuta se crea un contador, despues de que se transcurre el tiempo se elimina el mensaje, pero lo tengo ejecutando ese metodo cada ves que se actualiza la lista de los mensajes y el metodo se aplica a cada mensaje, ¿como puedo hacer que ese metodo solo se aplique al nuevo mensaje?. Aquí mi codigo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// objeto de mensajes
class ChatWindowAdapter extends SimpleCursorAdapter {
	String mScreenName, mJID;
 
	ChatWindowAdapter(Cursor cursor, String[] from, int[] to,
			String JID, String screenName) {
		super(ChatWindow.this, android.R.layout.simple_list_item_1, cursor,
				from, to);
		mScreenName = screenName;
		mJID = JID;
	}
 
 
 
 
 
	@SuppressLint("InflateParams")
	public View getView(int position, View convertView, ViewGroup parent) {
 
 
		View row = convertView;
		ChatItemWrapper wrapper = null;
		Cursor cursor = this.getCursor();
		cursor.moveToPosition(position);
 
 
 
		long dateMilliseconds = cursor.getLong(cursor
				.getColumnIndex(ChatProvider.ChatConstants.DATE));
 
		int _id = cursor.getInt(cursor
				.getColumnIndex(ChatProvider.ChatConstants._ID));
		String date = getDateString(dateMilliseconds);
		String message = cursor.getString(cursor
				.getColumnIndex(ChatProvider.ChatConstants.MESSAGE));
		boolean from_me = (cursor.getInt(cursor
				.getColumnIndex(ChatProvider.ChatConstants.DIRECTION)) ==
				ChatConstants.OUTGOING);
		String jid = cursor.getString(cursor
				.getColumnIndex(ChatProvider.ChatConstants.JID));
 
		int delivery_status = cursor.getInt(cursor
				.getColumnIndex(ChatProvider.ChatConstants.DELIVERY_STATUS));
 
 
		if (row == null) {
			LayoutInflater inflater = getLayoutInflater();
			row = inflater.inflate(R.layout.chatrow, null);
			wrapper = new ChatItemWrapper(row, ChatWindow.this);
			row.setTag(wrapper);
		} else {
			wrapper = (ChatItemWrapper) row.getTag();
		}
 
		if (!from_me && delivery_status == ChatConstants.DS_NEW) {
 
			if (!markAsReadDelayed(_id, DELAY_NEWMSG)){
				delivery_status = ChatConstants.DS_SENT_OR_READ;
			}
		}
 
 
 
		String from = jid;
		if (jid.equals(mJID))
			from = mScreenName;
		wrapper.populateFrom(date, from_me, from, message, delivery_status);
 
		return row;
 
	}
 
}
 
@SuppressLint("SimpleDateFormat")
private String getDateString(long milliSeconds) {
	SimpleDateFormat dateFormater = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
	Date date = new Date(milliSeconds);
	return dateFormater.format(date);
}
 
public class ChatItemWrapper {
	private TextView mDateView = null;
	private TextView mFromView = null;
	private TextView mMessageView = null;
	private ImageView mIconView = null;
 
	private final View mRowView;
	private ChatWindow chatWindow;
 
	ChatItemWrapper(View row, ChatWindow chatWindow) {
		this.mRowView = row;
		this.chatWindow = chatWindow;
	}
 
	void populateFrom(String date, boolean from_me, String from, String message,
			int delivery_status) {
	//	int cin = 5000;
		int omi = 60000;
		int tre = 180000;
		int cim = 300000;
 
 
		String messages = message;
		String[] arraymess = messages.split(",");
 
		message = arraymess[0];
		int tim = Integer.parseInt(arraymess[1]);
 
 
 
 
//			Log.i(TAG, "populateFrom(" + from_me + ", " + from + ", " + message + ")");
		getDateView().setText(date);
		TypedValue tv = new TypedValue();
		if (from_me) {
			getTheme().resolveAttribute(R.attr.ChatMsgHeaderMeColor, tv, true);
			getDateView().setTextColor(tv.data);
			getFromView().setText(getString(R.string.chat_from_me));
			getFromView().setTextColor(tv.data);
			from = YaximApplication.getConfig(ChatWindow.this).userName;
		} else {
			getTheme().resolveAttribute(R.attr.ChatMsgHeaderYouColor, tv, true);
			getDateView().setTextColor(tv.data);
			getFromView().setText(from + ":");
			getFromView().setTextColor(tv.data);
		}
		switch (delivery_status) {
		case ChatConstants.DS_NEW:
			ColorDrawable layers[] = new ColorDrawable[2];
			getTheme().resolveAttribute(R.attr.ChatNewMessageColor, tv, true);
			layers[0] = new ColorDrawable(tv.data);
			if (from_me) {
				// message stored for later transmission
				getTheme().resolveAttribute(R.attr.ChatStoredMessageColor, tv, true);
				layers[1] = new ColorDrawable(tv.data);
			} else {
				layers[1] = new ColorDrawable(0x00000000);
			}
			TransitionDrawable backgroundColorAnimation = new
				TransitionDrawable(layers);
			int l = mRowView.getPaddingLeft();
			int t = mRowView.getPaddingTop();
			int r = mRowView.getPaddingRight();
			int b = mRowView.getPaddingBottom();
			mRowView.setBackgroundDrawable(backgroundColorAnimation);
			mRowView.setPadding(l, t, r, b);
			backgroundColorAnimation.setCrossFadeEnabled(true);
			backgroundColorAnimation.startTransition(DELAY_NEWMSG);
			getIconView().setImageResource(R.drawable.ic_chat_msg_status_queued);
			break;
		case ChatConstants.DS_SENT_OR_READ:
			getIconView().setImageResource(R.drawable.ic_chat_msg_status_unread);
			mRowView.setBackgroundColor(0x00000000); // default is transparent
 
 
			break;
		case ChatConstants.DS_READ:
			getIconView().setImageResource(R.drawable.ic_chat_msg_status_unread);
			mRowView.setBackgroundColor(0x00000000); // default is transparent
 
			if(tim == 0){metodelred(messages);}
			if(tim == 1){metodelred(messages); deletime(messages, omi);}
			if(tim == 2){metodelred(messages); deletime(messages, tre);}
			if(tim == 3){metodelred(messages); deletime(messages, cim);}
 
			break;
		case ChatConstants.DS_ACKED:
			getIconView().setImageResource(R.drawable.ic_chat_msg_status_ok);
			mRowView.setBackgroundColor(0x00000000); // default is transparent	
 
			if(tim == 0){metodelred(messages);}
			if(tim == 1){metodelred(messages); deletime(messages, omi);}
			if(tim == 2){metodelred(messages); deletime(messages, tre);}
			if(tim == 3){metodelred(messages); deletime(messages, cim);}
 
			break;
		case ChatConstants.DS_FAILED:
			getIconView().setImageResource(R.drawable.ic_chat_msg_status_failed);
			mRowView.setBackgroundColor(0x30ff0000); // default is transparent
			break;
 
		}
 
 
		boolean slash_me = message.startsWith("/me ");
		if (slash_me)
			message = String.format("\u25CF %s %s", from, message.substring(4));
		getMessageView().setText(message);
		getMessageView().setTypeface(null, slash_me ? android.graphics.Typeface.ITALIC : 0);
		getMessageView().setTextSize(TypedValue.COMPLEX_UNIT_SP, chatWindow.mChatFontSize);
		getDateView().setTextSize(TypedValue.COMPLEX_UNIT_SP, chatWindow.mChatFontSize*2/3);
		getFromView().setTextSize(TypedValue.COMPLEX_UNIT_SP, chatWindow.mChatFontSize*2/3);
 
	}
 
	TextView getDateView() {
		if (mDateView == null) {
			mDateView = (TextView) mRowView.findViewById(R.id.chat_date);
		}
		return mDateView;
	}
 
	TextView getFromView() {
		if (mFromView == null) {
			mFromView = (TextView) mRowView.findViewById(R.id.chat_from);
		}
		return mFromView;
	}
 
	TextView getMessageView() {
		if (mMessageView == null) {
			mMessageView = (TextView) mRowView
					.findViewById(R.id.chat_message);
 
		}
		return mMessageView;
	}
 
	ImageView getIconView() {
		if (mIconView == null) {
			mIconView = (ImageView) mRowView
					.findViewById(R.id.iconView);
		}
		return mIconView;
	}
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder