0

我正在开发一个应用程序,该应用程序ListView在文件中将电影声明strings.xml为字符串数组。每ListView行有 3 个元素,a.) 电影标题 b.) Gross c.) 发布日期。长按一行时,会弹出编辑该特定行的选项。如果用户点击它,他/她将被带到下一个屏幕,其中 3EditTexts对应于 Title、Gross 和 Date,他们可以在其中编辑所述条目。然而,必须首先使用初始数据填充编辑文本。

我的问题是,我似乎无法访问 的数据ListView,因此“编辑屏幕”没有填充初始数据。代码没有错误,但我知道我做错了什么,我不能把我的手指放在它上面。这是我的第一个屏幕的代码:

public void editEntry(int id) {
    Intent newData = new Intent(Lab8_082588.this, Lab8_082588Add.class);
    TextView movieTitle = (TextView) findViewById(R.id.title);
    TextView movieGross = (TextView) findViewById(R.id.gross);
    TextView movieDate = (TextView) findViewById(R.id.date);

    String startTitle = movieTitle.getText().toString();
    String startGross = movieGross.getText().toString();
    String startDate = movieDate.getText().toString();

    newData.putExtra(Lab8_082588Edit.TITLE_STRING, startTitle);
    newData.putExtra(Lab8_082588Edit.GROSS_STRING, startGross);
    newData.putExtra(Lab8_082588Edit.DATE_STRING, startDate);
    startActivityForResult(newData, Lab8_082588.EDIT_MOVIE);
}

我的适配器类:

    private class SampleCustomAdapter extends BaseAdapter {


    public SampleCustomAdapter(ArrayList<Lab8_082588FetchDetails> movies) {
        internalList = movies;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return internalList.size();
    }

    @Override
    public Object getItem(int index) {
        // TODO Auto-generated method stub
        return internalList.get(index);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View view;

        if (convertView == null) {
            view = inflater.inflate(R.layout.row, null);
        } else {
            view = convertView;
        }

        // extract the views to be populated
        TextView title = (TextView) view.findViewById(R.id.title);
        TextView gross = (TextView) view.findViewById(R.id.gross);
        TextView date = (TextView) view.findViewById(R.id.date);

        // extract the object that will fill these
        Lab8_082588FetchDetails movie = internalList.get(position);

        title.setText(movie.getTitle());
        date.setText(movie.getDate());
        gross.setText(movie.getGross());

        // return the view
        return view;
    }
}

我的 EditMovies 活动:

      @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addedit);
    initialize();
}

private void initialize() {
    // TODO Auto-generated method stub
    Intent prepopulate = getIntent();
    EditText movieTitle = (EditText) findViewById(R.id.etTitle);
    EditText movieGross = (EditText) findViewById(R.id.etGross);
    EditText movieDate = (EditText) findViewById(R.id.etDate);

    String startTitle =  prepopulate.getStringExtra(Lab8_082588Edit.TITLE_STRING);
    String startGross = prepopulate.getStringExtra(Lab8_082588Edit.GROSS_STRING);
    String startDate = prepopulate.getStringExtra(Lab8_082588Edit.DATE_STRING);

    movieTitle.setText(startTitle);
    movieGross.setText(startGross);
    movieDate.setText(startDate);
}

我的 FetchDetails 类

    public class Lab8_082588FetchDetails implements Comparable<Lab8_082588FetchDetails>     {

private String title;
private String gross;
private String date;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getGross() {
    return gross;
}

public void setGross(String gross) {
    this.gross = gross;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

@Override
public int compareTo(Lab8_082588FetchDetails another) {
    // TODO Auto-generated method stub
    return title.compareTo(another.title);
}

}

我选择的上下文项目:

    @Override
public boolean onContextItemSelected(MenuItem item) {
    if (item.getTitle() == "Edit Entry") {
        editEntry(item.getItemId());
    } else if (item.getTitle() == "Delete Entry") {
        deleteEntry(item.getItemId());
    } else {
        return false;
    }
    return true;
}

我该如何补救?

4

1 回答 1

0

您可以使用该[ListView].setOnItemClickListiner(listOnItemClickListener );方法并使用位置索引来获取相应电影的数据:

OnItemClickListener listOnItemClickListener = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position,
            long arg3) {

                    Lab8_082588FetchDetails movie = internalList.get(position);

                    //instead of passing int value pass movie object.
                    editEntry(movie);

                     //newData.putExtra(Lab8_082588Edit.TITLE_STRING, movie.getTitle());
                     //newData.putExtra(Lab8_082588Edit.GROSS_STRING, movie.getGross());
                     //newData.putExtra(Lab8_082588Edit.DATE_STRING, movie.getDate());

                     //startActivityForResult(newData, Lab8_082588.EDIT_MOVIE);

    }
};

如果这对您不起作用,您可以使用View.setTag()将数据标记到视图中getView()View.getView()获取该数据。

于 2012-08-18T14:52:00.370 回答