Listview in fragment crashes the app - android

I am trying to implete listview into fragment.But my app is crashing here is my fragment code
public class ConversationFragment extends Fragment {
public View rootView=null;
private ListView lv;
private ArrayList<String> strArr;
private ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String name = getArguments().getString("name");
rootView = inflater.inflate(R.layout.fragment_conversation, container, false);
TextView username=(TextView)rootView.findViewById(R.id.user_name);
username.setText(name);
strArr=new ArrayList<String>();
adapter=new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,strArr);
lv.setAdapter(adapter);
rootView.findViewById(R.id.get_from_user).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == R.id.get_from_user) {
getFromUser(v);
}
}
});
return rootView;
}
This is the fragment layout 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:background="#878787" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="dfgdfgdf"
android:textSize="20dp"
android:layout_centerInParent="true"
android:id="#+id/user_name"/>
<EditText
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/get_from_user"
android:text="Gönder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
/>
<ListView
android:id="#+id/message_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
And there is the logcat:http://prntscr.com/4a5svo
How can I fix it ? What do you suggest ?

lv value is not set. add code of findviewbyid on lv you should be good

initialize your Listview lv like you did for Textview and make sure your strArr Not empty
TextView username=(TextView)rootView.findViewById(R.id.user_name);

Related

How can I create List with dynamically number of items with Android Studio

I created list using example: Material design suggestions for lists with avatar, text and icon
I want to have list with upcoming movies, so I need list with dynamically number of items. I dont understand and I dont know how I should do it.
There is my code for one item list:
public class FragmentOne extends Fragment {
TextView textView;
TextView textView2;
ImageView imageView;
public static FragmentOne newInstance() {
FragmentOne fragment = new FragmentOne();
return fragment;
}
public class MovieTask extends AsyncTask<Void, Void, MovieDb>{
#Override
protected MovieDb doInBackground(Void... voids) {
TmdbMovies movies = new TmdbApi("my_api_key").getMovies();
MovieDb movie = movies.getMovie(5335, "en");
return movie;
}
#Override
protected void onPostExecute(MovieDb movieDb) {
textView.setText(movieDb.getOriginalTitle());
textView2.setText(movieDb.getReleaseDate());
Glide.with(imageView).load("https://image.tmdb.org/t/p/w500" + movieDb.getPosterPath()).into(imageView);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
MovieTask mt = new MovieTask();
mt.execute();
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View returnView = inflater.inflate(R.layout.fragment_one, container, false);
textView = (TextView) returnView.findViewById(R.id.text);
textView2 = (TextView) returnView.findViewById(R.id.text2);
imageView = (ImageView) returnView.findViewById(R.id.list_icon);
return returnView;
}
}
and xml for that class
<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="72dp"
tools:context=".FragmentOne">
<ImageView
android:id="#+id/list_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="16dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_toRightOf="#+id/list_icon"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true">
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="16dp" />
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="16dp" />
</LinearLayout>
</RelativeLayout>
I would personally suggest you to use Recyclerview and cardview to get this job done. You will need to create an adapter class for Recyclerview if you are not familiar with this.
Just check out this link -
https://www.journaldev.com/10024/android-recyclerview-android-cardview-example-tutorial

How tochange color of a textView in onclick listener in a list fragment

I was trying to change the color of a text view in a list fragment after onclick button.
Im using an add button(addBtn) to add the contents to list dynamically an another button(clrBtn) to change the color of the textview which is already added to the list
Im getting a nullPointerException. Can you please let me know where am i going wrong
Below is my code :
public class FragmentOne extends ListFragment {
String[] order = new String[] {};
int[] qty;
public FragmentOne(){}
/** Declaring an ArrayAdapter to set items to ListView */
SimpleAdapter adapter1;
// Keys used in Hashmap
String[] from = { "num","itm","price","qty" };
// Ids of views in listview_layout
int[] to = { R.id.num,R.id.itm,R.id.price,R.id.qty};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
Button addBtn,clrBtn;
rootView = inflater.inflate(R.layout.fragment_1, container, false);
addBtn = (Button) rootView.findViewById(R.id.btnAdd);
clrBtn = (Button) rootView.findViewById(R.id.btnClear);
addBtn.setOnClickListener (new OnClickListener() {
#Override
public void onClick(View v) {
//ListFragment fragment=new FragmentProdQtyPrice();
Fragment fragment=new FragmentExListView();
Bundle bundle = new Bundle();
bundle.putInt("fragmentId", 1);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
}
});
clrBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
/*MainActivity.aList.clear();
adapter1=new SimpleAdapter(getActivity().getBaseContext(), MainActivity.aList, R.layout.listview_layout, from, to);
setListAdapter(adapter1);*/
LinearLayout tv=(LinearLayout)arg0.findViewById(R.id.listtest);
tv.setBackgroundColor(Color.parseColor("#FF0000"));
}
});
adapter1=new SimpleAdapter(getActivity().getBaseContext(), MainActivity.aList, R.layout.listview_layout, from, to);
setListAdapter(adapter1);
return rootView;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
String prompt =
"clicked item: " + getListView().getItemAtPosition(position).toString();
Toast.makeText(getActivity(),prompt , Toast.LENGTH_SHORT).show();
}}
Edited:
My Layout files :
Fragment_1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="add" />
<Button
android:id="#+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clr" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/LinearLayout02" />
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/LinearLayout02"
android:gravity="center_horizontal"
android:text="#string/txtEmpty" />
</RelativeLayout>
listview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listtest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/itm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" />
</LinearLayout>
<TextView
android:id="#+id/qty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingTop="10dp" android:textSize="15sp" /></LinearLayout>
Try this Solution:
You are looking child view in Button element. Button element doesn't have any child element.
clrBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
LayoutInflater inflater = LayoutInflater.from(FragmentOne.this.getActivity().getApplicationContext());
View root = inflater.inflate(R.layout.listview_layout,null);
LinearLayout tv=(LinearLayout)root.findViewById(R.id.listtest);
tv.setBackgroundColor(Color.parseColor("#FF0000"));
}
});

FragmentTabHost not showing tab content

I have a problem on displaying child fragment(fragment in fragment) with FragmentTabHost in my program. The tab host is displayed perfectly but its content does not been shown...
First, introduce the classes:
Order.java:
public class Order extends Fragment{
private View view;
private FragmentTabHost orderMenu;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.order, container, false);
setLayout();
return view;
}
public void setLayout(){
orderMenu = (FragmentTabHost)view.findViewById(R.id.order_tabhost);
orderMenu.setup(getActivity(), getChildFragmentManager(), R.id.order_tabcontent);
Bundle testArg1 = new Bundle();
testArg1.putString("tag", "t1");
Bundle testArg2 = new Bundle();
testArg2.putString("tag", "t2");
Bundle testArg3 = new Bundle();
testArg3.putString("tag", "t3");
orderMenu.addTab(orderMenu.newTabSpec("t1").setIndicator("fruit"), OrderMenuList.class, testArg1);
orderMenu.addTab(orderMenu.newTabSpec("t2").setIndicator("bird"), OrderMenuList.class, testArg2);
orderMenu.addTab(orderMenu.newTabSpec("t3").setIndicator("meat"), OrderMenuList.class, testArg3);
}
}
order.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.support.v4.app.FragmentTabHost
android:id="#+id/order_tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<FrameLayout
android:id="#+id/order_tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
OrderMenuList.java
public class OrderMenuList extends Fragment{
private final String TAG = this.getClass().getName();
private View view;
private ListView menuList;
private String[] menu1 = {"apple", "orange", "banana", "melon"};
private String[] menu2 = {"duck", "chicken", "turkey"};
private String[] menu3 = {"steak", "pork"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.menu_list, container, false);
Log.d(TAG, ""+this.getArguments().getString("tag"));
setLayout();
return view;
}
public void setLayout(){
menuList = (ListView)view.findViewById(R.id.menu_list);
ArrayAdapter<String> adapter = null;
if (this.getArguments()!=null){
if (this.getArguments().getString("tag").equals("t1")){
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menu1);
} else if (this.getArguments().getString("tag").equals("t2")){
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menu2);
} else if (this.getArguments().getString("tag").equals("t3")){
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menu3);
}
}
menuList.setAdapter(adapter);
Log.d(TAG, ""+adapter.getCount());
}
}
menu_list.xml:
<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/menu_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
I have also tried simply put a textview instead of listview to test it but it also does not work. So, for now stage, my aim is very simple........show the tab content in the fragment!!!!!!
You have FrameLayout with android:id="#android:id/tabcontent" and android:layout_height="match_parent", so it occupies all available height. But you setup your FragmentTabHost with R.id.order_tabcontent which resides below, so you don't see it.
Remove one of FrameLayout's and setup FragmentTabHost with another.

How to put a ListView in a Fragment

I had a good ListView, but now I added Fragments, now my Code doesn't work in the onCreateMenu...
How to make my ListView back?
So here's my code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView;
Bundle b = getArguments();
int Nr = b.getInt(ARG_SECTION_NUMBER);
if (Nr == 0) {
myFragmentView = inflater.inflate(R.layout.layoutexample, container, false);
} else {
myFragmentView = inflater.inflate(R.layout.another_layout_example, container, false);
}
return myFragmentView;
}
I don't understand, where to put my code (for example if I want a ListView)!
Can I use something similar to the inflater?
Please help, as a beginner I only understand half of the information, Google Developers gives me...
This is my ListView:
public class Home extends ListActivity {
ListView listView;
static final String[] Liste = {"a", "b", "c"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("CalcPlus");
listView = getListView();
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, Liste) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
String item = getItem(position);
TextView subTitleView = (TextView) view.findViewById(R.id.subtitle);
subTitleView.setText("Subtitle of " + item);
return view;
}
});
}
}
That's the list_item:
<?xml version="1.0" encoding="utf-8"?>
<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="10dp" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text=""
android:textAllCaps="false"
android:textSize="16dp"
android:textStyle="normal" />
</LinearLayout>
Here's the example.xml (they both look the same):
<?xml version="1.0" encoding="utf-8"?>
<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" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
After having inflated your view, you can add :
ListView listView = myFragmentView.findViewById(android.R.id.list);
listView.setAdapter(....);
Edit : I didn't know the id you set to your ListView. Replace android.R.id.list by R.id.listView1

Fragment overlaying an Activity layout

I'm having problems working with fragments. The problem is: I have a layout in my activity with a ListView. When i select an item in this ListView, instantiates a fragment replacing the activity layout. My result is both fragment and activity contents are showing on screen, overlaying each other. What can I fix that (the fragment just replacing the activity content)? Here is the codes:
activityLayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity"
android:orientation="vertical">
<FrameLayout android:id="#+id/fragment_to_replace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:orientation="vertical"
android:layout_gravity="center" >
<TextView android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/textView_select_table"
android:textSize="30dp"/>
<ListView android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="#+id/listView"
android:visibility="visible"
android:paddingTop="40dp" />
</FrameLayout>
</LinearLayout>
Activity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
View view = findViewById(R.id.activity);
//Getting the ListView
ListView listView = (ListView) view.findViewById(R.id.listView);
listView.setTextFilterEnabled(true);
//Setting the adapter
listView.setAdapter(new Adapter(this));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Creates a fragment
addFragment();
}
});
}
public void addFragment(){
MyFragment newFragment;
FragmentTransaction newFragmentTransaction = getSupportFragmentManager().beginTransaction();
newFragment = (MyFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment);
if (newFragment == null){
newFragment = new MyFragment(this, getSupportFragmentManager());
newFragmentTransaction.replace(R.id.fragment_to_replace,
new SelectDrinkFragment(this, getSupportFragmentManager()));
newFragmentTransaction.addToBackStack(null);
newFragmentTransaction.commit();
}
}
fragmentLayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/textView_two"
android:textSize="20dp"/>
<LinearLayout android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/textView" />
<TextView android:id="#+id/textView_two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"/>
</LinearLayout>
<TextView
android:id="#+id/textView_three"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:text="#string/textView_three"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/listView_two"
android:visibility="visible"
android:paddingTop="40dp" />
</LinearLayout>
Fragment.java
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle saveInstanceState){
View view = inflater.inflate(R.layout.fragment, container, false);
final ListView listView = (ListView) view.findViewById(R.id.listView_two);
CharSequence [] list = getResources().getStringArray(R.array.array);
listView.setAdapter(new ListAdapter(getContext(), list));
return view;
}
ListAdapter.java
public class ListAdapter extends BaseAdapter{
private Context context;
private CharSequence[] drinkArraySize;
public ListAdapter(Context context, CharSequence[] array){
super();
this.context = context;
this.array= array;
}
#Override
public int getCount() {
return array.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
public Context getContext(){
return context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout linearLayout = new LinearLayout(getContext());
RadioButton radioButton = new RadioButton(getContext());
radioButton.setText(array[position]);
linearLayout.addView(radioButton);
return linearLayout;
}
}
FragmentTransaction.replace() looks like it only replaces another fragment (and not just any View), so I'm pretty sure this is expected behavior (or at least, I know I've had this happen to me before). I suppose you have a few options:
I don't see in your code anything that would make the fragment's background opaque. You could try that first and see if it helps. If it does, that might be the simplest.
You could just set the other items' visibilities to View.GONE at the same time you perform your transaction, but that has some issues as far as backstack goes.
You could replace the ListView and TextView with a ListFragment instead. This is probably the "best" way to do it, but also is probably the most work.

Categories

Resources