长按时如何访问ListView中的元素

时间:2012-08-19 03:30:16

标签: java android android-listview contextmenu

我正在处理一部有ListView部电影的应用程序。该列表在strings.xml中声明为数组。它包含元素TitleGrossDate Released。当长按行时,它会弹出一个上下文菜单,允许用户编辑所述行或将其删除。当用户选择编辑时,他/她将被带到第二个屏幕,其中包含与TitleGrossDate对应的3个编辑文本。 EditText字段使用单击行中的数据进行初始化。这是我的代码:

     @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String[] titleList = getResources().getStringArray(R.array.title_array);
    String[] grossList = getResources().getStringArray(R.array.gross_array);
    String[] dateList = getResources().getStringArray(R.array.date_array);

    results = new ArrayList<Lab8_082588FetchDetails>();

    for (int i = 0; i < titleList.length; i++) {
        Lab8_082588FetchDetails sr = new Lab8_082588FetchDetails();
        sr.setTitle(titleList[i]);
        sr.setGross(grossList[i]);
        sr.setDate(dateList[i]);
        results.add(sr);
    }

    adapter = new SampleCustomAdapter(results);
    setListAdapter(adapter);

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
            registerForContextMenu(lv);


      }
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();

    // places the contents of the XML to the menu
    inflater.inflate(R.menu.context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();
    switch (item.getItemId()) {
    case R.id.delete:
        results.remove(info.position);
        adapter.notifyDataSetChanged();
        return true;
    case R.id.edit:
        System.out.println(info.id);
        System.out.println(info.position);

        Intent newData = new Intent(Lab8_082588.this, Lab8_082588Edit.class);
        results.get(info.position);
        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);
        return true;
    default:
        return super.onContextItemSelected(item);

    }
}

对于编辑屏幕:

    public class Lab8_082588Edit 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";

@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.replaceAll(",", "").replace("$", ""));
    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);
}

 }

我的适配器:

  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;
    }
}

我的问题是,确实编辑文本已填充,但仅包含整个列表中第一项的数据(例如,泰坦尼克号是列表中的第一项,并且是唯一填充的项目)。即使我单击列表视图中的第n行电影,泰坦尼克号仍然是正在检索的那部。我该如何解决这个问题?

编辑:我意识到,不知何故,代码只考虑列表的第一个元素。如何访问其他行的元素?

1 个答案:

答案 0 :(得分:1)

  

我意识到,不知何故,代码只考虑第一个   列表的元素。如何访问其他行的元素?

对于findViewById行中的项目,您绝不应该使用ListView进行搜索。在onContextItemSelected回调中,您点击了元素的位置,以便您可以使用它来获取与此行相关联的数据:

case R.id.edit:
    Intent newData = new Intent(Lab8_082588.this, Lab8_082588Edit.class);
    // I hope you implemented the adapter correctly
    Lab8_082588FetchDetails item = (Lab8_082588FetchDetails) getListView().getItemAtPosition(info.position); 
    String startTitle = item.getTitle();
    String startGross = item.getGross();
    String startDate = item.getDate();

    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);
    return true;