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.

Menma
  • 799
  • 1
  • 9
  • 35
Kuthay Gumus
  • 764
  • 3
  • 12
  • 30

2 Answers2

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.

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99
  • 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 – Kuthay Gumus Dec 19 '13 at 09:37
  • 1
    "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. – Blo Dec 19 '13 at 09:45
  • 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.. – Kuthay Gumus Dec 19 '13 at 09:53
  • And I did not understand what to do or where to put with // Get the Context in Activity MainActivity.this / this // Get the Context from Fragments getActivity() getApplicationContext() – Kuthay Gumus Dec 19 '13 at 09:54
  • they are 500x375 lol :) what is suggested ? And facebook can show 500 images in my phone but I cannot show 10 images? :/ Should I dispose after scrolling ? – Kuthay Gumus Dec 19 '13 at 09:58
  • Post the LogCat with this error, I will try to help you with this. – Blo Dec 19 '13 at 10:01
  • It is too long my friend.. I tried with 10 images, It gives me error at 7th. I tried with 6 images, It gave me error at 4th.. – Kuthay Gumus Dec 19 '13 at 10:05
  • E/AndroidRuntime(1923): FATAL EXCEPTION: main E/AndroidRuntime(1923): Process: com.ukbteam.syap, PID: 1923 E/AndroidRuntime(1923): java.lang.ArrayIndexOutOfBoundsException: length=7; index=7 E/AndroidRuntime(1923): at com.ukbteam.syap.FragmentA$CapsAdapter.getView(FragmentA.java:73) E/AndroidRuntime(1923): at android.widget.AbsListView.obtainView(AbsListView.java:2240) E/AndroidRuntime(1923): at android.widget.ListView.makeAndAddView(ListView.java:1790) E/AndroidRuntime(1923): at android.widget.ListView.fillDown(ListView.java:691) E/AndroidRuntime(1923): at android.widget.ListView....... – Kuthay Gumus Dec 19 '13 at 10:06
  • 1
    Check your **Arrays** numbers. This is a clue: http://stackoverflow.com/a/17047194/2668136 | Position starts to 0, don't forget. – Blo Dec 19 '13 at 10:21
  • Yes, I understand now. Thanks for your update by the way. And lastly, do you know, my action tab looks changed. Why this happened, do you have any idea ? – Kuthay Gumus Dec 19 '13 at 11:58
  • I tried in another device, it looks latest action tab, but in my own device, it changed to 2.3 and older.. Interesting.. – Kuthay Gumus Dec 19 '13 at 12:10
  • 1
    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: http://developer.android.com/guide/topics/ui/actionbar.html#Style & http://android-developers.blogspot.fr/2011/04/customizing-action-bar.html. It mights help you to have the same Tab on all devices. – Blo Dec 19 '13 at 12:16
  • Thanks again. I will ask new questions soon I think, how can I reach you :D – Kuthay Gumus Dec 19 '13 at 13:01
  • ;) You can't on SO, but don't worry, there're a lot of gentle guys here to answer to your questions :D If I'll have time, I'll check your next questions. Good luck and good dev. – Blo Dec 19 '13 at 13:04
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.

Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74
  • Can you be more specific my friend? Whats gonna be inside of "public View onCreateView" and "public void onActivityCreated" ? – Kuthay Gumus Dec 19 '13 at 09:24
  • 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. – Kuthay Gumus Dec 19 '13 at 09:34
  • 1
    declare new `sAdapter(getActivity(), sLikes, images, sTimes);` – Rakeeb Rajbhandari Dec 19 '13 at 09:36