Java ContextMenu无法使用自定义适配器从ListView中删除行
问题是:My ContextMenu无法使用自定义适配器和 应用程序崩溃,错误为:UnsupportedOperationException,请参阅Logcat输出 我做过谷歌搜索和stackoverflow搜索。我所找到的任何信息都不能解决这个问题 我的问题是:我的代码有什么问题? 请提供解决此问题的正确代码 Java代码:Java ContextMenu无法使用自定义适配器从ListView中删除行,java,android,listview,android-adapter,android-contextmenu,Java,Android,Listview,Android Adapter,Android Contextmenu,问题是:My ContextMenu无法使用自定义适配器和 应用程序崩溃,错误为:UnsupportedOperationException,请参阅Logcat输出 我做过谷歌搜索和stackoverflow搜索。我所找到的任何信息都不能解决这个问题 我的问题是:我的代码有什么问题? 请提供解决此问题的正确代码 Java代码: public class TestActivity extends ListActivity { private String item
public class TestActivity extends ListActivity {
private String itemNameArray[];
private String dateArray[];
private ListView listview;
CustomListViewAdapter customListViewAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
registerForContextMenu(getListView());
root = Environment.getExternalStorageDirectory() + File.separator + "/ListTestFiles";
getDir(root);
listview = getListView();
name=itemNameArray;
lastmod=dateArray;
customListViewAdapter = new CustomListViewAdapter(this, name, lastmod);
listview.setAdapter(customListViewAdapter);
} // End of onCreate code.
class CustomListViewAdapter extends ArrayAdapter<String> {
Context context;
String[] nameArray;
String[] modifiedArray;
CustomListViewAdapter(Context c, String[] name, String[] lastmod)
{
super(c, R.layout.layout_item_view, R.id.rowtext, name);
this.context = c;
this.nameArray = name;
this.modifiedArray = lastmod;
}
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.layout_item_view, parent, false);
TextView myName;
TextView myLastMod;
myName=(TextView) row.findViewById(R.id.rowtext);
myName.setText(nameArray[position]);
myLastMod=(TextView) row.findViewById(R.id.textView6);
myLastMod.setText(modifiedArray[position]);
return row;
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem itemMenu) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)itemMenu.getMenuInfo();
switch (itemMenu.getItemId()) {
case R.id.context_menu_rename:
Toast.makeText(this, "Rename", Toast.LENGTH_SHORT).show();
return true;
case R.id.context_menu_delete:
// NOTE TESTING the line of code below caused a runtime error: java.lang.UnsupportedOperationException
customListViewAdapter.remove(customListViewAdapter.getItem(info.position));
customListViewAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "position = " + info.position, Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
return true;
return super.onContextItemSelected(itemMenu);
}
}
}
只需从该特定位置的名称数组和修改数组中删除项,然后通知适配器 我明白了。一个大错误是在需要动态大小数组的地方使用固定大小数组 换衣服
String nameArray[];
String modifiedArray[];
到
arraylistnamearray;
ArrayList修饰Darray;
我修改了你的工作:
public class TestActivity extends ListActivity {
private ArrayList<String> itemNameArray;
private ArrayList<String> dateArray;
private ListView listview;
CustomListViewAdapter customListViewAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
registerForContextMenu(getListView());
String root = Environment.getExternalStorageDirectory() + File.separator + "/ListTestFiles";
getDir(root);
listview = getListView();
CustomListViewAdapter = new CustomListViewAdapter(this, itemNameArray, dateArray);
listview.setAdapter(customListViewAdapter);
}
// End of onCreate code. class
CustomListViewAdapter extends ArrayAdapter<String> {
ArrayList<String> nameArray;
ArrayList<String> modifiedArray; CustomListViewAdapter(Context c, ArrayList<String> name, ArrayList<String> lastmod) {
super(c, 0, name);
this.nameArray = name;
this.modifiedArray = lastmod;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(parent.get context());
convertView = inflater.inflate(R.layout.layout_item_view, parent, false);
TextView myName;
TextView myLastMod;
myName=(TextView) row.findViewById(R.id.rowtext);
myName.setText(nameArray.get(position));
myLastMod=(TextView) row.findViewById(R.id.textView6);
myLastMod.setText(modifiedArray.get(position));
return convertView;
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem itemMenu) {
switch (itemMenu.getItemId()) {
case R.id.context_menu_rename:
Toast.makeText(this, "Rename", Toast.LENGTH_SHORT).show();
break;
case R.id.context_menu_delete: itemNameArray.remove(listView.getSelectedItemPosition());
dateArray.remove(listView.getSelectedItemPosition()); customListViewAdapter.notifyDataSetChanged();
break;
}
return true;
}
}
公共类测试活动扩展了ListActivity{
私有ArrayList itemNameArray;
私有数组列表日期数组;
私有列表视图列表视图;
CustomListViewAdapter CustomListViewAdapter;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_测试);
registerForContextMenu(getListView());
String root=Environment.getExternalStorageDirectory()+File.separator+“/ListTestFiles”;
getDir(根);
listview=getListView();
CustomListViewAdapter=新的CustomListViewAdapter(this、itemNameArray、dateArray);
setAdapter(customListViewAdapter);
}
//onCreate代码.class的结束
CustomListViewAdapter扩展了ArrayAdapter{
ArrayList名称数组;
ArrayList ModifierDarray;CustomListViewAdapter(上下文c、ArrayList名称、ArrayList lastmod){
super(c,0,name);
this.nameArray=name;
this.modifiedArray=lastmod;
}
公共视图getView(int位置、视图转换视图、视图组父级){
LayoutInflater充气器=LayoutInflater.from(parent.get context());
convertView=充气机。充气(R.layout.layout\u item\u视图,父视图,false);
TextView我的名字;
文本视图myLastMod;
myName=(TextView)row.findViewById(R.id.rowtext);
setText(nameArray.get(position));
myLastMod=(TextView)row.findViewById(R.id.textView6);
myLastMod.setText(modifiedArray.get(position));
返回视图;
}
}
@凌驾
public void onCreateContextMenu(ContextMenu菜单,视图v,ContextMenu.ContextMenuInfo菜单信息){
super.onCreateContextMenu(menu,v,menuInfo);
getMenuInflater().充气(R.menu.context_菜单,菜单);
}
@凌驾
选择公共布尔OnContextItem(MenuItemMenu){
开关(itemMenu.getItemId()){
案例R.id.context\u菜单\u重命名:
Toast.makeText(这个“重命名”,Toast.LENGTH_SHORT).show();
打破
case R.id.context_菜单_删除:itemNameArray.remove(listView.getSelectedItemPosition());
dateArray.remove(listView.GetSelectEditePosition());customListViewAdapter.notifyDataSetChanged();
打破
}
返回true;
}
}
1。你是说删除nameArray和ModifierDarray的所有代码?2.如果我这样做,我的ListView将不会在TextView中显示任何数据。请删除在该位置找到的特定项。由于您有两个ArrayList:nameArray和modifiedArray,因此必须在每个数组上删除该位置的项;私有字符串日期数组[];1.我的ListView(包括CustomListViewAdapter类及其字符串数组)在显示ListView和所有填充了数据的行方面运行良好。2.我现在的目标是删除通过长时间单击选定的整行。3.我并不是只想从行中的文本视图中删除数据。4.下面的代码似乎试图仅删除行中文本视图中的数据:itemNameArray.remove(listView.GetSelectEditePosition());删除(listView.getSelectedItemPosition());我想,你不明白!!!!!!!!!!![]的数组,如字符串[]是用于固定大小的数组!!!;;!!!!!!!!!!!!!!!!!!!!!!!你不能缩小或扩大它的大小!!!!!!!!要增加或减少数组中的项目数,必须使用集合数组包,如ArrayList!!!!!!!!!!!!!!!!!
ArrayList<String> nameArray;
ArrayList<String> modifiedArray;
public class TestActivity extends ListActivity {
private ArrayList<String> itemNameArray;
private ArrayList<String> dateArray;
private ListView listview;
CustomListViewAdapter customListViewAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
registerForContextMenu(getListView());
String root = Environment.getExternalStorageDirectory() + File.separator + "/ListTestFiles";
getDir(root);
listview = getListView();
CustomListViewAdapter = new CustomListViewAdapter(this, itemNameArray, dateArray);
listview.setAdapter(customListViewAdapter);
}
// End of onCreate code. class
CustomListViewAdapter extends ArrayAdapter<String> {
ArrayList<String> nameArray;
ArrayList<String> modifiedArray; CustomListViewAdapter(Context c, ArrayList<String> name, ArrayList<String> lastmod) {
super(c, 0, name);
this.nameArray = name;
this.modifiedArray = lastmod;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(parent.get context());
convertView = inflater.inflate(R.layout.layout_item_view, parent, false);
TextView myName;
TextView myLastMod;
myName=(TextView) row.findViewById(R.id.rowtext);
myName.setText(nameArray.get(position));
myLastMod=(TextView) row.findViewById(R.id.textView6);
myLastMod.setText(modifiedArray.get(position));
return convertView;
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem itemMenu) {
switch (itemMenu.getItemId()) {
case R.id.context_menu_rename:
Toast.makeText(this, "Rename", Toast.LENGTH_SHORT).show();
break;
case R.id.context_menu_delete: itemNameArray.remove(listView.getSelectedItemPosition());
dateArray.remove(listView.getSelectedItemPosition()); customListViewAdapter.notifyDataSetChanged();
break;
}
return true;
}
}