我已经编写了一个程序来从服务器获取数据并使用TextView
(s)在Android Activity中显示,现在我想要使用这些数据显示到List中ListView
,但我不知道我需要在代码中实现ListView
以在列表中显示数据。
我从每一行中提取两个字段,即: TotalAmount 和 ItemDetails 。
注意:我也为ListView
编写了XML文件,即 listrow_orders
请不要求任何其他教程链接,根据我的代码向我介绍,最好是理解。
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();
}
}
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" >
<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>
listrow_orders.xml:
<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>
答案 0 :(得分:2)
首先,您应该使用AsyncTask作为后期请求(因为这些内容不应该在gui线程中运行,它会阻止它)。为了您的结果,您最好创建一个新类。对于每个结果,您可以创建此类的实例并将它们放在ArrayList
。
然后查看this tutorial。
使用ArrayAdapter
- 不需要自定义适配器。