从列表视图中删除项目

时间:2011-01-21 21:25:41

标签: android

如果我想通过点击它从列表视图中删除一个项目,我是否可以从我正在使用ArrayAdapter的ArrayList中删除该对象,还是直接从适配器中删除该对象并调用notifyDatasetChange? / p>

此外,我想将列表中的数据存储在磁盘上,是否保存了适配器或我使用的arraylist?

以下是代码:我想要的是保存列表中的数据并在活动再次启动时将其加载回来,但是一旦发生这种情况,我就不能再从列表中删除项目了。

public class EditKeywords extends ListActivity implements Serializable{

    private ArrayList<String> keyWordList;
    private EditText keywordEditText;
    private IconicAdapter adapter;
    private Button enterButton;
    private int position;
    private Button finishedButton;
    public static final String KEYWORDS = "keywords";
    public static final String ADAPTER = "adapter";


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editkeyowords);
        getKeywordsAndAdapter();
        keywordEditText = (EditText) findViewById(R.id.keywordedittext);
        setListAdapter(adapter);

        enterButton = (Button) findViewById(R.id.enterkeywords);
        enterButton.setOnClickListener(enterButtonListener);

        finishedButton = (Button) findViewById(R.id.finishedbutton);
        finishedButton.setOnClickListener(finishedButtonListener);
    }

    public void onResume(){
    super.onResume();
    getKeywordsAndAdapter();
    }

    public void onPause(Bundle savedInstanceState){
    super.onPause();
    writeKeywordsAndAdapter();
    }


    public void getKeywordsAndAdapter(){
    try {
        InputStream fi = openFileInput(KEYWORDS);

        if (fi!=null) {
            ObjectInputStream in = new ObjectInputStream(fi);
                    keyWordList = (ArrayList<String>) in.readObject();
                    in.close();
        }
    }
    catch (java.io.FileNotFoundException e) {
        // that's OK, we probably haven't created it yet
    }
    catch (Throwable t) {
        Toast
            .makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
            .show();
    }
    if(keyWordList == null){
        keyWordList = new ArrayList<String>();
    }

    try {
        InputStream fi = openFileInput(ADAPTER);

        if (fi!=null) {
            ObjectInputStream in = new ObjectInputStream(fi);
                adapter = (IconicAdapter) in.readObject();
                in.close();
        }
    }
    catch (java.io.FileNotFoundException e) {
        // that's OK, we probably haven't created it yet
    }
    catch (Throwable t) {
        Toast
            .makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
            .show();
    }
    if(adapter == null){
        adapter = new IconicAdapter();
    }
    }

    public void writeKeywordsAndAdapter(){

    try {
        OutputStream fi = openFileOutput(KEYWORDS, 0);

        if (fi!=null) {
            ObjectOutputStream out = new ObjectOutputStream(fi);
                out.writeObject(keyWordList);
                out.close();
        }
    }
    catch (java.io.FileNotFoundException e) {
        // that's OK, we probably haven't created it yet
    }
    catch (Throwable t) {
        Toast
            .makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
            .show();
    }

    try {
        OutputStream fi = openFileOutput(ADAPTER, 0);

        if (fi!=null) {
            ObjectOutputStream out = new ObjectOutputStream(fi);
            out.writeObject(adapter);
            out.close();
        }
    }
    catch (java.io.FileNotFoundException e) {
        // that's OK, we probably haven't created it yet
    }
    catch (Throwable t) {
        Toast
            .makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
            .show();
    }


    }

    public void onListItemClick(ListView parent, View v,
        int aPosition, long id) {

        ImageView image = (ImageView) findViewById(R.id.icon1);
        image.setImageResource(R.drawable.delete);
        image.setOnClickListener(deleteImageListener);
        getListView().getChildAt(aPosition).invalidate();
        //adapter.notifyDataSetInvalidated();
        position = aPosition;
    }

    public void startHome() {
    Intent intent = new Intent(this, ECS.class);
    this.startActivity(intent);
    finish();
    }

    public class IconicAdapter extends ArrayAdapter<String> implements Serializable {
    private static final long serialVersionUID = 6175078429973952022L;


    IconicAdapter() {
        super(EditKeywords.this, R.layout.rowkeywords, keyWordList);
    }


    public View getView(int position, View convertView,ViewGroup parent) {
        LayoutInflater inflater=getLayoutInflater();

        View row=inflater.inflate(R.layout.rowkeywords, parent, false);
        TextView label=(TextView)row.findViewById(R.id.label1);

        label.setText(keyWordList.get(position));
        return(row);
    }
    }

    private OnClickListener enterButtonListener = new OnClickListener() {

    @Override
    public void onClick(View arg0){

        if(keywordEditText.getText().toString() != ""){
        boolean unique = true;
        for(String s : keyWordList){
            if(s.equals(keywordEditText.getText().toString())){
            unique = false; 
            }
        }
        if(unique == true){
            adapter.add(keywordEditText.getText().toString());
            keywordEditText.setText("");
            adapter.notifyDataSetChanged();
        }
        }
    }
    };

    private OnClickListener deleteImageListener = new OnClickListener() {

    @Override
    public void onClick(View arg0){
        //keyWordList.remove(position);
        adapter.remove(keyWordList.get(0));
        adapter.notifyDataSetChanged();
        writeKeywordsAndAdapter();


    }
    };

    private OnClickListener finishedButtonListener = new OnClickListener() {

    @Override
    public void onClick(View arg0){
       writeKeywordsAndAdapter();
       startHome();
    }
    };

}

3 个答案:

答案 0 :(得分:2)

您必须将其从arrayList中删除,然后设置:adapter.notifyDataSetChanged();

答案 1 :(得分:1)

更改ArrayList或适配器应具有相同的效果,因为适配器只是在查看ArrayList。在任何一种情况下,您都需要通过notifyDatasetChanged宣布更改。

正如f20k所述,data storage指南中介绍了许多存储数据的选项。谈论存储适配器或数组列表是没有意义的,您将把数据存储在该列表中。

答案 2 :(得分:1)

我所做的是编辑数组列表,然后将setListAdapter(适配器)设置为新创建的适配器对象。

相关问题