【发布时间】:2016-09-14 11:26:18
【问题描述】:
以下是我创建列表视图的尝试:此代码用于我的 MainActivity,应用程序根本无法运行。 我试图将我的布局膨胀包含到我的视图持有者中,这破坏了一切。我将我当前的代码(不起作用)和它工作的最后一点(在尝试将布局包含到 View Holder 之前)都放入了。 这也是我唯一改变的部分,所以问题就在这里。 请告诉我我做错了什么?有没有办法将布局包含到 ViewHolder 中?谢谢。
更新代码基于答案(仍然不起作用):
public View getView(final int i, View view, ViewGroup viewGroup) {
View rowView = view;
ViewHolder viewHolder;
/*using HolderView to reuse view*/
if (rowView == null){
viewHolder = new ViewHolder();
//inflating the layout Once and then storing it in rowView for reusing
rowView = inflater.inflate(R.layout.row_super_class, null);
// configure view holder and store it for reusing
viewHolder.largeText = (TextView) view.findViewById(R.id.largeText);
viewHolder.smallText = (TextView) view.findViewById(R.id.smallText);
rowView.setTag(viewHolder);
} else{
viewHolder = (ViewHolder)rowView.getTag();
}
当前(不工作)代码:
public View getView(final int i, View view, ViewGroup viewGroup) {
View rowView = view;
ViewHolder viewHolder = new ViewHolder();
/*using HolderView to reuse view*/
if (rowView == null){
//inflating the layout Once and then storing it in rowView for reusing
rowView = inflater.inflate(R.layout.row_super_class, null);
// configure view holder and store it for reusing
viewHolder.largeText = (TextView) view.findViewById(R.id.largeText);
viewHolder.smallText = (TextView) view.findViewById(R.id.smallText);
}
/*using if condition to populate each row with correct information*/
if (i >= 0) {
viewHolder.largeText.setText(name[i]);
viewHolder.smallText.setText(quantity[i]);
view.setClickable(true);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context.getApplicationContext(), name[i] + " * " + quantity[i], Toast.LENGTH_SHORT).show();
Intent goToNextActivity = new Intent(context, ClassActivity.class);
goToNextActivity.putExtra("superClass_name", name[i]);
context.startActivity(goToNextActivity);
}
});
}
return view;
}
这是我的应用正常运行的最后一点:
public View getView(final int i, View view, ViewGroup viewGroup) {
/*using HolderView to reuse view*/
ViewHolder viewHolder = new ViewHolder();
view = inflater.inflate(R.layout.row_super_class, null);
viewHolder.largeText = (TextView) view.findViewById(R.id.largeText);
viewHolder.smallText = (TextView) view.findViewById(R.id.smallText);
/*using if condition to populate each row with correct information*/
if (i >= 0) {
viewHolder.largeText.setText(name[i]);
viewHolder.smallText.setText(quantity[i]);
view.setClickable(true);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context.getApplicationContext(), name[i] + " * " + quantity[i], Toast.LENGTH_SHORT).show();
Intent goToNextActivity = new Intent(context, ClassActivity.class);
goToNextActivity.putExtra("superClass_name", name[i]);
context.startActivity(goToNextActivity);
}
});
}
return view;
}
/*using viewHolder to cache row view */
public static class ViewHolder {
public TextView largeText;
public TextView smallText;
}
我搜索了几个小时,但找不到答案。
【问题讨论】:
-
当视图为空时初始化
viewHolder = new ViewHolder();if (rowView == null){
标签: android android-layout listview android-viewholder getview