我想要發生以下事情(一個簡單的想法):我點擊我的主要活動中的添加按鈕,輸入一些信息(到EditText框中),並返回到主屏幕。在這裏,我想顯示一個列表,只有兩個標題(不是所有的信息)。當我點擊該列表項目時,我想顯示該項目的相應保存信息。因此,每件商品的信息會有所不同。具體OnItemClick意圖Android
當我這樣做時,我使用主屏幕上的startActivityForResult(),然後返回。這對於一個項目來說是完美的。當我添加另一個項目時,問題就出現了。無論點擊哪個項目,顯示的信息都是相同的。總之,我需要找到一種方法來保存每個項目獨有的信息。
截至目前,我有下面的代碼片段:
//After I add all the information in the second intent
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 1) {
//these display in the list (Each list item has two textfields,row1 and row 2)
row1 = data.getStringExtra("com.painLogger.row1");
row2 = data.getStringExtra("com.painLogger.row2");
//below is the other info entered
painLevelString = data.getStringExtra("com.painLogger.painLevel");
painLocation = data.getStringExtra("painLocation");
timeOfPainString = data.getStringExtra("com.painLogger.painTime");
textTreatmentString = data
.getStringExtra("com.painLogger.treatment");
addItem();
}
}
//When I click the item-- this is the info that is not unique...
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent intent = new Intent(this, Item1.class);
intent.putExtra("com.painLogger.painLevel", painLevelString);
intent.putExtra("com.painLogger.painTime" , timeOfPainString);
intent.putExtra("com.painLogger.treatment", textTreatmentString);
intent.putExtra("painLocation", painLocation);
startActivity(intent);
}
// 編輯:添加項目CODE添加
private void addItem() {
HashMap<String, String> map = new HashMap<String, String>();
map.put("row_1", row1);
map.put("row_2", row2);
painItems.add(map);
adapter.notifyDataSetChanged();
}
// * 編輯:一些定義*
SimpleAdapter adapter;
List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
我可以根據堅持這是爲什麼發生 - 每次我點擊一個項目,意圖是把最後一個額外的,意味着最後一個對象的信息添加。我如何讓每個OnItemClick獨一無二?我懷疑我將不得不使用位置變量。
你要保存在哪裏? –
你在哪裏保存每個項目的信息? – user802421
不幸的是,我認爲這是問題所在。我只保存數組列表,如我以前的問題所示(此時正在工作):http://stackoverflow.com/questions/6839965/non-functional-writing-and-getting-arrarylist-android/6840035 #6840035。那麼我將如何去保存每個項目的信息呢? – Kgrover