2013-11-20 2 views
1

두 열 사용자 지정 ListView 동적으로 행을 추가하는 데 문제가 있습니다. AsyncTask는 웹 서비스에서 데이터를 올바르게 가져 오지만 행은 ListView에 추가되지 않습니다. add_to_list() 프로 시저가 호출되었으며 매개 변수를 표시하지만 ListView가 업데이트되지 않습니다. 코드에 무엇이 잘못되었는지 안내해주세요.다중 열 사용자 정의 ListView 안드로이드에서

1) mail_row.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/cell_borders" 
    android:orientation="horizontal" 
    android:paddingBottom="1dip" 
    android:paddingTop="1dip" 
    android:weightSum="4" 
    tools:ignore="HardcodedText" >  
    <TextView 
     android:id="@+id/msgTime" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content"   
     android:gravity="center" 
     android:layout_weight="1.5" 
     android:text="Time" />  
    <TextView 
     android:id="@+id/msgText" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content"   
     android:gravity="center"  
     android:layout_weight="2.5" 
     android:text="Message" />  
</LinearLayout> 

2) mail_box.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="5dp" 
    tools:ignore="HardcodedText" > 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     tools:ignore="UselessParent" > 
     <Button 
      android:id="@+id/bBack_Mail" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Back" 
      tools:ignore="HardcodedText" /> 
    </LinearLayout> 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="2dp" 
     android:background="@android:color/darker_gray" /> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/cell_borders" 
     android:orientation="horizontal" 
     android:paddingBottom="1dip" 
     android:paddingTop="1dip" 
     android:weightSum="4" 
     tools:ignore="HardcodedText" > 
     <TextView 
      android:id="@+id/tvTime_Mail" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1.5" 
      android:gravity="center" 
      android:text="Time" /> 
     <TextView 
      android:id="@+id/tvText_Mail" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="2.5" 
      android:gravity="center" 
      android:text="Message" /> 
    </LinearLayout> 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="2dp" 
     android:background="@android:color/darker_gray" /> 
    <ListView 
     android:id="@+id/lstMail_Mail" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 
</LinearLayout> 

3) Mail_Box.java

import java.util.ArrayList; 
import java.util.HashMap; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 

public class Mail_Box extends Activity implements View.OnClickListener { 

    private ListView list; 
    private ArrayList<HashMap<String, String>> mylist; 
    private HashMap<String, String> map; 
    private String url = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //Here is a valid URL 
    private String urlSearch = null; 
    private Button bBack; 
    private ProgressDialog progressDialog; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mail_box); 
     this.initializer(); 
     SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, 
       R.layout.mail_row, 
       new String[] { "mail_time", "mail_message" }, new int[] { 
         R.id.tvTime_Mail, R.id.tvText_Mail }); 
     list.setAdapter(mSchedule); 

     this.urlSearch = this.url + "rashid"; 
     MyTask runner = new MyTask(); 
     runner.execute(this.url); 

    } 

    private void initializer() { 
     bBack = (Button) findViewById(R.id.bBack_Mail); 
     bBack.setOnClickListener(this); 
     list = (ListView) findViewById(R.id.lstMail_Mail); 
     mylist = new ArrayList<HashMap<String, String>>(); 

    } 

    @Override 
    public void onClick(View v) { 
     finish(); 
    } 

    private void add_to_list(String tm, String msg) { 
     //This message is shown properly 
     Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); 
     try { 
     map = null; 
     map = new HashMap<String, String>(); 
     map.put("mail_time", tm); 
     map.put("mail_message", msg); 
     mylist.add(map); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show(); 
     } 
    } 

    // --------------AsyncTask Class-------------------------------- 
    private class MyTask extends AsyncTask<String, Void, String> { 
     private String msg = ""; 
     private JSONParser jParser; 
     private JSONObject json; 
     private static final String TAG_DATA = "data"; 
     private static final String TAG_TIME = "MAIL_TIME"; 
     private static final String TAG_TYPE = "TYPE"; 
     private static final String TAG_TEXT = "MSG_TEXT"; 

     private JSONArray data = null; 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
      progressDialog = ProgressDialog.show(Mail_Box.this, 
        "Loading Mails", "Please wait for a while.", true); 
     } 

     @Override 
     protected String doInBackground(String... urls) { 
      try { 
       this.jParser = new JSONParser(); 
       this.json = jParser.getJSONFromUrl(urls[0]); 
       if (this.json != null) 
        msg = "DONE"; 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      Log.d("Response: ", this.msg); 
      return msg; 
     } 

     @Override 
     protected void onPostExecute(String msg) { 
      progressDialog.dismiss(); 
      if (msg.equals("DONE")) { 
       try { 
        data = json.getJSONArray(TAG_DATA); 
        int i = data.length(); 
        if (i == 0) { 
         Toast.makeText(getApplicationContext(), 
           "No record found.", Toast.LENGTH_SHORT).show(); 
        } else { 
         for (int j = 0; j < i; j++) { 
          JSONObject c = data.getJSONObject(j); 
          String tm = c.getString(TAG_TIME); 
          String type = c.getString(TAG_TYPE); 
          String txt = c.getString(TAG_TEXT); 
          add_to_list(tm, txt); 
         } 
        } 

       } catch (Exception e) { 
        Toast.makeText(getApplicationContext(), "Error", 
          Toast.LENGTH_SHORT).show(); 
       } 
      } else { 
       Toast.makeText(getApplicationContext(), 
         "Unable to search record.", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 

} 
+0

맞춤 어댑터가 없습니까? – hasan83

+0

대부분의 경우이 방법이 더 편리하다는 것을 알게되었습니다. –

답변

1

당신은 adapter.notifyDataSetChanged을 (호출해야) 당신은 데이터를 동적으로로드하고 있습니다. 어댑터를 전역 변수로 만들고 add_to_list() method to

private void add_to_list(String tm, String msg) { 
    //This message is shown properly 
     Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); 
     try { 
      map = null; 
      map = new HashMap<String, String>(); 
      map.put("mail_time", tm); 
      map.put("mail_message", msg); 
      mylist.add(map); 
      adapter.notifyDataSetChanged() 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show(); 
     } 
} 
+0

답장을 보내 주셔서 감사합니다. 이제 행은 add_t_list()에 대한 호출마다 추가되지만 열 값은 mail_row.xml에 제공된 텍스트 값 즉 시간 및 메시지를 표시합니다. 리스트 배열에 추가 할 HashMap에 제공된 매개 변수가 작동하지 않습니다. –

+0

R.layout.mail_row의 ID가 SampleAdapter의 생성자에있는 매개 변수와 일치하지 않습니다. –

+0

문제가 해결되었습니다. 오류를 지적 해 주셔서 대단히 감사합니다. –

관련 문제