项目点击事件无法正常工作

时间:2013-05-16 18:32:26

标签: android listview baseadapter

项目点击事件仅在首次创建列表激活时有效。如果我离开活动或关闭申请,该活动将不再有效。只有我重新安装应用程序。有什么帮助吗?

MY ACTIVITY

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
        String[] fields = new String[] {
                "Nome da disciplina",
                "Número de faltas permitidas",
                "Número de faltas cometidas",
                "Hora do término",
                "Dias da semana"
        };

       setListAdapter(new DisciplineFormAdapter(this,fields));

 }




protected void onListItemClick(ListView l, View v, int position, long lo){
     super.onListItemClick(l, v, position, lo);
     Toast.makeText(this, "toast", Toast.LENGTH_SHORT).show();


}

我的适配器

public DisciplineFormAdapter(Context c, String[] l){
    this.context = c;
    this.fields = l;
}

@Override
public int getCount() {

return fields.length;
}

@Override
public Object getItem(int position) {

return fields[position];
}

@Override
public long getItemId(int position) {

return position;
}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

    String field = fields[position];
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.activity_discipline, null);


    TextView fieldView = (TextView) v.findViewById(R.id.field);
    fieldView.setText(field);

    TextView name = (TextView) v.findViewById(R.id.value);
    name.setText("value");

    return v;
}

MY XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_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"
android:orientation="vertical"
tools:context=".DisciplineActivity" >


<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/field"
    android:textSize="17sp"
    android:textStyle="bold" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/value" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您正在使用listItemAdapter(自定义)。将事件侦听器放入适配器的getView:

public View getView(int position, View convertView, ViewGroup parent) {

    String field = fields[position];
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.activity_discipline, null);


    TextView fieldView = (TextView) v.findViewById(R.id.field);
    fieldView.setText(field);

    TextView name = (TextView) v.findViewById(R.id.value);
    name.setText("value");

     v.setOnClickListener(new View.OnClickListener() {          
            @Override
            public void onClick(View v) {       
            ... do something...
        }
    });



return v;

}

其中v是您的观点。

如果要显示祝酒词:

Toast.makeText(this, "toast", Toast.LENGTH_SHORT).show();

你需要回复你的主要ui线程 - 不要试图从这个背景线程中尝试更新UI - 你的应用程序会崩溃!