0

I'm trying to make a basic Tourist app for my hometown. I have an onItemClickListener set on a listView. My goal is that when a list item is clicked, I want to open a new activity to show the user more information about the selected item(address, phone number, GPS location, etc). I set all the textviews in the fragment before I send the intent, so, when I setContentView, it should be displayed with all the accurate information already populated.

The problem is, when the list view is clicked, the activity starts a blank activity. There is no error message and the logCat shows that the textviews were updated correctly but they just aren't displaying correctly with the layout.

I've been working on this for days and would appreciate any help that anyone can give. Thanks in advance. Code is below.

**This is the fragment that holds the arrayList with the information to be displayed.

public class FoodFragment extends Fragment {

public FoodFragment() {
    // Required empty public constructor
}

public TourSite currentSite;


@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.sites_listview, container, false);

    // Create an ArrayList of TourSite objects for main tab lists
    final ArrayList<TourSite> tourSites = new ArrayList<TourSite>();
    tourSites.add(new TourSite(R.string.ginasAddress, R.string.ginasNumber, R.string.ginasGPS,
            R.string.ginasWeblink, R.drawable.pizza_100, R.string.ginasName));
    tourSites.add(new TourSite(R.string.ninosAddress, R.string.ninosnumber, R.string.ninosGPS,
            R.string.ninosWeblink, R.drawable.pizza_100, R.string.ninosName));
    tourSites.add(new TourSite(R.string.topsAddress, R.string.topsDinerNumber,
            R.string.topsDinerGPS, R.string.topsWeblink, R.drawable.restaurant_100,
            R.string.topsName));
    tourSites.add(new TourSite(R.string.goldenEagleAddress, R.string.goldenEagleNumber,
            R.string.goldenEagleGPS, R.drawable.noodles_100, R.string.goldenEagleName));


    //Creates adapter that displays tourSites on tabs
    TourSiteListAdapter adapter = new TourSiteListAdapter(getActivity(), tourSites);

    //finds the listview that were using to display the images and names
    ListView listview = (ListView) rootView.findViewById(R.id.siteList);

    listview.setAdapter(adapter);


    //attaches Listener to listView in order to open new activity with further
    // information about the toursite
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            //finds the selected list item
            currentSite = tourSites.get(position);

            //inflates the layout we want to display using the intent below
            LayoutInflater inflater1 = (LayoutInflater) getContext().getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            View root = inflater1.inflate(R.layout.list_item_info, null, true);


            //finds the textViews in the layout that we want to update before
            // displaying in activity
            TextView addressView = (TextView) root.findViewById(R.id.addressView);
            TextView numberView = (TextView) root.findViewById((R.id.numberView));
            TextView gpsView = (TextView) root.findViewById(R.id.gpsView);
            TextView weblinkView = (TextView) root.findViewById(R.id.weblinkView);

            //some toursites dont have phone numbers or websites. This handles that
            // while also updateding the content to be displayed
            if (currentSite.hasAddress()) {
                addressView.setText(currentSite.getmAddress());

                addressView.setVisibility(View.VISIBLE);
            } else {
                addressView.setVisibility(View.GONE);
            }

            if (currentSite.hasPhone()) {
                numberView.setText(currentSite.getmPhone());

                numberView.setVisibility(View.VISIBLE);
            } else {
                numberView.setVisibility(View.GONE);
            }

            gpsView.setText(currentSite.getmGPSlink());

            gpsView.setMovementMethod(LinkMovementMethod.getInstance());

            if (currentSite.hasWeblink()) {
                weblinkView.setText(currentSite.getmWeblink());

                weblinkView.setVisibility(View.VISIBLE);
                weblinkView.setMovementMethod(LinkMovementMethod.getInstance());
            } else {
                weblinkView.setVisibility(View.GONE);
            }

            //used these logs to make sure the textviews were being updated correctly
            Log.d("addressView = ", addressView.getText().toString());
            Log.d("numberView = ", numberView.getText().toString());
            Log.d("gpsView = ", "" + gpsView.getText().toString());
            Log.d("weblinkView = ", weblinkView.getText().toString());

            //starts new activity that displays tourSite info
            Intent showInfo = new Intent(getContext(), TourSiteInfo.class);
            startActivity(showInfo);

        }
    });

    return rootView;
}

}

**This is the activity that is supposed to display the layout, but is blank

public class TourSiteInfo extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.list_item_info);
}
}

**This is the XML layout that is supposed to display

<LinearLayout
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:orientation="vertical"
android:padding="24dp"
android:id="@+id/infoLayout">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="123 Main Ave, Newark, New Jersey"
android:id="@+id/addressView"
    android:padding="16dp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="123 Main Ave, Newark, New Jersey"
    android:id="@+id/numberView"
    android:padding="16dp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="123 Main Ave, Newark, New Jersey"
    android:id="@+id/gpsView"
    android:padding="16dp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="123 Main Ave, Newark, New Jersey"
    android:id="@+id/weblinkView"
    android:padding="16dp"/>

</LinearLayout>

***This is the mainActivity java that contains the viewPager and tabLayout

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);

    CategoryAdapter adapter = new CategoryAdapter(this, getSupportFragmentManager());

    viewpager.setAdapter(adapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    tabLayout.setupWithViewPager(viewpager);
}
}

**the mainAcivity XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/mainLayout">

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

***LogCat when the app launches and list item is clicked

01/25 14:37:31: Launching app
D/ViewRootImpl: ViewPostImeInputStage processPointer 0
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
D/addressView =: ​503 Frank E Rodgers Blvd N, Harrison, NJ 07029
D/numberView =: ​(973) 482-4883
D/gpsView =: ​(correct google link displays but stack overflow won't allow it)
D/weblinkView =: ​ginaspizzanj.com
I/Timeline: Timeline:Activity_launch_requestid
:com.example.android.tourguide
time:222869939
D/SecWifiDisplayUtil: Metadata value : none
D/ViewRootImpl: #1 mView =
com.android.internal.policy.
PhoneWindow$DecorView{d9020d4 I.E......  R.....ID 0,0-0,0}
D/ViewRootImpl: MSG_RESIZED_REPORT: 
ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
W/DisplayListCanvas: DisplayListCanvas is started on unbinded
RenderNode (without mOwningView)
I/Timeline: Timeline: Activity_idle id:
android.os.BinderProxy@5ab3627 time:222870123
V/ActivityThread: updateVisibility : ActivityRecord{5e11572
token=android.os.BinderProxy@3d0da23
{com.example.android.tourguide/com.example.android.tourguide.MainActivity}} show : true

I've been researching and trying different solutions for days but nothing has worked yet.

joelc1225
  • 13
  • 1
  • 6
  • Whenever you launch an Activity it's like a new screen. You cannot set something for this view prior to launch. Although you can pass tourist parameters as intent bundle extra and then set those values in layout of activity. – Anurag Singh Jan 25 '17 at 20:00
  • Thanks Anurag!! Both you and christian helped me resolve me issue! – joelc1225 Jan 26 '17 at 01:47

1 Answers1

0

So, I think you need to do UI initialization in TourSiteInfo class. Inflating and setting values in onItemClick doesn't make sense. It will just create a new view and will be GCed soon after the creation, the view created there doesn't have any relation with the TourSiteInfo activity.

So here is what you need to do.

  1. Intent showInfo = new Intent(getContext(), TourSiteInfo.class);. After this you need to set all the site info to this intent as an extra value. You can refer this.

  2. Extract info from the intent in TourSiteInfo.onCreate and set values to controls. The code will be exactly the same as you did in onItemClick.

Community
  • 1
  • 1
christian
  • 445
  • 1
  • 3
  • 12
  • I'm going to try this after work. Thanks for the suggestion. I will update after I try. – joelc1225 Jan 26 '17 at 00:28
  • IT WORKED!! Thanks so much!! Glad you told me about bundles, because none of the solutions I searched mentioned them. Thank you so much! – joelc1225 Jan 26 '17 at 01:49