我创建了一个listView,它使用JSON Parsing显示来自mySql数据库的记录的部分属性。我在listView中显示了4个属性,我想要的是当我单击一个listView项时,它会在我创建的另一个活动中显示该记录的所有属性;其中只有textViews来显示信息。
这是我的代码:
public void updateJSONdata() {
mCommentList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
try {
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
String orderid = c.getString(TAG_ORDER_ID);
String customerid = c.getString(TAG_CUSTOMER_ID);
String type = c.getString(TAG_TYPE);
String pricelist = c.getString(TAG_PRICELIST);
String taxrate = c.getString(TAG_TAXRATE);
String discount = c.getString(TAG_DISCOUNT);
String orderdate = c.getString(TAG_ORDER_DATE);
String requireddate = c.getString(TAG_REQUIRED_DATE);
String shipper = c.getString(TAG_SHIPPER);
String freight = c.getString(TAG_FREIGHT);
String name = c.getString(TAG_NAME);
String address = c.getString(TAG_ADDRESS);
String city = c.getString(TAG_CITY);
String region = c.getString(TAG_REGION);
String postalcode = c.getString(TAG_POSTALCODE);
String country = c.getString(TAG_COUNTRY);
String notes = c.getString(TAG_NOTES);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ORDER_ID, orderid);
map.put(TAG_CUSTOMER_ID, customerid);
map.put(TAG_TYPE, type);
map.put(TAG_PRICELIST, pricelist);
map.put(TAG_TAXRATE, taxrate);
map.put(TAG_DISCOUNT, discount);
map.put(TAG_ORDER_DATE, orderdate);
map.put(TAG_REQUIRED_DATE, requireddate);
map.put(TAG_SHIPPER, shipper);
map.put(TAG_FREIGHT, freight);
map.put(TAG_NAME, name);
map.put(TAG_ADDRESS, address);
map.put(TAG_CITY, city);
map.put(TAG_REGION, region);
map.put(TAG_POSTALCODE, postalcode);
map.put(TAG_COUNTRY, country);
map.put(TAG_NOTES, notes);
mCommentList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
这是我的updateList方法:
private void updateList() {
final ListAdapter adapter = new SimpleAdapter(this, mCommentList,
R.layout.single_comment, new String[] { TAG_ORDER_ID,
TAG_ORDER_DATE, TAG_FREIGHT, TAG_NAME }, new int[] {
R.id.orderid, R.id.orderdate, R.id.freight, R.id.name });
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent extras = new Intent(ReadComments.this,
ViewOrderDetails.class);
adapter.getItem(position);
TextView OrderId = (TextView) findViewById(R.id.orderid);
TextView OrderDate = (TextView) findViewById(R.id.orderdate);
String str = OrderId.getText().toString();
String str1 = OrderDate.getText().toString();
extras.putExtra("orderid", str);
extras.putExtra("orderdate", str1);
startActivity(extras);
}
});
}
正如您所看到的,我在列表适配器中的单个记录中只显示了四个属性。现在我想在onItemClick下显示另一个活动中的所有属性(有16个)。 上面的onItemClick正在显示其他活动的数据,但它为每个列表项显示相同的值(即第一个listView项的值)。那就是我被困住的地方!
注意:我只使用了两个textView进行测试,但我有16个属性,而且我不知道如何在另一个活动中显示所有16个属性。
请帮助!!
这是我想通过点击listView项目显示完整信息的其他活动:
public class ViewOrderDetails extends Activity {
TextView OrderId, CustomerId, Type, PriceList, TaxRate, Discount,
OrderDate, RequiredDate, Shipper, Freight, Name, Address, City,
Region, PostalCode, Country, Notes;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view_order_details);
CustomerId = (TextView) findViewById(R.id.tvcustomerID);
OrderId = (TextView) findViewById(R.id.tvorderID);
Type = (TextView) findViewById(R.id.tvType);
PriceList = (TextView) findViewById(R.id.tvPriceList);
TaxRate = (TextView) findViewById(R.id.tvTaxRate);
Discount = (TextView) findViewById(R.id.tvDiscount);
OrderDate = (TextView) findViewById(R.id.tvOrderDate);
RequiredDate = (TextView) findViewById(R.id.tvReqDate);
Shipper = (TextView) findViewById(R.id.tvShipper);
Freight = (TextView) findViewById(R.id.tvFreight);
Name = (TextView) findViewById(R.id.tvName);
Address = (TextView) findViewById(R.id.tvAddress);
City = (TextView) findViewById(R.id.tvCity);
Region = (TextView) findViewById(R.id.tvRegion);
PostalCode = (TextView) findViewById(R.id.tvPostalCode);
Country = (TextView) findViewById(R.id.tvCountry);
Notes = (TextView) findViewById(R.id.tvNotes);
Intent extras = getIntent();
if (extras.hasExtra("orderid") && (extras.hasExtra("orderdate"))) {
OrderId = (TextView) findViewById(R.id.tvorderID);
OrderId.setText(extras.getStringExtra("orderid"));
OrderDate.setText(extras.getStringExtra("orderdate"));
}
}
}
答案 0 :(得分:0)
在OnItemClickListener中检索值的方法是错误的。 尝试进行此更改。
创建对象并在对象中添加所有16个变量,并使对象可序列化。 然后创建Object列表并发送到适配器。 使用对象列表更新getView方法。 onItemCLick通过获取位置,从你创建的列表中获取对象。 将意图中的对象发送到其他活动。
我希望它有助于你...