我创建了一个 listView,它使用 JSON 解析显示 mySql 数据库中记录的部分属性。我在 listView 中显示了 4 个属性,我想要的是,当我单击 listView 项时,它会在我创建的另一个 Activity 中显示该记录的所有属性;其中只有textView来显示信息。
这是我的代码:
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 下的另一个 Activity 中显示所有属性(有 16 个)。 上面的 onItemClick 显示其他 Activity 的数据,但它为每个列表项显示相同的值(即第一个 listView 项的值)。这就是我被困住的地方!
注意:我只使用了两个 textView 进行测试,但我有 16 个属性,并且我不知道如何在另一个 Activity 中显示所有 16 个属性。
请帮忙!!
这是我的其他 Activity ,我想通过单击 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"));
}
}
}
最佳答案
OnItemClickListener 中检索值的方式错误。 尝试做这个改变。
- 创建对象列表并传递给适配器并更新您的 View 列表项对象。
- OnItemClick 从列表中获取对象而不是从中获取 适配器和获取值也是错误的。你应该采取观点 但你没有从列表的角度来看。所以,你得到相同的值 每次。
- 使您的对象可序列化,以便您可以直接在 bundle 中发送对象,而不是发送 16 个值。
创建对象并在对象中添加所有 16 个变量并使对象可序列化。 然后创建对象列表并发送给适配器。 使用对象列表更新 getView 方法。 onItemCLick 通过占据位置,从您创建的列表中获取对象。 将 Intent 对象发送到其他 Activity 。
希望对你有帮助...
关于android - 如何通过单击另一个 Activity 中的ListView项目来显示所有记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26086808/