0

I am pretty new to android development, so please don't flame me for stupid questions/mistakes!

I have a listview inside my activity, but i want to open up a fragment when a frame in the listview is clicked on. I have the onclick working, but it is not launching the new activity.

I'll include my code below.

This is the oncreate

protected void onCreate (Bundle savedInstanceState)
{
    //Fragment fragment_blank2=new SomeFragment();
    e = myBadData.getData();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.event_list);
    events=(ListView) findViewById(R.id.dayList);
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,e);
    events.setAdapter(adapter);
    events.setOnItemClickListener(this);
}

this is the onclick

public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l)
{
    myBadData.setId(i);
    Fragment fr = new event_description();

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();

    fragmentTransaction.add(R.id.eventdescription, fr);
}

this is the XML of the fragment

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

<!-- TODO: Update blank fragment layout -->


    <TextView
        android:layout_width="match_parent"
        android:layout_height="137dp"
        android:text="@string/event_name"
        android:textSize="26sp"
        android:layout_gravity="top"
        android:gravity="top|center"
        android:paddingTop="10dp"
        android:id="@+id/textView"
        android:background="@android:color/holo_orange_light"
        android:textColor="@android:color/black"
        android:layout_weight="1.08" />
    <TextView
        android:text="@string/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/date"
        android:layout_marginTop="@dimen/activity_top_margin"
        android:layout_marginLeft="@dimen/activity_side_margin"
        android:textSize="18sp"
        android:drawableTint="@android:color/background_dark" />
    <TextView
        android:text="@string/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/time"
        android:layout_marginTop="70dp"
        android:layout_marginLeft="@dimen/activity_side_margin"
        android:textSize="18sp" />
    <TextView
        android:text="@string/cost"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cost"
        android:layout_marginTop="@dimen/activity_top_margin"
        android:layout_marginRight="70dp"
        android:textSize="18sp"/>
    <TextView
        android:text="@string/address"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/address"
        android:layout_marginTop="70dp"
        android:layout_marginRight="60dp"
        android:textSize="18sp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="135dp"
        android:layout_marginLeft="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1">

            <TextView
                android:id="@+id/tv_long"
                android:paddingTop="10dp"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="@string/sample_text"
                android:textSize="18sp"
                android:layout_weight="1.08">
            </TextView>
        </LinearLayout>
    </ScrollView>

    <Button
        android:text="@string/add_to_calendar"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:id="@+id/button2"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="95dp"/>
</FrameLayout>

here is the class of the fragment

public class event_description extends Fragment
{
    @Nullable
    //@Override
    public View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate (R.layout.event_description, container, false);
        Bundle args = getArguments();
        String arg = args.getString(eventList.KEY_NAME);
        //return super.(R.layout.event_description, container, false);
        return view;
    }

    public View getView() {
      return getView();
    }
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Benjamin Li
  • 1
  • 1
  • 2
  • Of-topic, but instead of adding the fragment you may consider displaying the new activity, passing the necessary data toit upon creation if this might make your code clearer.. – Alexander Kulyakhtin Mar 30 '17 at 05:24
  • what do you mean by the view group of the fragment? – Benjamin Li Mar 30 '17 at 05:37
  • Please don't repeat questions. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working will bump it to the top of the active queue. – Mike M. Mar 30 '17 at 23:34

1 Answers1

0

For Your Activity XML

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- TODO: Update blank fragment layout -->

    <ListView
        android:id="@+id/dayList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

   </FrameLayout>

And Pass Activity's Container in FramgmentTransaction

public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l)
{
    myBadData.setId(i);
    Fragment fr = new event_description();

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();


    //Your Container of Activity
    fragmentTransaction.add(R.id.my_frame, fr);

    fragmentTransaction.commit();
}

and Don't forgot to commint it.

No need to use framgment's FrameLayout as a container..

9spl
  • 357
  • 1
  • 11
  • So basically two things to change ( 1 ) Container from Activity ( 2 ) Need to commit fragmentTrasaction , just try this and if you find userful than don't forgot to Check it as true..thanks.. – 9spl Mar 30 '17 at 06:13
  • i've tried that, and it does not seem to work. while it does not crash the app, it doesn't open up the new fragment – Benjamin Li Mar 30 '17 at 15:32