onItemClickListener不触发自定义ArrayAdapter [英] onItemClickListener not firing on custom ArrayAdapter

查看:146
本文介绍了onItemClickListener不触发自定义ArrayAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动来自Web服务检索数据。这个数据是psented在$ P $一个的ListView 通过ArrayAdapter其膨胀一个 RelativeLayout的 TextViews 里面,没有任何幻想,它做工精细。

I have an Activity that retrieves data from a web service. This data is presented in a ListView via an ArrayAdapter which inflates a RelativeLayout with three TextViews inside, nothing fancy and it work fine.

现在我想实现一个详细活动当用户点击在ListView一个项目,应该叫,听起来很容易,但我不能为我的生活获得onItemClickListener工作在我的ArrayAdapter。

Now I want to implement a Details Activity that should be called when a user clicks an item in the ListView, sounds easy but I can't for the life of me get the onItemClickListener to work on my ArrayAdapter.

这是我的主活动

public class Schema extends Activity {

    private ArrayList<Lesson> lessons = new ArrayList<Lesson>();
    private static final String TAG = "Schema";
    ListView lstLessons;
    Integer lessonId;

    // called when the activity is first created.
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // can we use the custom titlebar?
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        // set the view
        setContentView(R.layout.main);

        // set the title
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);

        // listview called lstLessons
        lstLessons = (ListView)findViewById(R.id.lstLessons);

        // load the schema
        new loadSchema().execute();

        // set the click listeners
        lstLessons.setOnItemClickListener(selectLesson);      

    }// onCreate

// declare an OnItemClickListener for the AdapterArray (this doesn't work)
private OnItemClickListener selectLesson = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int i, long l) {  
        Log.v(TAG, "onItemClick fired!");
    }                
};

private class loadSchema extends AsyncTask<Void, Void, Void> {

    private ProgressDialog progressDialog; 

    // ui calling possible
    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(Schema.this,"", "Please wait...", true);
    }

    // no ui from this one
    @Override
    protected Void doInBackground(Void... arg0) {
    // get some JSON, this works fine
    }

    @Override
    protected void onPostExecute(Void result) {
        progressDialog.dismiss();
        // apply to list adapter
        lstLessons.setAdapter(new LessonListAdapter(Schema.this, R.layout.list_item, lessons));        
    }

我的ArrayAdapter code:

My ArrayAdapter code:

// custom ArrayAdapter for Lessons 
private class LessonListAdapter extends ArrayAdapter<Lesson> {
    private ArrayList<Lesson> lessons;

    public LessonListAdapter(Context context, int textViewResourceId, ArrayList<Lesson> items) {
        super(context, textViewResourceId, items);
        this.lessons = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_item, null);
        }
        Lesson o = lessons.get(position);

        TextView tt = (TextView) v.findViewById(R.id.titletext);
        TextView bt = (TextView) v.findViewById(R.id.timestarttext);
        TextView rt = (TextView) v.findViewById(R.id.roomtext);

        v.setClickable(true);
        v.setFocusable(true);

        tt.setText(o.title);
        bt.setText(o.fmt_time_start);
        rt.setText(o.room);
        return v;
    }
}// LessonListAdapter

本的main.xml

The main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:screenOrientation="portrait"
    >

    <!-- student name -->
    <TextView 
      android:id="@+id/schema_view_student"
      android:text="Name"  android:padding="4dip"
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
      android:gravity="center_vertical|center_horizontal"
      style="@style/schema_view_student"
    />

    <!-- date for schema -->    
    <TextView
      android:id="@+id/schema_view_title"   
      android:layout_height="wrap_content"
      android:layout_margin="0dip" 
      style="@style/schema_view_day"
      android:gravity="center_vertical|center_horizontal"
      android:layout_below="@+id/schema_view_student"         
      android:text="Date" android:padding="6dip"
      android:layout_width="fill_parent"
    />

    <!-- horizontal line -->
    <View
      android:layout_width="fill_parent"
      android:layout_height="1dip"
      android:background="#55000000"
      android:layout_below="@+id/schema_view_title"
    />

    <!--  list of lessons -->
    <ListView
      android:id="@+id/lstLessons" 
      android:layout_height="wrap_content"
      android:layout_width="fill_parent"
      android:layout_below="@+id/schema_view_title"
    />

</RelativeLayout>

在list_item.xml

The list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60px"
    android:padding="12dip">

    <TextView
        android:id="@+id/timestarttext"
        android:text="09:45"
        style="@style/LessonTimeStartText"

        android:layout_width="60dip"

        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"

        android:layout_height="fill_parent" android:gravity="center_vertical|right" android:paddingRight="6dip"/>

    <TextView
        android:id="@+id/titletext"
        android:text="Test"
        style="@style/LessonTitleText"

        android:layout_width="wrap_content"
        android:layout_height="fill_parent"

        android:layout_toRightOf="@+id/timestarttext"

        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" android:gravity="center_vertical|center_horizontal"/>

    <TextView
        android:id="@+id/roomtext"
        android:text="123"

        android:layout_width="wrap_content"
        android:layout_height="fill_parent"

        style="@style/LessonRoomText"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical" />

</RelativeLayout>

被搞乱这最后几个小时,我似乎无法让我的头周围的问题是什么。我的问题看起来非常相似,<一个href="http://stackoverflow.com/questions/5066220/get-specific-item-property-on-onlistitemclick-using-custom-arrayadapter">this问题,但我不延长ListActivity,所以我还是不知道我的onListClickItem()应该去。

Been messing with this for the last couple of hours and I can't seem to get my head around what the problem is. My problem looks very similar to this question, but I'm not extending ListActivity, so I still don't know where my onListClickItem() should go.

更新:现在我已经困惑这个好几天,但还是无法找到问题。

UPDATE: Now I've puzzled with this for several days and still can't find the issue.

我应该重写活动,这次延长ListActivity而不是活动?因为它提供了onItemClick方法本身并可能是更容易覆盖

Should I rewrite the activity, this time extending ListActivity instead of Activity? Because it provides the onItemClick method itself and is probably easier to overwrite.

或者,我应该在我的ArrayAdapter直接在每个getView()绑定一个监听器?我相信,我已阅读,这是不好的做法(我应该做的,因为我试过,但在我的岗位)。

Or, should I bind a listener directly in each getView() in my ArrayAdapter? I believe I have read this is bad practice (I should do as I tried and failed in my post).

推荐答案

中找到的错误 - 这似乎是的这个问题。添加机器人:可调焦=假到每个list_item.xml元素的解决了这个问题,并且的onclick现在触发与原来的code

Found the bug - it seems to be this issue. Adding android:focusable="false" to each of the list_item.xml elements solved the issue, and the onclick is now triggered with the original code.

这篇关于onItemClickListener不触发自定义ArrayAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆