2013-05-22 62 views
1

我是試圖從服務器中檢索數據並希望在ListView中顯示該數據。ListView沒有出現 - 從服務器獲取數據

問題:ListView中沒有出現

OrdersActivity.java:

public class OrdersActivity extends Activity 
    { 
    public static final String LOG_TAG = "OrdersActivity"; 
    ArrayList<HashMap<String, String>> d = new ArrayList<HashMap<String, String>>();; 
    ListView list; 
    OrdersAdapter adapter; 
    @Override 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_orders); 


     final ArrayList<HashMap<String, String>> itemsList = new ArrayList<HashMap<String, String>>(); 
     list = (ListView) findViewById(R.id.listView1); 
     adapter = new OrdersAdapter(this, itemsList); 
     list.setAdapter(adapter); 


     // Permission StrictMode 
     if (android.os.Build.VERSION.SDK_INT > 9) { 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
     } 
    } 
} 

activity_orders.java:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ffffff" > 

    <include 
     android:id="@+id/header" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     layout="@layout/header_orders" /> 

<ListView 
    android:layout_width="match_parent" 
    android:id="@+id/listView1" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/header" 
    android:cacheColorHint="#00000000" 
    android:divider="#b5b5b5" 
    android:dividerHeight="1dp" 
    android:layout_alignParentBottom="true" /> 

</RelativeLayout> 

OrdersActivity.java:

public class OrdersAdapter extends BaseAdapter { 
    TextView tName,tId,tOid ; 
    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater=null; 
    String strName,strMemberID ; 

    public OrdersAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public int getCount() { 
     return data.size(); 
     // return (data == null) ? 0 : data.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.listrow_orders, null); 

     tId = (TextView)vi.findViewById(R.id.txtTotalAmount); 
     tName = (TextView)vi.findViewById(R.id.txtItemDetails); 

     HashMap<String, String> item = new HashMap<String, String>(); 
     item = data.get(position); 

     tId.setText(item.get(strName)); 
     tName.setText(item.get(strMemberID)); 


      String url = "http://172.16.0.4/res/order_fetch.php"; 
      Intent intent= activity.getIntent(); 
      String MemberID = intent.getStringExtra("MemberID"); 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("sMemberID", MemberID)); 
      String resultServer = getHttpPost(url,params); 

      strMemberID = ""; 
      strName = ""; 


      JSONObject c; 
      try { 
      c = new JSONObject(resultServer); 
      strMemberID = c.getString("TotalAmount");    
      strName = c.getString("ItemDetails"); 

      if(!strMemberID.equals("")) 
      {     
       tName.setText(strName); 
       tId.setText(strMemberID);    
      } 
      else 
      {    
       tName.setText("-"); 
       tId.setText("-"); 
      } 
      } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      } 
      return vi; 

}  

     public String getHttpPost(String url,List<NameValuePair> params) { 
      StringBuilder str = new StringBuilder(); 
      HttpClient client = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

     try { 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 
      HttpResponse response = client.execute(httpPost); 
      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 
      if (statusCode == 200) { // Status OK 
       HttpEntity entity = response.getEntity(); 
       InputStream content = entity.getContent(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        str.append(line); 
       } 
      } else { 
       Log.e("Log", "Failed to download result.."); 
      } 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return str.toString(); 
    } 
} 

listrow_orders.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="5dp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="5dp" 
    android:background="@drawable/btn_background" 
    android:orientation="horizontal" 
    android:padding="5dip" > 

<TextView 
    android:id="@+id/txtTotalAmount" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:textColor="#a60704" 
    android:text="TextView" /> 

<TextView 
    android:id="@+id/txtItemDetails" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView3" 
    android:layout_below="@+id/textView3" 
    android:textColor="#a60704" 
    android:text="Item Details Here" /> 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/txtTotalAmount" 
    android:layout_below="@+id/txtTotalAmount" 
    android:layout_marginTop="17dp" 
    android:text="Ordered Items:" 
    android:textColor="#a60704" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="Total Amount" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textColor="#a60704" /> 

</RelativeLayout> 
+0

粘貼布局文件。 – Sandeep

+1

你在哪裏得到你的數據在數組中? – Richa

+0

嘗試使用View Holder .... –

回答

0

您需要存儲的觀點本身的數據,當涉及到的ListView的定製。努力實現這一點:

public class PlacesListAdapter extends BaseAdapter 
{ 
    // private Context mContext; 
    private LayoutInflater mInflater; 
    private ArrayList<String> AL_id_text = new ArrayList<String>(); 
    private ArrayList<String> AL_text = new ArrayList<String>(); 

    public PlacesListAdapter(Context c, ArrayList<String> AL_name_time, ArrayList<String> AL_name_time1) 
    { 
     mInflater = LayoutInflater.from(c); 
     // mContext = c; 
     this.AL_id_text = AL_name_time; 
     this.AL_text = AL_name_time1; 
    } 

    public int getCount() 
    { 
     return AL_id_text.size(); 
    } 

    public Object getItem(int position) 
    { 
     return AL_id_text.get(position); 
    } 

    public long getItemId(int position) 
    { 
     return position; 
    } 

    static class ViewHolder 
    { 
     TextView txt_maintext; 
     TextView txt_mtext; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) 
    { 
     // TODO Auto-generated method stub 
     final ViewHolder holder; 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.place_row, null); 
      holder = new ViewHolder(); 

      holder.txt_maintext = (TextView) convertView.findViewById(R.id.txt_maintext); 
      holder.txt_mtext = (TextView) convertView.findViewById(R.id.txt_mtext); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.txt_maintext.setText(AL_id_text.get(position)); 
     holder.txt_mtext.setText(AL_text.get(position)); 

     return convertView; 
    } 
} 
0

的getView()方法是通過Android的ListView控件時,它呈現您的屏幕,一塊一塊的視圖元素調用。您不應該在getView()方法中獲取數據,這往往會阻止您的UI線程。

要做到這一點,最好的辦法是先取數據預先,將其存儲在您喜歡的對象中並使用它們來創建ListAdapter實例。這樣,當Android爲您繪製ListView時,您無需等待圖像抓取完成。

因此,嘗試使用

getHttpPost() 

的getView方法外獲取數據,優選地是異步的。

使用此方法提取數據後,適當填充您的itemsList。使用此itemList數據創建您的ListAdapter實例。將此適配器設置爲您的列表視圖。