0

我正在使用实现 sectionIndexer 的自定义 listView 和 ArrayAdapter。我正在使用 Json 解析来自我的服务器的数据。我可以在 listView 上显示数据,但是当我单击我的 listView 时,它应该转到下一个详细页面,但此时它不起作用。

这是代码

    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);

    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();
    }


}
}

从这个类中,我在 xml 中创建列表视图,活动

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >

  <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>

这是 listView 中每一行的 xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:focusable="false"
    android:text="t1" />

<TextView
    android:id="@+id/textView_country"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView_name"
    android:layout_below="@+id/textView_name"
    android:layout_marginTop="19dp"
    android:focusable="false"
    android:text="t2" />

<TextView
    android:id="@+id/textView_localarea"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView_country"
    android:layout_below="@+id/textView_country"
    android:layout_marginTop="15dp"
    android:focusable="false"
    android:text="t3" />

</RelativeLayout>

这是我的数组适配器类

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;
}

}

这是我的活动课

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();
    masjidnames = l.getlistOfMasjids();


    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);
        }



    });


}
}

我在 logcat 中没有收到任何错误详细信息,或者应用程序没有意外关闭,这就是我无法解决此问题的原因

请帮我解决这个问题,在此先感谢。

4

3 回答 3

0

Android 提供了关于 ANR 以及如何保持应用程序响应的非常好的文档……当我遇到 ANR 时,我经常参考该文档……

http://developer.android.com/training/articles/perf-anr.html

我为自己设置了一个规则,让 UI 线程只用于 UI 处理,db、io、网络应该在非 ui 线程上完成......我使用线程和处理程序......仍然有时需要制作 ui 线程等等,在那种情况下你没有选择,但尽可能避免这种情况......

您还可以使用 runOnUiThread 方法从其他非 ui 线程更新 ui...

于 2014-08-29T21:12:49.313 回答
0

对于自定义 listView,在从服务器获取数据时使用 AsyncTask 对您有好处:

/**
 * Background Async Task to Load all product by making HTTP Request
 * */
class LoadAllItems extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(YourActivityName.this);
        pDialog.setMessage("Loading Items. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
        //your method that get data from server which getlistOfMasjids() 
        //Then save them in arrays or ArrayList, after that you can save them in listview 
                  return null;                    
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        listview();
        pDialog.dismiss();
    }
}

正如您在 doInBackground 方法中所述,您可以使用函数 getlistOfMasjids(),之后 您可以将数据保存在数组或 Arraylist 中以将它们放入列表视图中。

对于 onPostExecute() 方法中的列表视图,它像这样:

public void listview(){
    adapter =new SelectMasjidAdapter(this, array1, array2,array3);
    lv.setAdapter(adapter);
}

对于列表视图动作监听器:

lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            showInfo(position); // here you can put the call for another class or details  item for specific position

        }
    }); 

希望真的有帮助,

于 2014-01-08T10:09:32.410 回答
0

发生 ANR 是因为您在SelectMasjid活动的主线程中执行长任务。GetJsonList代码应该在异步任务中移动。

这是AsyncTask的文档

于 2014-01-08T09:29:50.833 回答