如何从自定义适配器获取特定列

时间:2017-11-01 13:29:04

标签: android autocomplete android-view android-adapter

如何从自定义适配器检索我的类的特定属性?我试过标签,但没有成功。任何想法如何才能实现?

我有自动完成的以下适配器:

final ArrayList<City> cities = new ArrayList<City>();
final CityAdapter arrayAdapter = new CityAdapter(this, android.R.layout.simple_dropdown_item_1line, cities);
autoCompleteTextViewDropDown.setAdapter(arrayAdapter);

我的观点看起来像这样:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    City city = (City) getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
        convertView = inflater.from(getContext()).inflate(R.layout.item_city, parent, false);
    }

    // Lookup view for data population
    TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
    tvName.setText(city.getName());

    final ImageView tvFavourite = (ImageView) convertView.findViewById(R.id.tvFavourite);

    // Populate the data into the template view using the data object
    if(city.favourite==false) tvFavourite.setImageResource(R.drawable.ic_action_star);
    else tvFavourite.setImageResource(R.drawable.ic_action_star_on);
    //IMPORTANT QUANDO PROGRAMAR A DIALOG BOX FAZER AQUI
    tvFavourite.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            tvFavourite.setImageResource(R.drawable.ic_action_star_on);
        }
    });


    convertView.setTag(String.valueOf(city.getName()));
    // Return the completed view to render on screen
    return convertView;
}

这是我的自动填充功能:

     final AutoCompleteTextView autoCompleteTextViewDropDown;
        autoCompleteTextViewDropDown = findViewById(R.id.autoCompleteTextViewDropDown);
...
autoCompleteTextViewDropDown.setAdapter(arrayAdapter);

单击MainActivity中的Listener:

autoCompleteTextViewDropDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                String Column_id = (String) arg0.getTag();
                coValue.setText(Column_id);
                if (!(autoCompleteTextViewDropDown.getText().toString().equals(""))){
                     autoCompleteTextViewDropDown.setText((String) arg0.getTag());
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        autoCompleteTextViewDropDown.showDropDown();
                    }
                }, 100);
            }
        });

2 个答案:

答案 0 :(得分:0)

只需设置一个项目单击Listener并使用position作为索引并获取所需的项目。

autoCompleteTextViewDropDown.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
       City city = (City) parent.getItemAtPosition(position);
       String name = city.getName();
}
});

答案 1 :(得分:0)

你可以这样做把标签设置到适配器的getView中的行视图,就像这个

一样
  @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
// Get the data item for this position
City city = (City) getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
    convertView = inflater.from(getContext()).inflate(R.layout.item_city, parent, false);
}

// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
tvName.setText(city.getName());

final ImageView tvFavourite = (ImageView) convertView.findViewById(R.id.tvFavourite);

// Populate the data into the template view using the data object
if(city.favourite==false) tvFavourite.setImageResource(R.drawable.ic_action_star);
else tvFavourite.setImageResource(R.drawable.ic_action_star_on);
//IMPORTANT QUANDO PROGRAMAR A DIALOG BOX FAZER AQUI
tvFavourite.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        tvFavourite.setImageResource(R.drawable.ic_action_star_on);
    }
});


convertView.setTag(String.valueOf(city.getName()));
// Return the completed view to render on screen
return convertView;
   }

然后

public void onItemClick(AdapterView<?> arg0, View arg1,int position, long id) {    
 String columid= (String) arg1.getTag();