2013-05-22 49 views
0

我正在製作一個應用程序,我想在PHPMYADMIN中使用服務器在ListView中向用戶顯示所有先前的訂單詳細信息。ListView沒有出現在活動

問題:沒有得到在活動的ListView,漸漸的代替的ListView

OrdersAdapter.java空白活動:

public class OrdersAdapter extends BaseAdapter { 

    TextView tName,tId,tOid ; 
    String MemberID,resultServer,strMemberID,strName,strOrderID; 

    Activity activity; 
    LayoutInflater inflater; 
    ListView listView; 


    public OrdersAdapter(Activity a) { 
     // TODO Auto-generated constructor stub 
     activity = a; 
     inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    public int getCount() { 
     return 0; 
     // TODO Auto-generated method stub 

    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

     public View getView(final int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      View vi = convertView; 
      if (convertView == null) 
       vi = inflater.inflate(R.layout.listrow_orders, null); // listrow_cart 

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


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

      String url = "http://172.16.0.4/res/order_fetch.php"; 
      Intent intent= activity.getIntent(); 
      MemberID = intent.getStringExtra("MemberID"); 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("sMemberID", MemberID)); 
      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; 
     }  

       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(); 
     } 

} 

OrdersActivity.java:

public class OrdersActivity extends Activity 
{    
    ListView mLstView1; 
    OrdersAdapter mViewOrdersAdpt; 

    @SuppressLint("NewApi") 
    @Override 

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

     mLstView1 = (ListView) findViewById(R.id.listView1); 

      mViewOrdersAdpt = new OrdersAdapter(OrdersActivity.this); 
      mLstView1.setAdapter(mViewOrdersAdpt); 

      mLstView1.setOnItemClickListener(new OnItemClickListener() { 

       public void onItemClick(AdapterView<?> parent, View v, 
         final int position, long id) 
       {  

       } 
      }); 
    } 
} 

activity_orders.xml:

<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> 

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> 
+1

Hi Chulbul ji,plz使用[Asynctask](http://developer.android.com/reference/android/os/AsyncTask.html)從服務器獲取數據而不是在UI上執行長任務線程 –

+0

您正在做getView中的整個操作嘗試在另一個線程中執行 –

+0

您在適配器的getCount方法中使用了0。將其更改爲列表視圖中項目的總數。 – Oam

回答

1

如果您解析jsonActivity,而不是BaseAdapter這是件好事,getView()會被多次這樣JSON將被解析多個時間太

Activity寫JSON獲取代碼中AsyncTask存儲數據ArrayList,如果你有單個ArrayList比使用like follow else需要通過多個ArrayListBaseAdapter constructor

onPostExecute use setAdapter(); 

這裏, mArrayListArrayList<String>

OrdersAdapter ordersAdapter = new OrdersAdapter(MyAct.this,mArrayList); 

內部BaseAdapter

ArrayList<String> mMyList; 

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

getCount方法

public int getCount() { 
    return mMyList.size(); 
} 
+0

啊哥們我同意你的看法,我也覺得我應該分析的活動,而不是BaseAdapter裏面的json返回#items適配器店,你能不能請更新您的答案,並最終告訴我我的代碼應該如何,活動和適配器類,只要按照您的想法放置,然後我將做必要的更改 –

+0

@ChulbulPandey查看更新的答案 –

0

您的適配器說,它沒有任何項目,如getCount將()返回0

+0

是的,我知道這一點,但我想知道我需要使用有 –

+0

只需在其模型 – pskink

0

問題在你OrdersAdapter.java類 您必須以適當方式覆蓋適配器getCount()方法,如

public int getCount() { 
     return SIZE_OF_ARRAYLIST; 

    } 

如果寫入返回0,則不會顯示列表。所以,你需要返回的ArrayList中

0

大小傳遞一個集合,可以說在構造函數的字符串

public OrdersAdapter(Activity a, List stringsToShow) { 
    // TODO Auto-generated constructor stub 
    activity = a; 
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    this.stringsToShow = stringsToShow; 
} 

然後

public int getCount() { 
    return stringsToShow.size(); 

} 

,避免getView()做這麼多的事情..卡邁勒卡特豪潘德吉..

0

不要試圖在getView()方法中加載數據,因爲它被系統多次調用。你可以檢查這兩個鏈接,以更好地瞭解 Yet another getView called multiple times 和第二個鏈接 custom listview adapter getView method being called multiple times, and in no coherent order 所以,現在你需要做什麼來克服這個問題。您可以從網頁加載數據,以便您可以使用asynctask將用於加載數據並將其傳遞到類中。檢查這個知道關於asynctask http://developer.android.com/reference/android/os/AsyncTask.html 從asynctask類你可以傳遞這個數據在一個類。這個類包含數據,然後你可以在主要活動中使用這些數據。最好在asynctask類中解析json數據。 其實你沒有在你的適配器類中傳遞任何數據。這就是爲什麼你的listview沒有顯示任何數據。希望這會起作用。