0

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

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

我的 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>
4

1 回答 1

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 - 您的应用程序将崩溃!

于 2013-05-16T18:42:06.030 回答