Подскажите пожалуйста, что я сделал не правильно? Хандлер обновляется раз в 10 сек (вижу по логам), но данные в ListView не обновляются
public class FriendMsgActivity extends AppCompatActivity {
ListView sList;
SimpleAdapter sAdapter;
ImageView avatars;
String FriendURL = "http://MySite.KZ/index.php";
String LOG_TAG = "FriendMsg Activity: ";
HashMap map;
ArrayList data;
Handler mHandler;
Context context = (Context) this;
TextView textView;
Button snd_msg;
protected String LV_KEY = Auth.key;
protected String LV_USID = Auth.id;
protected String LV_LOGIN = Auth.login;
protected String LV_lsNAME = Auth.lsname;
final String LV_USID_MSG = "user_msg_id";
final String LV_MSGID = "msg_id";
final String LV_MSG = "msg";
protected int GET_ID = Integer.parseInt(FriendActivity.get_id);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friend_msg);
textView = (TextView) findViewById(R.id.form_input);
snd_msg = (Button) findViewById(R.id.buttonMsgGet);
mHandler = new Handler();
new FriendMsgLoad().execute();
new RefreshActivity().run();
}
// тут пытаюсь обновится
class RefreshActivity extends Thread {
@Override
public void run() {
FriendMsgActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
mHandler.postDelayed(this, 10000);
Log.d(LOG_TAG, "Сработал mHandler!");
}
});
}
}
public void sendMsg(View v) {
textView = (TextView) findViewById(R.id.form_input);
String form_input_msg = textView.getText().toString();
if(!form_input_msg.equals("")) {
GetMsg getMsg = new GetMsg();
getMsg.SendMsg(context, GET_ID, form_input_msg);
textView.setText("");
} else {}
}
class FriendMsgLoad extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
byte[] data = null;
String parammetrs = "FriendMsgLoad=true&get_id=" + GET_ID + "&us_id=" + LV_USID + "&key=" + LV_KEY;
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
String forecastJsonStr = null;
try {
URL url = new URL(FriendURL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("Content-Length", "" + Integer.toString(parammetrs.getBytes().length));
OutputStream os = urlConnection.getOutputStream();
data = parammetrs.getBytes("UTF-8");
os.write(data);
data = null;
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
return null;
}
forecastJsonStr = buffer.toString();
return forecastJsonStr;
} catch (IOException e) {
return null;
} finally{
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);
}
}
}
}
@Override
protected void onPostExecute(String forecastJsonStr) {
super.onPostExecute(forecastJsonStr);
JSONObject dataJsonObj = null;
try {
dataJsonObj = new JSONObject(forecastJsonStr);
JSONArray chat = dataJsonObj.getJSONArray("data");
data = new ArrayList<Map<String, Object>>();
for (int i = 0; i < chat.length(); i++) {
JSONObject msgList = chat.getJSONObject(i);
String error = msgList.getString("error");
if(!error.equals("0")) {
if(error.equals("1")) {
Intent intent = new Intent(getApplicationContext(), FriendActivity.class);
startActivity(intent);
Log.d(LOG_TAG, "Ошибка! Не получилось зарузить сообщения..");
}
if(error.equals("2")) {
Log.d(LOG_TAG, "Ошибка! Не верный ключ авторизации..");
}
}
String msg = msgList.getString("msg");
String msg_id = msgList.getString("msg_id");
final String msg_id_us = msgList.getString("msg_id_user");
map = new HashMap<String, Object>();
map.put(LV_MSGID, msg_id);
map.put(LV_MSG, msg);
map.put(LV_USID_MSG, msg_id_us);
data.add(map);
}
String[] from = { LV_MSGID, LV_MSG, LV_USID_MSG };
int [] to = { R.id.id_msg, R.id.msg, R.id.id_msg_user };
sAdapter = new FriendMsgLoad.ChatAdapter(FriendMsgActivity.this, data, R.layout.activity_friend_msg_adapter, from, to);
sList = (ListView) findViewById(R.id.lvFriendMsg);
sList.setAdapter(sAdapter);
sAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
class ChatAdapter extends SimpleAdapter {
LayoutInflater inflater;
public ChatAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to){
super(context, data, resource, from, to);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
int type = getItemViewType(position);
View v = convertView;
HashMap < String, String > item = (HashMap < String, String > ) sList.getItemAtPosition(position);
String msg = item.get(LV_MSG);
switch (type)
{
case 1:
v = inflater.inflate(R.layout.activity_friend_msg_adapter, null);
TextView myText = (TextView) v.findViewById(R.id.msg);
myText.setText(msg);
break;
case 4:
v = inflater.inflate(R.layout.talker, null);
TextView otherText = (TextView) v.findViewById(R.id.msg);
otherText.setText(msg);
break;
default:
}
return v;
}
@Override
public int getItemViewType(int position)
{
HashMap < String, String > item = (HashMap < String, String > ) sList.getItemAtPosition(position);
int myIDfromList = Integer.parseInt(item.get(LV_USID_MSG));
return myIDfromList;
}
}
}
}
new FriendMsgLoad().execute();
и поместить его в handler :)