2

I'm trying to develop a timeline like facebooks one on below.

enter image description here

You see, I have a ViewPager to navigate user between "Fragments" like feed, profile, about etc..

I have "main.xml" which has just viewpager on it like this;

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

and also I have fragmenta.xml, fragmentb.xml, fragmentc.xml. Lets inspect on fragmenta.xml, inside of it, I put;

<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:background="#FFCC00"
    tools:context=".FragmentA" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true">
    </ListView>

</FrameLayout>

And made singlerow.xml to create that boxes into a timeline like this;

<?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="#e67e22" 
        android:layout_margin=" 5dp">

        <ImageView
            android:layout_marginTop="5dp"
            android:layout_marginBottom="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/s1" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:layout_gravity="left"
                android:textAlignment="gravity"
                android:text="16 likes"/>

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:layout_gravity="right"
                android:textAlignment="gravity"
                android:text="43mins ago"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1">

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:layout_gravity="left"
                android:textAlignment="gravity"
                android:text="Like" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:layout_gravity="right"
                android:textAlignment="gravity"
                android:text="Share"/>

        </LinearLayout>
</LinearLayout>

I made it worked with just one static box. But when I try to run this structure I get an error on debug mode "Source not found EDIT SOURCE LOOKUP PATH"

After I tried to put my adapter and other stuffs about listview into fragmentA.java like this;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentA extends Fragment {

    ListView list;
    String[] sLikes;
    String[] sTimes;
    int[] images={R.drawable.p1, R.drawable.p2,R.drawable.p3};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a,container,false);

        Resources res = getResources();
        sLikes=res.getStringArray(R.array.likes);
        sTimes=res.getStringArray(R.array.times);

        list = (ListView) findViewById(R.id.listView1);
        sAdapter adapter = new sAdapter(this, sLikes, images, sTimes);
        list.setAdapter(adapter);
    }    


    class sAdapter extends ArrayAdapter<String>
    {
        Context context;
        int[] images;
        String[] likesArray;
        String[] timesArray;
        sAdapter(Context c,String[] likes, int imgs[], String[] tms)
        {
            super(c,R.layout.singlerow,R.id.textView2,likes);
            this.context=c;
            this.images=imgs;
            this.likesArray=likes;
            this.timesArray=tms;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //converting to java object
            LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row=inflater.inflate(R.layout.singlerow,parent,false);

            ImageView myImage= (ImageView) row.findViewById(R.id.imageView1);
            TextView myLikes = (TextView) row.findViewById(R.id.textView);
            TextView myTimes = (TextView) row.findViewById(R.id.textView1);

            myImage.setImageResource(images[position]);
            myLikes.setText(likesArray[position]);
            myTimes.setText(timesArray[position]);

            return row;
        }
    }
}

But in here,

list = (ListView) findViewById(R.id.listView1);
CapsAdapter adapter = new CapsAdapter(this, capsLikes, images, capsTimes);

findViewById and new CapsAdapter in underlined with red..

What am I missing here. I should not touch inside of FragmentA.java ? I will create another java document ? Please help me about this.

-I created SingleRow/Bow structure, -I created Fragments / putted adapter etc inside of it, -I pulled them into mainactivity.java document..

Can anyone help me about this ? Thank you.

2 Answers 2

6

First of all your Debug Console Error, you can find a solution in the comments on this answer: I get “Source not found” when debugging my Java code in Eclipse.
Then, the "red underline", it is because Fragments are not build exactly like Activity. You should read this article: Fragments vs Activities. There is an example to see that in your Fragment, you must to do the following:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // select an layout to inflate
    View view = inflater.inflate(R.layout.fragment_a,container,false);

    Resources res = getResources();
    sLikes=res.getStringArray(R.array.likes);
    sTimes=res.getStringArray(R.array.times);

    // call the views with this layout
    list = (ListView) view.findViewById(R.id.listView1); 
    // you see, you need to find the view with "view.find..."

    sAdapter adapter = new sAdapter(getActivity(), sLikes, images, sTimes);
    list.setAdapter(adapter);

    // return the view
    return view;
}    

Also "new CapsAdapter is underline red too", because you need to attach a Context to your adapter. This = is a context, but it's not applicable for a fragment. You need to find the Context which is:

// Get the Context in Activity
MainActivity.this or this  
// Get the Context from Fragments 
getActivity()  
getApplicationContext()  

Finally, you can make your adapter in other file, if there are several Class which create a ListView with the same adapter.

Hope this helps.


UPDATE:
For example, to make and set a listview to an adapter, you do the following:

// declare the Adapter
MyAdapter adapter = new MyAdapter(
                         this, // to attach the adapter to a context, an activity
                         MyFirstItem, // an item (string...)  
                         MySecondItem, // an other (image...)  
                         MyOtherItem // and another...  
                    ); // end of my adapter  

In the adapter, you specify the nature of it:

// this method says:
// "hello, i'm an adapter and I am made with..."
sAdapter(
      Context c, // "..a context.." (an attachment)
      String[] MyFirstItem, // "..a first item which is a string array.."
      int MySecondItem[], // "..a second, an image.."
      String[] MyOtherItem // etc
)  

To works, you should "attach" the adapter to "where it's building" => the Activity => the Context. And to declare the context, you have the following example:

  1. NameOfActivity.this or this, example:

    to quit an activity, you do MainActivity.this.finish(); or this.finish()

  2. getActivity(), example:

    same as above, but this time you do getActivity().finish(); if your are not IN the Activity class.

  3. getApplicationContext():

    to change a little, you want to make a Toast from a fragment, you should do: Toast.makeText(getApplicationContext(), "I'm in a Fragment...", Toast.LENGTH_LONG).show();

Hope it's more understandable.

Sign up to request clarification or add additional context in comments.

14 Comments

First,thanks for the answer mate. I applied this but whats gonna be inside of "getApplicationContext()" method? You saw my codes on top, what to move inside of it ? Thanks. Need your help
"what to move inside of it?" - If you are 3 fragments which are the same Adapter in that case class sAdapter extends ArrayAdapter, you can take this class sAdapter and create a new file to put this code in. After, you have just to import (import com.myapp.test.sAdapter;) this class adapter in your Fragments.
I got it now, thanks mate. But when I reach the bottom of scrolling (still there are 1-2 pictures), the program "unfortunately stopped" is happening. Why is that, any idea ? In logchat, red lines starts like this; -FATAL EXCEPTION:main, -Process etc... -java.lang.OutofMemoryError and goes on..
Check your Arrays numbers. This is a clue: stackoverflow.com/a/17047194/2668136 | Position starts to 0, don't forget.
You're welcome. And no, sorry, with these modifications, you didnot touch/customise your action tab from your fragment. You should check your style.xml and customise it with all version, see: developer.android.com/guide/topics/ui/actionbar.html#Style & android-developers.blogspot.fr/2011/04/…. It mights help you to have the same Tab on all devices.
|
2

Do the following:

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

    Resources res = getResources();
    sLikes=res.getStringArray(R.array.likes);
    sTimes=res.getStringArray(R.array.times);

    list = (ListView) findViewById(R.id.listView1);
return view;
}    

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    sAdapter adapter = new sAdapter(this, sLikes, images, sTimes);
        list.setAdapter(adapter);
}

You should fill in your listView on the onActivityCreated to avoid NullPointerErrors.

3 Comments

Can you be more specific my friend? Whats gonna be inside of "public View onCreateView" and "public void onActivityCreated" ?
I applied this structure mate but "new sAdapter(this, sLikes, images, sTimes);" underlined.. It says; The constructor FragmentA.CapsAdapter(FragmentA, String[], int[], String[]) is undefined. Please help me about this.
declare new sAdapter(getActivity(), sLikes, images, sTimes);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.