Android:访问Listview行中的元素并将其存储在字符串中

时间:2012-08-18 14:29:36

标签: android listview arraylist listadapter

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

我的问题是,我似乎无法访问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;
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分: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()中的视图以获取该数据。