Skip to content

Instantly share code, notes, and snippets.

@andrikeev
Last active June 16, 2017 16:13
Show Gist options
  • Save andrikeev/d094f0f4d369a93f62c6c574e3077f05 to your computer and use it in GitHub Desktop.
Save andrikeev/d094f0f4d369a93f62c6c574e3077f05 to your computer and use it in GitHub Desktop.
class dowlandmes extends AsyncTask<my, Void, ArrayList> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* Получаем все продукт из url
*/
protected ArrayList doInBackground(my... args) {
my myid = args[0];
// Будет хранить параметры
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("room1", userid2));
params.add(new BasicNameValuePair("room2", myid.mas[30]));
// получаем JSON строк с URL
JSONObject json = jParser.makeHttpRequest(dowlandmess, "GET", params);
ArrayList messangelist = new ArrayList<Message>();
Log.d("All Products: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS2);
if (success == 1) {
// Эта переменная локальная, не нужно её объявление выносить отсюда.
JSONArray products = json.getJSONArray(TAG_PRODUCT4);
// перебор всех продуктов
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Эти переменные локальные, не нужно их объявление выносить отсюда.
String autor = c.getString(TAG_AUTOR);
String prosmotrenno = c.getString(TAG_PROSMOTRENNO);
String texttt = c.getString(TAG_TEXT);
messangelist.add(new Message(autor, texttt, prosmotrenno));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return messangelist;
}
protected void onPostExecute(ArrayList messangelist) {
// Это нужно вынести в onCreate(), сделать у Activity/Fragment поле listdiolog и инициализировать его в onCreate()
ListView listdiolog = (ListView) findViewById(R.id.listdiolog);
ListAdapter adapter6 = new MyArrayAdapter(null, R.layout.simple_list_item_1, messangelist);
listdiolog.setAdapter(adapter6);
}
}
public class Message {
private String author;
private String text;
private String prosmotrenno;
public Message(String author, String text, String prosmotrenno) {
this.author = author;
this.text = text;
this.prosmotrenno = prosmotrenno;
}
public String getAuthor() {
return author;
}
public String getText() {
return text;
}
public String getProsmotrenno() {
return prosmotrenno;
}
}
public class MyArrayAdapter extends ArrayAdapter<Message> {
public MyArrayAdapter(Context context, int resource, List<Message> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.simple_list_item_1, null, true);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Message item = getItem(position);
holder.textView.setText(item.getText());
if (item.getAuthor().equals("Вася")) {
holder.textView.setBackgroundColor(Color.RED);
} else {
holder.textView.setBackgroundColor(Color.TRANSPARENT);
}
return convertView;
}
private class ViewHolder {
TextView textView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment