我有一个包含电影的列表视图。该列表在strings.xml中声明。用户可以将电影添加到此列表视图,可以编辑条目并删除条目。
我正在实施一种新的“插入”方法,该方法允许用户突出显示(即使用火箭,使用鼠标的轨迹球)每个条目,并在所选条目的正下方插入新电影。我的问题是,当我从插入活动中调用setResult时,我的代码中出现了arrayindexoutofbounds错误:
我的一个OnActivityResult案例:
case INSERT_MOVIE:
Lab8_082588FetchDetails insertMovie = new Lab8_082588FetchDetails();
ListView list = getListView();
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
NumberFormat formatterInsert = new DecimalFormat("###,###,###");
insertMovie.setTitle(data
.getStringExtra(Lab8_082588Insert.TITLE_STRING));
insertMovie.setGross("$"
+ formatterInsert.format(Double.parseDouble(data
.getStringExtra(Lab8_082588Insert.GROSS_STRING))));
insertMovie.setDate(data
.getStringExtra(Lab8_082588Insert.DATE_STRING));
results.add(insertMovie);
adapter.notifyDataSetChanged();
我的OnOptionsItemSelected:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection using item.getItemId()
switch (item.getItemId()) {
case R.id.add:
add();
break;
case R.id.insert:
insert(item);
break;
}
return true;
}
private void insert(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
Intent insertData = new Intent(Lab8_082588.this,
Lab8_082588Insert.class);
insertData.putExtra(Lab8_082588.CURRENT_ROW, info.position); //nullPointerException
startActivityForResult(insertData, Lab8_082588.INSERT_MOVIE);
}
我的插入类:
public class Lab8_082588Insert extends Activity {
public static final String TITLE_STRING = "TITLE_STRING";
public static final String GROSS_STRING = "GROSS_STRING";
public static final String DATE_STRING = "DATE_STRING";
public static final String PASSED_ROW = "PASSED_ROW";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addeditinsert);
Intent rowNumber = getIntent();
results.add(data.getIntExtra(Lab8_082588Insert.PASSED_ROW, 0) + 1, insertMovie);
adapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu3, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection using item.getItemId()
switch (item.getItemId()) {
case R.id.insert2:
next();
break;
}
return true;
}
private void next() {
// TODO Auto-generated method stub
EditText movieTitle = (EditText) findViewById(R.id.etTitle);
EditText movieGross = (EditText) findViewById(R.id.etGross);
EditText movieDate = (EditText) findViewById(R.id.etDate);
String title = movieTitle.getText().toString();
String gross = movieGross.getText().toString();
String date = movieDate.getText().toString();
if ((title.length() > 0) && (gross.length() > 0)
&& (date.length() == 4)) {
Intent data = getIntent();
int rowNumber = getIntent().getIntExtra(Lab8_082588.CURRENT_ROW, 0);
data.putExtra(Lab8_082588Insert.TITLE_STRING, title);
data.putExtra(Lab8_082588Insert.GROSS_STRING, gross);
data.putExtra(Lab8_082588Insert.DATE_STRING, date);
data.putExtra(Lab8_082588Insert.PASSED_ROW, rowNumber);
setResult(RESULT_OK, data);
finish();
}
}
答案 0 :(得分:0)
我不完全理解你的代码,但为什么你使用getCheckedItemPosition()
方法的重新启动作为新插入项目的索引?
如果你只想在用户选择的行之后插入,那么简单地传递用户点击的项目的位置(到 你不太明白我说的话。在 然后在insert活动中,只需获取该行号,并在返回结果时将其添加到intent: 然后将其传递给完成 在insertData
,我看到了你之前的问题,你知道如何做到这一点)并将其传递给将收集插入数据的活动。在插入活动中,然后沿用户插入的数据,返回行的位置(将其增加一个,以便您可以在下一个位置插入)并将其用作onActivityResult
中列表添加中的索引。 / p>
insert
方法中:private void insert(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
Intent insertData = new Intent(Lab8_082588.this,
Lab8_082588Insert.class);
insertData.put("CURRENT_ROW", info.position);// there is no need for extra data, you're going to insert it anyway
startActivityForResult(insertData, Lab8_082588.INSERT_MOVIE);
}
//
super.onCreate(savedInstanceState);
setContentView(R.layout.addeditinsert);
rowNumber = getIntent().getExtra("CURRENT_ROW");
Intent
: hobby.putExtra(TITLE_STRING, title);
hobby.putExtra(GROSS_STRING, gross);
hobby.putExtra(DATE_STRING, date);
hobby.putExtra("PASSED_ROW", rowNumber);
setResult(RESULT_OK, hobby);
finish();
onActivityResult
:results.add(data.getIntExtra("PASSED_ROW") + 1, insertMovie);