ListView setOnItemClicklistener NullPointerException

时间:2017-04-09 07:03:05

标签: android listview android-arrayadapter

public  class CustomAdapter extends ArrayAdapter<Plan> implements View.OnClickListener {

    private ArrayList<Plan> planArrayList;
    int position;
    Context context;
    private static class ViewHolder{
        TextView workType;
        TextView zone;
        TextView division;
        TextView station;
        TextView Msg;
    }
    public CustomAdapter(ArrayList<Plan> plan,Context context){
        super(context,R.layout.plan_list,plan);
        this.planArrayList=plan;
        this.context=context;

    }
    @Override
    public void onClick(View v) {

    }
    private int lastPosition = -1;
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Plan plans = getItem(position);
        ViewHolder viewHolder;
        final View result;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null){
            viewHolder = new ViewHolder();
            //LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.plan_list,parent,false);
            viewHolder.workType = (TextView) convertView.findViewById(R.id.plan_worktype);
            viewHolder.zone = (TextView) convertView.findViewById(R.id.plan_zone);
            viewHolder.division = (TextView) convertView.findViewById(R.id.plan_division);
            viewHolder.station = (TextView) convertView.findViewById(R.id.plan_station);
            viewHolder.Msg = (TextView) convertView.findViewById(R.id.)
            result=convertView;
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
            result = convertView;
        }
        Animation animation = AnimationUtils.loadAnimation(context, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
        result.startAnimation(animation);
        Plan plan = planArrayList.get(position);
        lastPosition = position;
        viewHolder.workType.setText(plans.getWorkType());
        viewHolder.zone.setText(plans.getZone());
        viewHolder.division.setText(plans.getDiv());
        viewHolder.station.setText(plans.getSTA());
        viewHolder.workType.setText(plan.getWorkType());
        viewHolder.zone.setText(plan.getZone());
        viewHolder.division.setText(plan.getDiv());
        viewHolder.station.setText(plan.getSTA());

        if (this.position == position){
            View view2 = inflater.inflate(R.layout.list_dialog,null);
            TextView worktype= (TextView) view2.findViewById(R.id.dlg_worktype);
           TextView zone = (TextView) view2.findViewById(R.id.dlg_zone);
            TextView div = (TextView) view2.findViewById(R.id.dlg_division);
            TextView sta = (TextView) view2.findViewById(R.id.dlg_station);
            TextView msg = (TextView) view2.findViewById(R.id.dlg_msg);
            worktype.setText(plan.getWorkType());
            zone.setText(plan.getZone());
            div.setText(plan.getDiv());
            sta.setText(plan.getSTA());
            msg.setText(plan.getMsg());
            return view2;
        }
        return convertView;
    }
    public void selectedItem(int position){
        this.position=position;
    }
}

当我在setOnItemClickListener项目上调用listView时,这些项目值需要显示在对话框中。

通过上述代码我得到的显示java.lang.NullPointerException:尝试从字段“android.widget.TextView com.example.nsurekha.entry_ex.CustomAdapter $ ViewHolder.workType”上读取空对象引用

1 个答案:

答案 0 :(得分:0)

如果你想在listView上使用onClick

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                String title=adapter.getItem(arg2).getTitle();
                String description=adapter.getItem(arg2).getDescription();
                String addedby=adapter.getItem(arg2).getAddedby();

            }
        });

现在,您可以轻松地显示包含所需数据的对话框。希望它适合你。