0
以下是我尝试创建列表视图:此代码适用于我的MainActivity,并且该应用根本无法工作。 我试图将我的布局通货膨胀包括到我的查看持有人,并打破了一切。我把我当前的代码(这是行不通的)和最后一点(当试图包含Layout到View Holder之前)。 此外,这是我改变的唯一部分,所以问题在这里。 请告诉我我做错了什么?有没有办法将Layout添加到ViewHolder?谢谢。ViewHolder无法创建ListView,无法将“inflater布局”存储为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;
}
我搜索了几个小时,但c无法找到答案。
初始化'viewHolder = new ViewHolder();'当view是null时'if(rowView == null){' – Piyush