开发者

Remove an item from a list view

If I want to remove an item from my list view by tapping it, do I remove the object from the ArrayList I'm using with my ArrayAdapter, or do I remove the object directly from the adapter and call the notifyDatasetChange?

Also I want to store the data from the list on the disk, do I save the adapter or the arraylist I use?

Here is the code: The idea I'm going for is to save the data from the list and load it back when the activity launches again, however once this happens I cannot delete items from the list anymore.

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);
            开发者_JS百科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();
    }
    };

}


you have to remove it from your arrayList and set then: adapter.notifyDataSetChanged();


Changing the ArrayList or adapter should have the same effect, since the adapter is just looking at the ArrayList. In either case you need to announce the change via notifyDatasetChanged.

As f20k mentioned, lots of options for storing data are explained in the data storage guide. It doesn't make sense to talk about storing an adapter or array list, you would be storing the data within that list.


What I did was edit the array list then set the setListAdapter(adapter) to a newly created adapter object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜