簡體   English   中英

從自定義 arrayadapter android 中刪除項目時發生致命異常

[英]Fatal Exception while removing item from custom arrayadapter android

我有一個 customlistadapter,我正在為我的列表視圖設置它。 現在我想刪除其中的一個項目。 我在 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)

項目適配器類

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 分配給我的列表視圖

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

}

使用 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"};

創建一個數組列表並將這些元素添加到其中,然后將列表傳遞給適配器構造函數。

讓我知道它是否有效。 干杯:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM