使用数据模型创建的 ListView 的交互行
我一直在关注这个教程,一切都很顺利,直到我决定自己实现一些东西。 我有以下适配器:
package alphabet.fast.scroll;
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
public class interactiveArrayAdapter extends ArrayAdapter<model> {
private final List<model> list;
private final Activity context;
public interactiveArrayAdapter(Activity context, List<model> list) {
super(context, R.layout.custom_row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected ImageView avatar;
protected TextView name;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.custom_row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.avatar = (ImageView) view.findViewById(R.id.avatar);
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkBox);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
model element = (model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.avatar.setImageResource(R.drawable.icon2);
holder.name.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
此模型:
package alphabet.fast.scroll;
import android.graphics.drawable.Drawable;
public class model {
private Drawable avatar;
private String name;
private boolean selected;
public model(String name) {
this.name = name;
selected = false;
}
public model(String name, Drawable avatar) {
this.avatar = avatar;
this.name = name;
selected = false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
和 MainActivity :
package alphabet.fast.scroll;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class tutorial extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ArrayAdapter<model> adapter = new interactiveArrayAdapter(this, getModel());
setListAdapter(adapter);
}
private List<model> getModel() {
List<model> list = new ArrayList<model>();
list.add(get("Linux"));
list.add(get("Windows7"));
list.add(get("Android"));
list.add(get("iPhone"));
list.add(get2("Problem coming up here", getResources().getDrawable(R.drawable.icon)));
return list;
}
private model get(String s) {
return new model(s);
}
private model get2(String s, Drawable av) {
return new model(s, av);
}
}
您能告诉我我的代码有什么问题以及为什么我的带有文本和可绘制对象的构造函数无法正常工作 - 我没有得到更新的头像
对于 ListView 中的 Problem come up here
行?
I have been following this tutorial and everything was going nice and easy until I decided to implement some stuff on my own.
I have the following adapter:
package alphabet.fast.scroll;
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
public class interactiveArrayAdapter extends ArrayAdapter<model> {
private final List<model> list;
private final Activity context;
public interactiveArrayAdapter(Activity context, List<model> list) {
super(context, R.layout.custom_row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected ImageView avatar;
protected TextView name;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.custom_row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.avatar = (ImageView) view.findViewById(R.id.avatar);
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.checkBox);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
model element = (model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.avatar.setImageResource(R.drawable.icon2);
holder.name.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
this model:
package alphabet.fast.scroll;
import android.graphics.drawable.Drawable;
public class model {
private Drawable avatar;
private String name;
private boolean selected;
public model(String name) {
this.name = name;
selected = false;
}
public model(String name, Drawable avatar) {
this.avatar = avatar;
this.name = name;
selected = false;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
and MainActivity :
package alphabet.fast.scroll;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class tutorial extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ArrayAdapter<model> adapter = new interactiveArrayAdapter(this, getModel());
setListAdapter(adapter);
}
private List<model> getModel() {
List<model> list = new ArrayList<model>();
list.add(get("Linux"));
list.add(get("Windows7"));
list.add(get("Android"));
list.add(get("iPhone"));
list.add(get2("Problem coming up here", getResources().getDrawable(R.drawable.icon)));
return list;
}
private model get(String s) {
return new model(s);
}
private model get2(String s, Drawable av) {
return new model(s, av);
}
}
Can you please tell me what is wrong with my code and why my constructor with text and drawable is not working properly - I don't get an updated avatar
for the Problem coming up here
row in the ListView ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是使用您正在执行的头像图像更新 imageView
holder.avatar.setImageResource(R.drawable.icon2);
而是您应该在 Avatar 类中创建一个方法,例如
public Drwable getAvatarImage(){
return this.avatar;
}
然后你应该执行以下操作在 getView 方法中:
holder.avatar.setImageResource(list.get(position).getAvatarImage);
instead of updateing the imageView with the avatar image you are doing
holder.avatar.setImageResource(R.drawable.icon2);
Rather you should make a method in the Avatar class like
public Drwable getAvatarImage(){
return this.avatar;
}
Then you should do as below in your getView method:
holder.avatar.setImageResource(list.get(position).getAvatarImage);