如何从自定义适配器获取特定列
How to get a specific column from a custom Adapter
如何从自定义适配器中检索 class 的特定属性?我试过标记,但没有成功。有什么想法可以实现吗?
我有以下自动完成适配器:
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 中点击监听器:
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);
}
});
只要设置一个item点击Listener,使用position作为索引,得到你想要的item。
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();
}
});
您可以通过这种方式将标签设置到适配器的 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();
如何从自定义适配器中检索 class 的特定属性?我试过标记,但没有成功。有什么想法可以实现吗?
我有以下自动完成适配器:
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 中点击监听器:
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);
}
});
只要设置一个item点击Listener,使用position作为索引,得到你想要的item。
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();
}
});
您可以通过这种方式将标签设置到适配器的 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();