Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用CustomAdapter单击ListView_Java_Android_Listview_Android Fragments - Fatal编程技术网

Java 使用CustomAdapter单击ListView

Java 使用CustomAdapter单击ListView,java,android,listview,android-fragments,Java,Android,Listview,Android Fragments,我已经用CustomAdapter创建了一个ListView。一切都很完美,但是当点击一个项目时,我如何打开一个新的片段呢。请告诉我,我应该如何打开一个新的片段,通过一个图像和一个文本字段来描述listitem。 类文件: package com.basil.victor; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v4.app.Fragment; imp

我已经用CustomAdapter创建了一个ListView。一切都很完美,但是当点击一个项目时,我如何打开一个新的片段呢。请告诉我,我应该如何打开一个新的片段,通过一个图像和一个文本字段来描述listitem。 类文件:

package com.basil.victor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.io.IOException;
import java.io.InputStream;

public class Events extends Fragment {

private ListView listEvent;

String eventname[]={
    "Name",
    "of",
    "the",
    "events",
    "are",
    "present",
    "here"
};

String eventlogoname[]={
    "Logo",
    "name",
    "of",
    "events",
    "are",
    "present",
    "here"
};

Drawable[] arr=new Drawable[7];

String eventsubtitle []={
    "Subtitles",
    "of",
    "the",
    "events",
    "are",
    "present",
    "here"
};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_events, null);



for(int i=0;i<7;i++) {
    try {
        InputStream stream = getContext().getAssets().open(eventlogoname[i] + ".jpg");
        Drawable el = Drawable.createFromStream(stream, null);
        arr[i] = el;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

EventList adapter = new
        EventList(getActivity(), eventname, arr, eventsubtitle);
//ListView lv = (ListView)rootView.
listEvent=(ListView)view.findViewById(R.id.listEvent);
listEvent.setAdapter(adapter);


return view;
}
}
CustomListView适配器:

package com.basil.victor;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class EventList extends ArrayAdapter<String>{

private final Activity context;
private final String[] title;
private final Drawable[] banner;
private final String[] subtitle;
public EventList(Activity context,
              String[] title, Drawable[] banner, String[] subtitle) {
super(context, R.layout.list_single, title);
this.context = context;
this.title = title;
this.banner = banner;
this.subtitle = subtitle;

}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.event_row, null, true);

TextView txtTitle = (TextView) rowView.findViewById(R.id.event_title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.event_banner);
TextView subTitle = (TextView) rowView.findViewById(R.id.event_subtitle);


txtTitle.setText(title[position]);
imageView.setImageDrawable(banner[position]);
subTitle.setText(subtitle[position]);


return rowView;
}
}
根片段:--

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class RootFragment extends Fragment {

    private static final String TAG = "RootFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        /* Inflate the layout for this fragment */
        View view = inflater.inflate(R.layout.root_fragment, container, false);

        FragmentTransaction transaction = getFragmentManager()
                .beginTransaction();
        /*
         * When this container fragment is created, we fill it with our first
         * "real" fragment
         */
        transaction.replace(R.id.root_frame, new Events());

        transaction.commit();

        return view;
    }

} 
根片段xml:-

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_frame" >

</FrameLayout>
你的碎片

  package com.basil.victor;
    import android.graphics.drawable.Drawable;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListView;

    import java.io.IOException;
    import java.io.InputStream;

    public class Events extends Fragment {

    private ListView listEvent;

    String eventname[]={
        "Name",
        "of",
        "the",
        "events",
        "are",
        "present",
        "here"
    };

    String eventlogoname[]={
        "Logo",
        "name",
        "of",
        "events",
        "are",
        "present",
        "here"
    };

    Drawable[] arr=new Drawable[7];

    String eventsubtitle []={
        "Subtitles",
        "of",
        "the",
        "events",
        "are",
        "present",
        "here"
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_events, null);



    for(int i=0;i<7;i++) {
        try {
            InputStream stream = getContext().getAssets().open(eventlogoname[i] + ".jpg");
            Drawable el = Drawable.createFromStream(stream, null);
            arr[i] = el;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    EventList adapter = new
            EventList(getActivity(), eventname, arr, eventsubtitle);
    //ListView lv = (ListView)rootView.
    listEvent=(ListView)view.findViewById(R.id.listEvent);
    listEvent.setAdapter(adapter);


         listEvent.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {

                 FragmentTransaction trans = getFragmentManager()
                        .beginTransaction();
                /*
                 * IMPORTANT: We use the "root frame" defined in
                 * "root_fragment.xml" as the reference to replace fragment
                 */
                trans.replace(R.id.root_frame, new SecondFragment());

                /*
                 * IMPORTANT: The following lines allow us to add the fragment
                 * to the stack and return to it later, by pressing back
                 */
                trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                trans.addToBackStack(null);

                trans.commit();
                    }
                });

        return view;
        }
        }

在您的事件片段中,输入此代码可能有助于转到其他片段。

在您的事件片段中尝试此代码:

 listEvent.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                            .beginTransaction();
            Fragment profileFragment = new Profile();//the fragment you want to show
            Bundle bundle = new Bundle();
            bundle.putString("TITLE", parent.getItemAtPosition(position););
            profileFragment.setArguments(bundle);
            fragmentTransaction
                .replace(R.id.content_frame, profileFragment);//R.id.content_frame is the layout you want to replace
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

    }
});

您可以将附加到ListViewer或发生:无法解析方法“getBaseContext”正常工作!!!!我将getBaseContext更改为getContext,因为该类属于片段,因此getBaseContext在片段中可能不起作用,但如何从该片段移动到另一个片段内容框架是什么
 listEvent.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager()
                            .beginTransaction();
            Fragment profileFragment = new Profile();//the fragment you want to show
            Bundle bundle = new Bundle();
            bundle.putString("TITLE", parent.getItemAtPosition(position););
            profileFragment.setArguments(bundle);
            fragmentTransaction
                .replace(R.id.content_frame, profileFragment);//R.id.content_frame is the layout you want to replace
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

    }
});
Bundle bundle = getArguments();
String title = bundle.getString("TITLE") ;