gpt4 book ai didi

android - 交互式 ListView

转载 作者:搜寻专家 更新时间:2023-11-01 09:08:51 28 4
gpt4 key购买 nike

我试图在不扩展 ListActivity 类的情况下创建交互式 ListView。布局应该是左侧的 RatingBar 和标签向右。这是我目前的代码,没有任何编译错误,但在应用程序启动时崩溃:

public class ProDroid_BAActivity extends Activity {
private static final String[] items = { "lorem", "ipsum", "dolor", "sit",
"amet", "consectetuer", "adipiscing", "elit", "morbi", "vel",
"ligula", "vitae", "arcu", "aliquet", "mollis", "etiam", "vel",
"erat", "placerat", "ante", "porttitor", "sodales", "pellentesque",
"augue", "purus" };

ListView listv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<RowModel> adapter;

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

ArrayList<RowModel> list = new ArrayList<RowModel>();
for (String s : items) {
list.add(new RowModel(s));
}

adapter = new RatingAdapter(list);
listv.setAdapter(adapter);

}

private RowModel getModel(int position) {
return adapter.getItem(position);
}

class RatingAdapter extends ArrayAdapter<RowModel> {

public RatingAdapter(ArrayList<RowModel> list) {
super(ProDroid_BAActivity.this, R.layout.row, R.id.label, list);

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);

ViewHolder holder = (ViewHolder) row.getTag();
if (holder == null) {
holder = new ViewHolder(row);
row.setTag(holder);

RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar,
float rating, boolean fromTouch) {
Integer myPosition = (Integer) ratingBar.getTag();
RowModel model = getModel(myPosition);

model.rating = rating;

LinearLayout parent = (LinearLayout) ratingBar
.getParent();
TextView label = (TextView) parent
.findViewById(R.id.label);

label.setText(model.toString());
}
};

holder.rate.setOnRatingBarChangeListener(l);



}

RowModel model = getModel(position);

holder.rate.setTag(new Integer(position));
// holder.text.setTag(new Integer(position));
holder.rate.setRating(model.rating);
// holder.text.setText(model.label);

return row;
}

}

class RowModel {
String label;
float rating = 2.0f;

RowModel(String label) {
this.label = label;
}

public String toString() {
if (rating >= 3.0) {
return (label.toUpperCase());
}

return (label);
}
}
}

最佳答案

移动线:

ListView listv = (ListView) findViewById(R.id.listView1);

在设置内容 View 后的 onCreate() 方法中。

关于android - 交互式 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9897504/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com