android - 选择特定项目后从 multi-AutoCompleteTextView 的列表中删除项目

标签 android multiautocompletetextview

如何从 multi-autocompleteTextview 中的列表中删除项目。所以,如果我在适配器中选择了位置 1 的项目,那么我应该删除它,以便永远不允许再次选择相同的项目。 引用链接:http://wptrafficanalyzer.in/blog/customizing-autocompletetextview-to-display-images-and-text-in-the-suggestion-list-using-simpleadapter-in-android/

我试过了

aList.remove(position);
adapter.notifyDataSetChanged();

但它不起作用。我已将其添加到 AdapterView.OnItemClickListener() 中 我怎样才能确保相同的值永远不会出现在多自动完成 TextView 中?非常感谢您的帮助。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

    // Array of strings storing country names
    String[] countries = new String[] {
        "India",
        "Pakistan",
        "Sri Lanka",
        "China",
        "Bangladesh",
        "Nepal",
        "Afghanistan",
        "North Korea",
        "South Korea",
        "Japan"
    };

    // Array of integers points to images stored in /res/drawable-ldpi/
    int[] flags = new int[]{
        R.drawable.india,
        R.drawable.pakistan,
        R.drawable.srilanka,
        R.drawable.china,
        R.drawable.bangladesh,
        R.drawable.nepal,
        R.drawable.afghanistan,
        R.drawable.nkorea,
        R.drawable.skorea,
        R.drawable.japan
    };

    // Array of strings to store currencies
    String[] currency = new String[]{
        "Indian Rupee",
        "Pakistani Rupee",
        "Sri Lankan Rupee",
        "Renminbi",
        "Bangladeshi Taka",
        "Nepalese Rupee",
        "Afghani",
        "North Korean Won",
        "South Korean Won",
        "Japanese Yen"
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Each row in the list stores country name, currency and flag
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

        for(int i=0;i<10;i++){
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("txt", countries[i]);
            hm.put("flag", Integer.toString(flags[i]) );
            hm.put("cur", currency[i]);
            aList.add(hm);
        }

        // Keys used in Hashmap
        String[] from = { "flag","txt"};

        // Ids of views in listview_layout
        int[] to = { R.id.flag,R.id.txt};

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.autocomplete_layout, from, to);

        // Getting a reference to CustomAutoCompleteTextView of activity_main.xml layout file
        CustomAutoCompleteTextView autoComplete = ( CustomAutoCompleteTextView) findViewById(R.id.autocomplete);

        /** Defining an itemclick event listener for the autocompletetextview */
        OnItemClickListener itemClickListener = new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {

                /** Each item in the adapter is a HashMap object.
                *  So this statement creates the currently clicked hashmap object
                * */
                HashMap<String, String> hm = (HashMap<String, String>) arg0.getAdapter().getItem(position);

                /** Getting a reference to the TextView of the layout file activity_main to set Currency */
                TextView tvCurrency = (TextView) findViewById(R.id.tv_currency) ;

                /** Getting currency from the HashMap and setting it to the textview */
                tvCurrency.setText("Currency : " + hm.get("cur"));
            }
        };

        /** Setting the itemclick event listener */
        autoComplete.setOnItemClickListener(itemClickListener);

        /** Setting the adapter to the listView */
        autoComplete.setAdapter(adapter);

        autoComplete.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer() );
    }

    /** A callback method, which is executed when this activity is about to be killed
    * This is used to save the current state of the activity
    * ( eg :  Configuration changes : portrait -> landscape )
    */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        TextView tvCurrency = (TextView) findViewById(R.id.tv_currency) ;
        outState.putString("currency", tvCurrency.getText().toString());
        super.onSaveInstanceState(outState);
    }

    /** A callback method, which is executed when the activity is recreated
    * ( eg :  Configuration changes : portrait -> landscape )
    */
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        TextView tvCurrency = (TextView) findViewById(R.id.tv_currency) ;
        tvCurrency.setText(savedInstanceState.getString("currency"));
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

最佳答案

尝试添加一行:

adapter.remove(the_string)

添加后:

aList.removeAt(position);
adapter.remove(the_string)
adapter.notifyDataSetChanged()

关于android - 选择特定项目后从 multi-AutoCompleteTextView 的列表中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30990497/

相关文章:

android - 如何将 List<ParseUser> 应用于 MultiAutoCompleteTextView?

android - 如何将 MultiAutoCompleteTextView 的值限制为仅来 self 的适配器的项目?

android - 学习约束布局有问题

android - 从android中的Multiautocomplete向数组添加值

android - Google map 布局如何动画化?

Android报错无法创建 "assets"文件夹因为它已经存在

java - Android MultiAutoCompleteTextView 具有自定义标记器,如 Whatsapp GroupChat

Android - 我什么时候应该在 Strings.xml 资源文件中转义 unicode?

java - Instagram布局如何将评论 TextView 和用户名 TextView 合为一体?