gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:41:58 25 4
gpt4 key购买 nike

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

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

我的问题是,确实,编辑文本得到了填充,但只有整个列表中第一项的数据(例如,泰坦尼克号在列表中排在第一位,并且是唯一被填充的)。即使我单击 ListView 中的第 n 行电影,泰坦尼克号仍然是被检索的那个。我该如何解决这个问题?

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

最佳答案

I realize that somehow, the code is only considering the first element of the list. How do I access the elements of the other rows?

您不应该使用 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;

关于java - 如何在长按时访问 ListView 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12023997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com