Android 如何在从服务器获取数据时实现ListView

Android 如何在从服务器获取数据时实现ListView,android,android-listview,Android,Android Listview,我已经编写了一个程序,用文本视图(s)从服务器获取数据,并在Android活动中显示,现在我想用列表视图将这些数据显示到列表中,但我不知道我需要如何在代码中实现列表视图 我从每一行中提取两个字段,即:TotalAmount和ItemDetails 注意:我也为ListView编写了XML文件,即-listrow\u订单 请我不需要任何其他教程链接,根据我的代码简要介绍我,这将是最好的最好的理解 OrdersActivity.java: public class OrdersActivity e

我已经编写了一个程序,用
文本视图
(s)从服务器获取数据,并在Android活动中显示,现在我想用
列表视图
将这些数据显示到列表中,但我不知道我需要如何在代码中实现
列表视图

我从每一行中提取两个字段,即:TotalAmountItemDetails

注意:我也为
ListView
编写了XML文件,即-listrow\u订单

请我不需要任何其他教程链接,根据我的代码简要介绍我,这将是最好的最好的理解

OrdersActivity.java:

 public class OrdersActivity extends Activity 
{ 
    public static final String LOG_TAG = "OrdersActivity";

    TextView txtDetail,txtAmount ;
    String MemberID,resultServer,strTotal,strDetails,strOrderID;

    @SuppressLint("NewApi")
    @Override

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

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

            txtAmount = (TextView)findViewById(R.id.txtTotalAmount);
            txtDetail = (TextView)findViewById(R.id.txtItemDetails);

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

            strTotal = "";
            strDetails = "";

            JSONObject c;
            try {
            c = new JSONObject(resultServer);
            strTotal = c.getString("TotalAmount");  
            Log.d("Total Amount::", " "+strTotal);
            strDetails = c.getString("ItemDetails");
            Log.d("Item Details::", " "+strDetails);

            if(!strDetails.equals(""))
            {                   
                txtAmount.setText(strTotal);
                txtDetail.setText(strDetails);              
            }
            else
            {               
                txtDetail.setText("-");
                txtDetail.setText("-");
            }
            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }       

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

<ListView 
    android:layout_width="match_parent" 
    android:id="@+id/list_orders" 
    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>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 >

<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: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:text="Item Details Here" />

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

<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:text="Ordered Items:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#a60704" />

</RelativeLayout>
公共类OrdersActivity扩展活动
{ 
公共静态最终字符串日志\u TAG=“OrdersActivity”;
TextView txtDetail,txtAmount;
字符串MemberID、resultServer、strTotal、strDetails、strOrderID;
@SuppressLint(“新API”)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orders);
//权限限制模式
如果(android.os.Build.VERSION.SDK_INT>9){
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
}           
txtAmount=(TextView)findViewById(R.id.txtTotalAmount);
txtDetail=(TextView)findViewById(R.id.txtItemDetails);
字符串url=”http://172.16.0.4/res/order_fetch.php";
Intent=getIntent();
MemberID=intent.getStringExtra(“MemberID”);
List params=new ArrayList();
添加(新的BasicNameValuePair(“sMemberID”,MemberID));
resultServer=getHttpPost(url,参数);
strTotal=“”;
strDetails=“”;
JSONObject c;
试一试{
c=新的JSONObject(resultServer);
strTotal=c.getString(“TotalAmount”);
Log.d(“总金额::”,“”+strTotal);
strDetails=c.getString(“ItemDetails”);
Log.d(“项目详细信息::”,“”+strDetails);
如果(!strDetails.equals(“”)
{                   
txtAmount.setText(strTotal);
txtdeail.setText(strDetails);
}
其他的
{               
txtDetail.setText(“-”);
txtDetail.setText(“-”);
}
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}       
}       
activity\u orders.xml:

 public class OrdersActivity extends Activity 
{ 
    public static final String LOG_TAG = "OrdersActivity";

    TextView txtDetail,txtAmount ;
    String MemberID,resultServer,strTotal,strDetails,strOrderID;

    @SuppressLint("NewApi")
    @Override

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

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

            txtAmount = (TextView)findViewById(R.id.txtTotalAmount);
            txtDetail = (TextView)findViewById(R.id.txtItemDetails);

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

            strTotal = "";
            strDetails = "";

            JSONObject c;
            try {
            c = new JSONObject(resultServer);
            strTotal = c.getString("TotalAmount");  
            Log.d("Total Amount::", " "+strTotal);
            strDetails = c.getString("ItemDetails");
            Log.d("Item Details::", " "+strDetails);

            if(!strDetails.equals(""))
            {                   
                txtAmount.setText(strTotal);
                txtDetail.setText(strDetails);              
            }
            else
            {               
                txtDetail.setText("-");
                txtDetail.setText("-");
            }
            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }       

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

<ListView 
    android:layout_width="match_parent" 
    android:id="@+id/list_orders" 
    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>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 >

<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: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:text="Item Details Here" />

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

<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:text="Ordered Items:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#a60704" />

</RelativeLayout>

listrow\u orders.xml:

 public class OrdersActivity extends Activity 
{ 
    public static final String LOG_TAG = "OrdersActivity";

    TextView txtDetail,txtAmount ;
    String MemberID,resultServer,strTotal,strDetails,strOrderID;

    @SuppressLint("NewApi")
    @Override

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

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

            txtAmount = (TextView)findViewById(R.id.txtTotalAmount);
            txtDetail = (TextView)findViewById(R.id.txtItemDetails);

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

            strTotal = "";
            strDetails = "";

            JSONObject c;
            try {
            c = new JSONObject(resultServer);
            strTotal = c.getString("TotalAmount");  
            Log.d("Total Amount::", " "+strTotal);
            strDetails = c.getString("ItemDetails");
            Log.d("Item Details::", " "+strDetails);

            if(!strDetails.equals(""))
            {                   
                txtAmount.setText(strTotal);
                txtDetail.setText(strDetails);              
            }
            else
            {               
                txtDetail.setText("-");
                txtDetail.setText("-");
            }
            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }       

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

<ListView 
    android:layout_width="match_parent" 
    android:id="@+id/list_orders" 
    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>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 >

<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: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:text="Item Details Here" />

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

<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:text="Ordered Items:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#a60704" />

</RelativeLayout>

首先,你应该为你的post请求使用一个实例(因为这样的东西不应该在gui线程中运行,它会阻止它)。对于你的结果,你最好创建一个新的类。对于每个结果,你创建这个类的一个实例,并将它们放在一个
数组列表中

第二,退房。
使用
ArrayAdapter
-不需要自定义适配器。

使用带有自定义适配器的自定义列表视图。示例显示图像和文本。您可以显示文本而不是图像Google Android自定义BaseAdapter,这是您的解决方案。@Raghundandandan如果可能,请向我显示在我的程序中需要使用的代码行数以实现此目的nt ListView,大教程不会有帮助me@ChulbulPandey你检查一下,如果有用的话