自定义listView上的ANR使用arrayAdapter和sectionIndexer

时间:2014-01-08 18:20:49

标签: java android listview android-arrayadapter sectionindexer

我使用自定义列表视图在右侧添加字母列表。我也使用了Indexer节。现在,当我可以在我的应用程序的屏幕上看到列表及其内容时,但是当我点击特定行时,它应该转到下一页。但该应用程序未能在此时做出响应

这是我的代码     

<SearchView
    android:id="@+id/searchView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</SearchView>

<com.deiontech.masjidtimetableapp.CustomListView 
    android:id="@+id/listViewformasjid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" 
    android:layout_below="@+id/searchView1"
    android:clickable="true"/>


 </RelativeLayout>

错误日志中出现此错误:ava.lang.NullPointerException Window&gt;中记录了异常详细信息。显示视图&gt;错误日志

java.lang.NullPointerException
    at com.deiontech.masjidtimetableapp.CustomListView.onDraw(CustomListView.java:66)
    at android.view.View.draw(View.java:13712)
    at android.widget.AbsListView.draw(AbsListView.java:3653)
    at android.view.View.draw(View.java:13596)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
    at android.view.View.draw(View.java:13594)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
    at android.view.View.draw(View.java:13594)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
    at android.view.View.draw(View.java:13715)
    at android.view.View.draw(View.java:13596)
    at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
    at android.view.View.draw(View.java:13715)

这是我的CustomListView

 public class CustomListView extends ListView {




    private Context ctx;

    private static int indWidth = 20;
    private String[] sections;
    private float scaledWidth;
    private float sx;
    private int indexSize;
    private String section;
    private boolean showLetter = true;
    private Handler listHandler;

    public CustomListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        ctx = context;

    }

    public CustomListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        ctx = context;

    }

    public CustomListView(Context context, String keyList) {
        super(context);
        ctx = context;

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        scaledWidth = indWidth * getSizeInPixel(ctx);
        sx = this.getWidth() - this.getPaddingRight() - scaledWidth;

        Paint p = new Paint();
        p.setColor(Color.WHITE);
        p.setAlpha(100);

        canvas.drawRect(sx, this.getPaddingTop(), sx + scaledWidth,
                this.getHeight() - this.getPaddingBottom(), p);
        //this is line 61 where I am getting error in errorlog
        indexSize = (this.getHeight() - this.getPaddingTop() - getPaddingBottom())
                / sections.length;

        Paint textPaint = new Paint();
        textPaint.setColor(Color.DKGRAY);
        textPaint.setTextSize(scaledWidth / 2);

        for (int i = 0; i < sections.length; i++)
            canvas.drawText(sections[i].toUpperCase(),
                    sx + textPaint.getTextSize() / 2, getPaddingTop()
                            + indexSize * (i + 1), textPaint);

        // We draw the letter in the middle
        if (showLetter & section != null && !section.equals("")) {

            Paint textPaint2 = new Paint();         
            textPaint2.setColor(Color.DKGRAY);
            textPaint2.setTextSize(2 * indWidth);

            canvas.drawText(section.toUpperCase(), getWidth() / 2,  getHeight() / 2, textPaint2);
        }
    }

    private static float getSizeInPixel(Context ctx) {
        return ctx.getResources().getDisplayMetrics().density;
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);
        if (adapter instanceof SectionIndexer)
            sections = (String[]) ((SectionIndexer) adapter).getSections();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            if (x < sx)
                return super.onTouchEvent(event);
            else {
                // We touched the index bar
                float y = event.getY() - this.getPaddingTop() - getPaddingBottom();
                int currentPosition = (int) Math.floor(y / indexSize);
                section = sections[currentPosition];
                this.setSelection(((SectionIndexer) getAdapter())
                        .getPositionForSection(currentPosition));
            }
            break;
        }
        case MotionEvent.ACTION_MOVE: {
            if (x < sx)
                return super.onTouchEvent(event);
            else {
                float y = event.getY();
                int currentPosition = (int) Math.floor(y / indexSize);
                section = sections[currentPosition];
                this.setSelection(((SectionIndexer) getAdapter())
                        .getPositionForSection(currentPosition));

            }
            break;

        }
        case MotionEvent.ACTION_UP: {

            listHandler = new ListHandler();
            listHandler.sendEmptyMessageDelayed(0, 30 * 1000);

            break;
        }
      }
        return true;
    }


    private class ListHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {            
            super.handleMessage(msg);           
            showLetter = false;
            CustomListView.this.invalidate();
        }


    }
    }

正如我所说我使用的是ArrayAdapter和sectionIndexer,这里是代码

 public class SelectMasjidAdapter extends ArrayAdapter< MasjidForListClass> implements SectionIndexer{

    private List< MasjidForListClass> itemList;
    private Context context;
    private static String sections = "abcdefghilmnopqrstuvz";

    ArrayList< MasjidForListClass> data;

    public SelectMasjidAdapter(List< MasjidForListClass> itemList, Context ctx) {
        super(ctx,R.layout.select_masjid_list_row, itemList);
        this.itemList = itemList;
        this.context = ctx;     
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return itemList.size();
    }

    @Override
    public MasjidForListClass getItem(int position) {
        // TODO Auto-generated method stub
        return super.getItem(position);
    }

    @Override
    public long getItemId(int arg0) {
        return itemList.get(arg0).hashCode();

    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View vi=arg1;
        if(arg1==null)

            vi = inflater.inflate(R.layout.select_masjid_list_row, null);

        TextView masjid_name_text = (TextView)vi.findViewById(R.id.textView_name); 
        TextView masjid_country_text = (TextView)vi.findViewById(R.id.textView_country); 
        TextView masjid_localarea_text = (TextView)vi.findViewById(R.id.textView_localarea); 



        MasjidForListClass m = itemList.get(arg0);

        masjid_name_text.setText(m.getMasjidname());
        masjid_country_text.setText(m.getMasjidcountry());
        masjid_localarea_text.setText(m.getMasjidlocalarea());

        return vi;
    }
    @Override
    public int getPositionForSection(int sectionIndex) {
        Log.d("ListView", "Get position for section");
        for (int i=0; i < this.getCount(); i++) {
            String item = this.getItem(i).getMasjidname().toLowerCase();
            if (item.charAt(0) == sections.charAt(sectionIndex))
                return i;
        }
        return 0;
    }
    @Override
    public int getSectionForPosition(int position) {
        Log.d("ListView", "Get section");
        return 0;
    }
    @Override
    public Object[] getSections() {
        Log.d("ListView", "Get sections");
        String[] sectionsArr = new String[sections.length()];
        for (int i=0; i < sections.length(); i++)
            sectionsArr[i] = "" + sections.charAt(i);

        return sectionsArr;
    }

    }

    And Finally this is my acivity class

    public class SelectMasjid extends Activity{

    SelectMasjidAdapter adapter;
    ListView masjidListView;
     List <MasjidForListClass> masjidnames;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_masjid_activity);
        GetJsonList l = new GetJsonList();
        AsyncTask<Void, Void, ArrayList<MasjidForListClass>> gLiat = l.execute();
        try {
            gLiat.get();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            masjidnames=l.masjidlist;
        Collections.sort(masjidnames, new Comparator<MasjidForListClass>() {
            public int compare(MasjidForListClass result1, MasjidForListClass result2) {
                return result1.getMasjidname().compareTo(result2.getMasjidname());
            }
        });

        masjidListView = (ListView)findViewById(R.id.listViewformasjid);
        adapter = new SelectMasjidAdapter(masjidnames,this);
        masjidListView.setAdapter(adapter);


        masjidListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                Intent i = new Intent(SelectMasjid.this, MasjidDetailedActivity.class);
                i.putParcelableArrayListExtra("selected", (ArrayList<? extends Parcelable>) masjidnames);
                i.putExtra("index", arg2);
                startActivity(i);
            }



        });


    }
}

一旦我认为问题应该存在,请通过customListView。请帮我解决一下这个。提前致谢。

0 个答案:

没有答案