这是我的适配器:
public class MainAdapter extends BaseAdapter implements View.OnClickListener {
private Context context;
private LayoutInflater inflater;
private Map<Integer, Info[]> originalDatas;
public MainAdapter(Context context, Map<Integer, Info[]> infoList) {
this.context = context;
originalDatas = infoList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return originalDatas.size();
}
@Override
public Object getItem(int position) {
return originalDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = inflater.inflate(R.layout.grid_list_row, null);
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
viewHolder.rightText = (TextView) convertView.findViewById(R.id.rightText);
viewHolder.leftText = (TextView) convertView.findViewById(R.id.leftText);
viewHolder.centerText = (TextView) convertView.findViewById(R.id.centerText);
viewHolder.rightText.setTypeface(TypeFaces.getTypeFace(context, TypeFaces.FONT_REGULAR));
viewHolder.leftText.setTypeface(TypeFaces.getTypeFace(context, TypeFaces.FONT_REGULAR));
viewHolder.centerText.setTypeface(TypeFaces.getTypeFace(context, TypeFaces.FONT_REGULAR));
viewHolder.rightText.setOnClickListener(this);
viewHolder.centerText.setOnClickListener(this);
viewHolder.leftText.setOnClickListener(this);
convertView.setTag(viewHolder);
} else viewHolder = (ViewHolder) convertView.getTag();
Iterator<Integer> iterator = originalDatas.keySet().iterator();
while (iterator.hasNext()) {
Info[] infos = originalDatas.get(iterator.next());
viewHolder.rightText.setText(infos[0].getTitle());
viewHolder.centerText.setText(infos[1].getTitle());
if (infos[2] != null)
viewHolder.leftText.setText(infos[2].getTitle());
else
viewHolder.leftText.setVisibility(View.INVISIBLE);
}
return convertView;
}
@Override
public void onClick(View v) {
String titleText = ((TextView) v).getText().toString();
if (titleText != "") {
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("parent", titleText);
Toast.makeText(context, titleText, Toast.LENGTH_SHORT).show();
context.startActivity(intent);
}
}
static class ViewHolder {
TextView rightText;
TextView centerText;
TextView leftText;
}
}
它给了我一个nullpioterexception
viewHolder.rightText.setText(infos[0].getTitle());
为什么会出现这个问题?
我也用过这个:
while (iterator.hasNext() && viewHolder!=null) {
Info[] infos = originalDatas.get(iterator.next());
viewHolder.rightText.setText(infos[0].getTitle());
viewHolder.centerText.setText(infos[1].getTitle());
if (infos[2] != null)
viewHolder.leftText.setText(infos[2].getTitle());
else
viewHolder.leftText.setVisibility(View.INVISIBLE);
}
但是所有行都按第一行数据填充,当我选择textviews时它不起作用
答案 0 :(得分:3)
convertView = inflater.inflate(R.layout.grid_list_row, null);
final ViewHolder viewHolder;
if (convertView == null) {
convertView
在您的代码中永远不会为null,并且ViewHolder永远不会被初始化。改变它
final ViewHolder viewHolder
if (convertView == null) {
convertView = inflater.inflate(R.layout.grid_list_row, null);