私のコードでいくつかのトラブルシューティングを行っていますが、新しいデータを1回ではなく投稿すると、リストビューの行が複数回返されることがあります。私は、データベースからリストビュー列の内容を読み取り、getViewメソッドのelse if
声明にそれを投影しています(ビットマップはまだ同じ問題を抱えて、TextViewにしてテストするためにコメントアウト)getView複数の行を返す
getViewメソッド:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row=convertView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (request_my_username.equals(arrRequest_UserContact.get(position))) //YOU
{
if (Request_Message.get(position).equals("MESSAGE-FAILED-TO-POST"))
{
row = inflater.inflate(R.layout.activity_chatprivate_single_right, parent, false);
TextView txtMessage = (TextView) row.findViewById(R.id.textMessage);
txtMessage.setTypeface(null, Typeface.BOLD);
txtMessage.setText(Request_Message.get(position));
}
else if (Request_Message.get(position).contains("/storage/"))
{
/***
row = inflater.inflate(R.layout.activity_chatprivate_single_right_img, parent, false);
Bitmap bmp_ico = BitmapFactory.decodeFile(Request_Message.get(position));
ImageButton ib = (ImageButton) row.findViewById(R.id.ib);
ib.setImageBitmap(bmp_ico);
***/
row = inflater.inflate(R.layout.activity_chatprivate_single_right, parent, false);
TextView txtMessage = (TextView) row.findViewById(R.id.textMessage);
txtMessage.setText(Request_Message.get(position));
}
else
{
row = inflater.inflate(R.layout.activity_chatprivate_single_right, parent, false);
TextView txtMessage = (TextView) row.findViewById(R.id.textMessage);
txtMessage.setText(Request_Message.get(position));
}
}
はMySQLを読みます
private class JsonReadChatPrivate extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
}
return answer;
}
@Override
protected void onPostExecute(String result) {
adapter.notifyDataSetChanged();
try{
ListDrawer_readPrivateChat(); //has ConnectionException (when it cannot reach server)
}catch (Exception e){
}
}
}
public void accessWebService_readChatPrivate() {
JsonReadChatPrivate task = new JsonReadChatPrivate();
task.execute(new String[] { "http://website/php/file_to_read_db.php?pcontactSelected="+contactSelected+"&pIMEI="+IMEI+"&pmysql_room_id="+mysql_room_id});
}
public void ListDrawer_readPrivateChat() {
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("request_chat_private");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String request_message = jsonChildNode.optString("Message");
String request_time = jsonChildNode.optString("Time");
mysql_room_id = jsonChildNode.optString("_id");
request_last_user = jsonChildNode.optString("LastUser");
arrRequest_UserContact.add(request_last_user);
arrRequest_Message.add(request_message);
arrRequest_Time.add(request_time);
adapter.notifyDataSetChanged();
}
} catch (JSONException e) {
}
}
がMySQLの上の任意の新しいデータであるとがあれば、私はリストビュー上に存在するアイテムを再度追加しないように、(最後のMySQL IDから内容だけを返すかどうかを確認するために、5秒ごとに聞きます):
private void DBListern() {
accessWebService_readChatPrivate();
}
private void loop() {
handler.postDelayed(new Runnable() {
public void run() {
DBListern();
handler.postDelayed(this, 5000);
}
}, 5000);
}
XML:上記と
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bg_chat_2"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transcriptMode="normal"
android:stackFromBottom="true"
android:divider="@null"
android:dividerHeight="0dp"
>
</ListView>
</LinearLayout>
...
は、私は、Bなどのコンテンツを投稿するときにA、B、Cは、しかし、それは、このA、AAと同様に、それは正しい出力を返すべきC、前記しましたBBB、CCCC < - 同じデータでビューを繰り返す。やったときに