我有一个customlistadapter,我正在为我的listview设置它。现在我想删除它的一个项目。我在longclick事件中做了以下的事情,但它返回和FATAL异常。 projectItemArrayAdapter是我的customAdapter类的对象。 item是列表的位置。
Object item1 = projectItemArrayAdapter.getItem(item);
projectItemArrayAdapter.remove(item1);
projectItemArrayAdapter.notifyDataSetChanged();
09-01 16:07:39.564 3506-3506/com.example.anuradha.tblogin
E/AndroidRuntime﹕ FATAL EXCEPTION: main Process:
com.example.anuradha.tblogin, PID: 3506
java.lang.UnsupportedOperationException at
java.util.AbstractList.remove(AbstractList.java:638) at
java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
at java.util.AbstractCollection.remove(AbstractCollection.java:229)
ProjectAdapter类
public class ProjectAdapter extends ArrayAdapter {
private final Context context;
private final String[] values;
private final String[] titles;
private LayoutInflater inflater;
public ProjectAdapter(Context activity,String[] values,String[] titles)
{
super(activity,R.layout.activity_project_item,values);
//inflater = activity.getWindow().getLayoutInflater();
this.context = activity;
this.values = values;
this.titles = titles;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//return inflater.inflate(R.layout.activity_project_item,parent,false);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.activity_project_item, parent, false);
TextView project = (TextView) rowView.findViewById(R.id.projectName);
TextView title = (TextView) rowView.findViewById(R.id.projectTitle);
project.setText(values[position]);
title.setText(titles[position]);
if (position%2!=1)
{
String myHexColor = "#2d07101c";
//project.setBackgroundColor(Color.parseColor(myHexColor));
rowView.setBackgroundColor(Color.parseColor(myHexColor));
}
return rowView;
}
@Override
public void remove(Object object) {
super.remove(object);
}
}
ProjectList类 - 我将arrayadapter分配给我的listview
public class ProjectList extends Activity {
private ListView projectListView;
private String[] stringArray ;
private ArrayAdapter projectItemArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_project_list);
//Set custom ActionBar Color
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#D6D6D6")));
String[] projectName =
new String[] { "Demo", "Test", "Anu", "QDMS"};
String[] projTitle =
new String[] { "Demo Title", "Test Title", "Anu Title", "QDMS Title"};
projectItemArrayAdapter = new ProjectAdapter(this,projectName,projTitle);
projectListView = (ListView) findViewById(R.id.projectList);
projectListView.setAdapter(projectItemArrayAdapter);
this.showActionBar();
projectListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ProjectList.this,MailActiivty.class);
startActivity(intent);
}
});
projectListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long arg3) {
final int item = position;
AlertDialog.Builder alert = new AlertDialog.Builder(
ProjectList.this);
alert.setTitle("Delete");
alert.setMessage("Do you want delete this item?");
alert.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Object item1 = projectItemArrayAdapter.getItemId(item);
projectItemArrayAdapter.remove(item1);
projectItemArrayAdapter.notifyDataSetChanged();
}
});
alert.setNegativeButton("CANCEL",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alert.show();
return true;
}
});
}
//method to assign custom actionbar
private void showActionBar() {
LayoutInflater inflator = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.cust_actionbar, null);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled (false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(v);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_project_list, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
使用ArrayList而不是数组。将适配器构造函数更改为此。
public ProjectAdapter(Context activity,List<String> values,String[] titles)
{
super(activity,R.layout.activity_project_item,values);
//inflater = activity.getWindow().getLayoutInflater();
this.context = activity;
//change the type of values from String[] to List<String>
this.values = values;
this.titles = titles;
}
然后更改remove方法的实现。看起来应该是这样的
@Override
public void remove(Object object)
{
//remove the call to super.
values.remove(object);
}
然后更改此行
String[] projectName = new String[] { "Demo", "Test", "Anu", "QDMS"};
创建一个数组列表并将这些元素添加到它中,然后将列表传递给适配器构造函数。
让我知道它是否有效。干杯:)