1

我有一个包含电影的列表视图。该列表在 strings.xml 中声明。用户可以将电影添加到此列表视图,可以编辑条目并可以删除条目。

我正在实现一个新的“插入”方法,它允许用户突出显示(即,使用火箭头,使用鼠标的轨迹球)每个条目,并在所选条目的正下方插入一个新电影。我的问题是,当我从插入活动中设置结果时,我的代码中出现了 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();
    }
}
4

1 回答 1

0

我不完全理解您的代码,但您为什么使用该getCheckedItemPosition()方法的重新运行作为新插入项目的索引?

如果您只想在用户选择的行之后插入它们,那么只需传递用户单击的项目的位置(到insertData,我在上一个问题中看到您知道如何执行此操作)并将其传递给为插入收集数据的活动。在插入活动中,然后沿着用户插入的数据,返回行的位置(将其增加一,以便您可以在下一个位置插入)并将其用作列表添加中的索引onActivityResult

你不是很明白我说的话。在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);
于 2012-08-19T09:55:27.040 回答