Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

android - Am I creating my custom ArrayAdapter correctly?

My ListView populates correctly, but for some reason adding and removing is quirky and does not work properly! Am I doing something wrong?

Set things up in OnCreate()

listView = (ListView) findViewById(R.id.ListView);

        registerForContextMenu(listView); 

        deserializeQuotes();

        if(quotes == null || quotes.size() == 0){
            quotes = new ArrayList<Quote>();
            //populateDefaultQuotes();
            //serializeQuotes();
            //getQuotesFromYQL();
        }

        this.quotesAdapter = new QuoteAdapter(this, R.layout.mainrow, quotes);
        listView.setAdapter(this.quotesAdapter);

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                deserializeQuotes();
                Quote myQuote = quotes.get(position);
                Toast toast = Toast.makeText(getApplicationContext(), myQuote.getName(), Toast.LENGTH_SHORT);
                toast.show();
            }
        });

Quote Adapter Private Class

private class QuoteAdapter extends ArrayAdapter<Quote> {

        private ArrayList<Quote> items;

        public QuoteAdapter(Context context, int textViewResourceId,
                ArrayList<Quote> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.mainrow, null);
            }
            Quote q = items.get(position);
            if (q != null) {
                TextView nameText = (TextView) v.findViewById(R.id.nameText);
                TextView priceText = (TextView) v.findViewById(R.id.priceText);
                TextView changeText = (TextView) v.findViewById(R.id.changeText);

                if (nameText != null) {
                    nameText.setText(q.getSymbol());
                }
                if (priceText != null) {
                    priceText.setText(q.getLastTradePriceOnly());
                }
                if (changeText != null) {
                    changeText.setText(q.getChange());
                }
            }
            return v;
        }
    }

Remove an item from the list (THIS DOESNT WORK, DOES NOTHING)

@Override  
    public boolean onContextItemSelected(MenuItem item) {  
        if(item.getTitle()=="Remove"){
            deserializeQuotes();
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            quotesAdapter.remove(quotes.get(info.position));
            quotesAdapter.notifyDataSetChanged();
            serializeQuotes();
        }  
        else {
            return false;
        }  

        return true;  
    }  

Add an item to the list (THIS WORKS)

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        deserializeQuotes();
        this.quotesAdapter = new QuoteAdapter(this, R.layout.mainrow, quotes);      
        quotesAdapter.notifyDataSetChanged();
        listView.setAdapter(quotesAdapter);
    }
}

Here is how I serialize and deserialize

private void serializeQuotes(){
        FileOutputStream fos;
        try {
            fos = openFileOutput(Constants.FILENAME, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(quotes); 
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    private void deserializeQuotes(){
        try{
            FileInputStream fis = openFileInput(Constants.FILENAME);
            ObjectInputStream ois = new ObjectInputStream(fis);
            quotes = (ArrayList<Quote>) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The code seems to be alright. Can you try using this in your remove?:

if("Remove".equals(item.getTitle())) {
   //....
}

Edit:

I just noticed you are de-serializing "Quote" objects by calling deserializeQuotes(). Have you overridden the boolean equals(Object) method of Quote object? When you de-serialize, the object created are not the "same", i.e. they are new objects altogether and if you haven't overridden the equals method:

quotesAdapter.remove(quotes.get(info.position));

will fail because it won't find any Quote object to remove in the list.

Can you check that?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...