难道我创建我的自定义ArrayAdapter是否正确?我的、自定义、是否正确、ArrayAdapter

2023-09-04 10:42:04 作者:那年夏天丶那场雨

我的ListView填充正确,但由于某种原因,添加和删除是古怪而不能正常工作!难道我做错了什么?

设置的东西在OnCreate中()

 的ListView =(ListView控件)findViewById(R.id.ListView);

        registerForContextMenu(ListView控件);

        deserializeQuotes();

        如果(行情== NULL || quotes.size()== 0){
            报价=新的ArrayList<报价>();
            // populateDefaultQuotes();
            // serializeQuotes();
            // getQuotesFromYQL();
        }

        this.quotesAdapter =新QuoteAdapter(这一点,R.layout.mainrow,行情);
        listView.setAdapter(this.quotesAdapter);

        listView.setOnItemClickListener(新OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>一种,视图V,INT位置,长的id){
                deserializeQuotes();
                报价myQuote = quotes.get(位置);
                吐司面包= Toast.makeText(getApplicationContext(),myQuote.getName(),Toast.LENGTH_SHORT);
                toast.show();
            }
        });
 

报价适配器专用类

 私有类QuoteAdapter扩展ArrayAdapter<报价> {

        私人的ArrayList<报价>项目;

        公共QuoteAdapter(上下文的背景下,INT textViewResourceId,
                ArrayList的<报价>项目){
            超(背景下,textViewResourceId,项目);
            this.items =项目;
        }

        @覆盖
        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            视图V = convertView;
            如果(V == NULL){
                LayoutInflater VI =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                V = vi.inflate(R.layout.mainrow,NULL);
            }
            报价Q = items.get(位置);
            如果(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);

                如果(nameText!= NULL){
                    nameText.setText(q.getSymbol());
                }
                如果(priceText!= NULL){
                    priceText.setText(q.getLastTradePriceOnly());
                }
                如果(changeText!= NULL){
                    changeText.setText(q.getChange());
                }
            }
            返回伏;
        }
    }
 

从列表中删除的项目(这不工作,不执行任何操作)

  @覆盖
    公共布尔onContextItemSelected(菜单项项){
        如果(item.getTitle()==删除){
            deserializeQuotes();
            AdapterContextMenuInfo信息=(AdapterContextMenuInfo)item.getMenuInfo();
            quotesAdapter.remove(quotes.get(info.position));
            quotesAdapter.notifyDataSetChanged();
            serializeQuotes();
        }
        其他 {
            返回false;
        }

        返回true;
    }
 
Android零基础入门 自定义ArrayAdapter

将项目添加到列表中(这工作)

 保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(结果code == RESULT_OK){
        deserializeQuotes();
        this.quotesAdapter =新QuoteAdapter(这一点,R.layout.mainrow,行情);
        quotesAdapter.notifyDataSetChanged();
        listView.setAdapter(quotesAdapter);
    }
}
 

下面是我如何序列化和反序列化

 私人无效serializeQuotes(){
        FileOutputStream中FOS;
        尝试 {
            FOS = openFileOutput(Constants.FILENAME,Context.MODE_PRIVATE);
            ObjectOutputStream的OOS =新的ObjectOutputStream(FOS);
            oos.writeObject(引号);
            oos.close();
        }赶上(FileNotFoundException异常E){
            e.printStackTrace();
        }赶上(IOException异常E){
            e.printStackTrace();
        }
    }

    @燮pressWarnings(未登记)
    私人无效deserializeQuotes(){
        尝试{
            的FileInputStream FIS = openFileInput(Constants.FILENAME);
            ObjectInputStream的OIS =新的ObjectInputStream(FIS);
            报价=(ArrayList的<报价>)ois.readObject();
        }赶上(FileNotFoundException异常E){
            e.printStackTrace();
        }赶上(IOException异常E){
            e.printStackTrace();
        }赶上(ClassNotFoundException异常E){
            e.printStackTrace();
        }
    }
 

解决方案

在code似乎是正常的。你可以尝试在你删除使用这样的:

 如果(删除.equals(item.getTitle())){
   // ....
}
 

编辑:

我只注意到通过调用 deserializeQuotes你反序列化报价对象()。你是否覆盖了布尔等于(对象)引用对象的方法?当你反序列化,创建的对象不是相同,也就是说,它们是新的对象完全,如果你有没有覆盖equals方法:

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

会失败,因为它找不到任何引用的对象列表中删除。

您可以检查?

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

解决方案

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?