I am trying to create a custom ListView but i can not even compile it, since it gives me this error early on:
Error:(25, 63) error: cannot find symbol variable listing
where "listing" is the xml file name for my ListView layout.
What am i doing wrong?
This is my fragment code:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class ListingFragment extends Fragment {
private CustomcursorAdapter mCustomcursorAdapter;
private ListView mListView;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_listing, container, false);
//CursorAdapter
mCustomcursorAdapter = new CustomcursorAdapter(view.getContext(), null, 0);
//ListView
mListView = (ListView) view.findViewById(R.id.listing);
mListView.setAdapter(mCustomcursorAdapter);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
fragment_listing
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
MainActivity and activity_main
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager.findFragmentById(android.R.id.content) == null) {
Listing listing = new Listing();
fragmentManager.beginTransaction().add(android.R.id.content, listing).commit();
}
}
}
activity_main
<RelativeLayout 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"
tools:context="${relativePackage}.${activityClass}">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
In your fragment_listing.xml you should add:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#android:color/transparent" >
</ListView>
</RelativeLayout>
It didnt compile because you didnt have that id in R.java
Edit
In your activty_main xml add:
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Then in your MainActivity add:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment fragment = new Listing();
transaction.replace(R.id.fragmentContainer,fragment);
ft.addToBackStack(null);
ft.commit();
Related
I am unable to remove a present fragment in my second fragment while trying to replace it from the first fragment along with trying to open the second fragment from the first fragment on a click of a button.
Here is my code
FirstFragment.java
package com.example.testanderase;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.List;
import java.util.Objects;
public class FirstFragment extends Fragment implements View.OnClickListener{
TextInputEditText inputEditText;
Button enterButton;
String getMessage;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.first_fragment,container,false);
inputEditText = view.findViewById(R.id.enter_edit_txt_input);
enterButton = view.findViewById(R.id.first_fragment_btn);
enterButton.setOnClickListener(this);
return view;
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public void onClick(View v) {
getMessage = inputEditText.getText().toString();
SecondFragment secondFragment = new SecondFragment();
Bundle b = new Bundle();
b.putString("data",getMessage);
secondFragment.setArguments(b);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.second_fragment_layout,secondFragment);
//transaction.add(R.id.second_fragment_layout,secondFragment);
transaction.commit();
***Below here I am trying to change the code
***In order to open the second fragment from the first fragment
/assert getFragmentManager() != null;
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.second_fragment_layout,secondFragment).commit();/
//fm.beginTransaction().replace(SecondFragment.newInstance());
}
}
first_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:id="#+id/first_fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp">
<android.support.design.widget.TextInputEditText
android:id="#+id/enter_edit_txt_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/input"
android:textAlignment="center"
android:gravity="center_horizontal" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/first_fragment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/enter"
android:background="#color/black"
android:textColor="#color/white"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
</LinearLayout>
</LinearLayout>
SecondFragment.java
package com.example.testanderase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SecondFragment extends Fragment implements View.OnClickListener{
TextView textView;
Button enterButton;
String firstFragmentValue;
Toast t;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.second_fragment,container,false);
textView = view.findViewById(R.id.second_fragment_txt_view);
enterButton = view.findViewById(R.id.second_fragment_btn);
enterButton.setOnClickListener(this);
//assert getArguments() != null;
//firstFragmentValue = getArguments().getString("data");
//textView.setText(firstFragmentValue);
Bundle bundle = getArguments();
if(bundle != null)
{
String name = bundle.getString("data");
textView.setText(name);
Toast.makeText(getContext(),"passed data "+name,Toast.LENGTH_SHORT).show();
}
//textView.setText(firstFragmentValue);
return view;
}
#Override
public void onClick(View v) {
//
ToastButton();
/*assert getFragmentManager() != null;
FirstFragment firstFragment = new FirstFragment();
FragmentManager fragmentTransaction = getFragmentManager();
fragmentTransaction.beginTransaction().replace(R.id.first_fragment_layout, firstFragment).commit();*/
}
public void ToastButton()
{
if(t!=null)
{
t.cancel();
}
else
{
Toast.makeText(getContext(),"Clicked",Toast.LENGTH_SHORT).show();
}
}
}
second_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/second_fragment_txt_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:text="#string/change_text"
android:textAlignment="center"
android:gravity="center_horizontal"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
<Button
android:id="#+id/second_fragment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/back_button_text"
android:background="#color/black"
android:textColor="#color/white"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
</LinearLayout>
SectionPageAdapter.java
package com.example.testanderase;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class SectionPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentListTitle = new ArrayList<>();
public void addFragment(Fragment fragment, String title)
{
fragmentList.add(fragment);
fragmentListTitle.add(title);
}
public SectionPageAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return fragmentListTitle.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
}
MainActivity.java
package com.example.testanderase;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity{
private SectionPageAdapter mSectionPageAdapter;
private ViewPager mainViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionPageAdapter = new SectionPageAdapter(getSupportFragmentManager());
mainViewPager = findViewById(R.id.container);
setMainViewPager(mainViewPager);
TabLayout tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mainViewPager);
}
public void setMainViewPager(ViewPager viewPager)
{
SectionPageAdapter adapter = new SectionPageAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstFragment(),"FIRST");
adapter.addFragment(new SecondFragment(),"SECOND");
viewPager.setAdapter(adapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity"
android:id="#+id/main_layout"
android:layout_margin="20dp">
<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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
where is R.id.second_fragment_layout
anyway change your second_fragment.xml to this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:id="#+id/second_fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/second_fragment_txt_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:text="hello"
android:textAlignment="center"
android:gravity="center_horizontal"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
<Button
android:id="#+id/second_fragment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="back"
android:background="#000"
android:textColor="#fff"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
/>
</LinearLayout>
</LinearLayout>
and to open the second fragment from the first fragment on a click of a button
first make mainViewPager static
static ViewPager mainViewPager
put this on first fragment onclick
int page =1;
mainViewPager.setCurrentItem(page);
to back to first fragment
put this on second fragment onclick
int page =0;
mainViewPager.setCurrentItem(page);
ts working for me.!
I got a problem. When im using a pagerView everything inside is showing up beside ImageView.
I'm using this :
<!-- activity_screen_slide.xml -->
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:id="#+id/relativeLayout">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="0dp" />
</RelativeLayout>
and this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/common_google_signin_btn_icon_dark" />
</LinearLayout>
And i use this for fragment
package com.example.gebruiker.drumio.SlideScreenTrackPreview;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.example.gebruiker.drumio.R;
public class TestFragment extends Fragment {
ImageView hihat;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup)
inflater.inflate(R.layout.track_preview_test,container,false);
hihat =(ImageView)rootView.findViewById(R.id.imageView2);
hihat.setBackgroundColor(Color.BLACK);
hihat.setColorFilter(Color.BLACK);
return rootView;
}
}
Except the button, nothing is showing up.
Here is the code for the activity that i use :
package com.example.gebruiker.drumio.SlideScreenTrackPreview;
public class ScreenSliderActivity extends FragmentActivity {
private static final int NUM_PAGES = 5;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setBackgroundColor(Color.WHITE);
mPagerAdapter = new
ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new TestFragment();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
Your activity works for me if you inherit from AppCompatActivity instead of from FragmentActivity. I don't know why; maybe somebody with more experience can elucidate.
In my project I have inserted "Two" Fragments on my MainActivity:
And on "leftside" fragment I want to load Listview and "Rightside" fragment I want to load just one empty view.
For this, I have tried the code below, but I it's showing an exception:
ClassCaste Exception
What did I do wrong here?
activity_main.xml:
<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:padding="10dp">
<fragment
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
class="com.example.ram_ramadevi.fragmentexample4.MenuFragment"
android:id="#+id/fragment1"
/>
<fragment
android:layout_marginLeft="10dp"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.example.ram_ramadevi.fragmentexample4.RightSideFragment"
android:id="#+id/fragment2"/>
</LinearLayout>
MainActivity:
package com.example.ram_ramadevi.fragmentexample4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MenuList.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
MenuFragment:
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MenuFragment extends Activity {
ListView list;
ArrayAdapter<String> adaapter;
String[] android_Versions = {"1","2","3","4","5","6","7","8","9","10"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MenuList);
list = (ListView)findViewById(R.id.ListView);
adaapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android_Versions);
list.setAdapter(adaapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + "is selected", Toast.LENGTH_SHORT).show();
}
});
}
}
RightsideFragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ffff"
>
</LinearLayout>
RightSideFragment:
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RightSideFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.RightsideFragment, container, false);
}
}
In menu fragment change Fragment instead of Activity
public class MenuFragment extends Fragment{
//your logic
}
Try below code:
activity_main.xml
<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:weightSum="2"
tools:context="com.example.newfragment.MainActivity" >
<fragment
android:id="#+id/fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.example.newfragment.MenuFragment" />
<fragment
android:id="#+id/fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
class="com.example.newfragment.RightSideFragment" />
</LinearLayout>
MainActivity:-
package com.example.newfragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
menulist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
MenuFragment:-
package com.example.newfragment;
import android.app.Fragment;</n>
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MenuFragment extends Fragment {
View view;
ListView list;
ArrayAdapter<String> adaapter;
String[] android_Versions = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.menulist, container, false);
list = (ListView) view.findViewById(R.id.ListView);
adaapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android_Versions);
list.setAdapter(adaapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(getActivity(), parent.getItemAtPosition(position) + "is selected", Toast.LENGTH_SHORT)
.show();
}
});
return view;
}
}
rightsidefragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ffff">
</LinearLayout>
RightSideFragment:-
package com.example.newfragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RightSideFragment extends Fragment{
View rightView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
rightView=inflater.inflate(R.layout.rightsidefragment, container, false);
return rightView;
}
}
I have the Problem, that I get a NullPointExeption when I want to set a adapter on my ListView.
Before I had the Fragment extended with ListFragment and a simple Adapter, that works but the problem was, that I have 3 Fragments in this activity all with ListViews and I got display errors (shows the wrong list in a fragment). So I decided to set for every Fragment own ids on the Listview but now it doesnt work.
Error listview.setAdapter(adapter):
java.lang.NullPointerException at
de.resper.e2cast.MainFragmentLive.onCreateView(MainFragmentLive.java:46)
Fragment:
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import de.resper.e2cast.classes.globalBox;
import de.resper.e2cast.helper.getXml;
import de.resper.e2cast.helper.parseXml;
public class MainFragmentLive extends android.support.v4.app.Fragment {
private List<String> bouquetListString;
private ArrayAdapter<String> adapter;
private globalBox activeBox;
private ListView listview;
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_live, container, false);
activeBox = ((globalBox) getActivity().getApplicationContext());
bouquetListString = new ArrayList<String>();
bouquetListString.add("loading...");
if(activeBox.isInit()){
if(activeBox.getBouquets().size() > 0 && activeBox.getBouquets().get(2).size() > 0){
bouquetListString = activeBox.getBouquets().get(2);
}else{
Log.d("Load Bouquet", "XML");
getBouquetBox();
}
}
listview = (ListView) getActivity().findViewById(R.id.listLive);
adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, bouquetListString);
listview.setAdapter(adapter);
ImageButton reloadBouquet = (ImageButton) view.findViewById(R.id.reloadBouquet);
reloadBouquet.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
getBouquetBox();
}
});
setHasOptionsMenu(true);
return view;
}
public void getBouquetBox(){
getXml.DownloadCompleteListener dcl = new getXml.DownloadCompleteListener() {
#Override
public void onDownloadComplete(String result) {
bouquetListString.clear();
String [] tags = {"e2servicereference", "e2servicename"};
List<List<String>> bouquetsList = parseXml.parseXmlByTag(result, tags);
activeBox.addBouquets(bouquetsList);
bouquetListString.addAll(activeBox.getBouquets().get(2));
adapter.notifyDataSetChanged();
}
};
Log.d("MyLogger", "XML Request GET BOUQUET");
getXml downloader = new getXml(dcl);
downloader.execute("http://" + activeBox.getIpPort() + "/web/getservices");
}
}
Fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:text="#string/selectBouquet"
style="#style/header1"/>
<ImageButton
android:layout_width="0dip"
android:layout_height="wrap_content"
android:id="#+id/reloadBouquet"
android:src="#drawable/ic_action_refresh"
android:contentDescription="#string/search"
android:layout_weight=".20"
android:layout_gravity="bottom"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listLive" />
</LinearLayout>
Use view instead of getActivity() to initializing ListView because ListView is inside Fragment layout instead of Activity :
listview = (ListView) view.findViewById(R.id.listLive);
I have a big request for any of You. I would like to create a third Fragment in my simple application. Everything works fine, no error. I would like to make the last Fragment it is: "TestFragment" when I click that button inside that Fragment I'd like to refresh its View (on click button) and load another view inside this fragment (the same fragment). Please help me do this, show how to get it. I have read in Google but can not find the solution appropriate for this application.
Here is simple code:
MAIN ACTIVITY:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
private MyAdapter mAdapter;
private ViewPager mPager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 2;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new TestFragment();
// case 1:
// return new ImageFragment();
case 1:
return new TestFragment();
//
default:
return null;
}
}
}
}
TEST FRAGMENT:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TestFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test, container, false);
// TextView textView = (TextView) view.findViewById(R.id.detailsText);
// textView.setText("Testing");
return view;
}
}
TEST LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QUIZ IMPORTANT TAKE A LOOK AT THIS!!!!!"
android:textSize="30dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginLeft="30dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdjiasdjasoldjasjdasjdlasjdksahfkadshfalidshfladshfladsfhlasdfgjl;adkglkafdgnklfdsgjpfasmga.dsnvlkasdnvkldsnflkdasjflkasdnvlkadjvodsijfosdjv;lasdvlasdjvladosijao" />
DETAILS FRAGMENT:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Details extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
TextView textView = (TextView) view.findViewById(R.id.detailsText);
textView.setText("Testing");
return view;
}
}
ACTIVITY_MAIN LAYOUT:
<?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="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
DETAILS LYOUT:
<?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="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
How to do this refresh inside the same fragment?