我在DialogFragment中有一个列表视图,列表项的onClick不起作用。不知道我在哪里错了。实现了onItemClick,但没有用。下面的代码。

对话片段

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class DFragment extends DialogFragment  {
ListView mylist;
ArrayList dateitems ;
ArrayList descitems ;
ArrayList locationitems ;
ArrayList listitems ;



public void setList(ArrayList list){
    listitems = list;
}

public void setDateList(ArrayList list){
    dateitems = list;
}
public void setLocList(ArrayList list){
    locationitems = list;
}
public void setDescList(ArrayList list){
    descitems = list;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.popup_content, container,
            false);
   mylist = (ListView)  rootView.findViewById(R.id.poplist);
    TextView title = (TextView) rootView.findViewById(R.id.dialog_title);
    title.setText("Today's Events");
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            Intent intent = new Intent(getActivity(), SupportEventActivity.class);
            intent.putExtra("rvEventTitle", listitems.get(position).toString());
            intent.putExtra("rvEventDescription", descitems.get(position).toString());
            intent.putExtra("rvEventLocation", locationitems.get(position).toString());
            intent.putExtra("rvEventTimestamp", dateitems.get(position).toString());
        }
    });



    // Do something else
    return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            R.layout.popup_content_row, R.id.poprow_text, listitems);

    mylist.setAdapter(adapter);  }


布局文件。 popup_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/back"
>
<TextView
    android:id="@+id/dialog_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textSize="25dp"/>
<ListView
    android:id="@+id/poplist"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:divider="#8F8F8F"
    android:dividerHeight="2px"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#ffffff" /> </LinearLayout>

最佳答案

您没有在startActivity(intent)中添加OnClickListener

09-05 06:25