ListView onClick insied DialogFragment不起作用

时间:2015-10-30 00:15:07

标签: android android-listview android-dialogfragment

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

DialogFragment

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>

1 个答案:

答案 0 :(得分:1)

您没有将startActivity(intent)添加到OnClickListener