【发布时间】:2013-05-23 11:50:41
【问题描述】:
我已经编写了一个程序来从服务器获取数据并使用TextView(s) 在Android Activity 中显示,现在我想使用@987654324 将这些数据显示到列表中@,但我不知道如何在我的代码中实现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>
【问题讨论】:
-
将自定义列表视图与自定义适配器一起使用。示例显示图像和文本。而不是图像,您可以显示文本androidhive.info/2012/02/…
-
Google Android 自定义 BaseAdapter,这就是您的解决方案。
-
@Raghunandan 如果可能的话,请告诉我在我的程序中需要使用几行代码来实现 ListView,大教程对我没有帮助
-
@ChulbulPandey 你检查了这个stackoverflow.com/questions/10816243/…。如果有帮助