I am using Google Place API to find nearest places. I am displaying map and list on two tabs. I have made two fragments. In one i am showing map and in second one i am using list. I have used an editext box on actionbar. When i am running my app it is crashing and giving nullpointer exception at setListAdapter.
It is my ListFragment file
import android.support.v4.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.aquib.android.place.Place;
import com.aquib.android.place.PlaceAdapter;
import com.aquib.android.place.R;
import java.util.List;
/**
* Created by dell on 2/9/2015.
*/
public class ListItemFragment extends ListFragment{
private PlaceAdapter adapter;
List<Place> placeList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, null);
TextView emptyTextView = (TextView) view.findViewById(android.R.id.empty);
emptyTextView.setTextSize(10);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
adapter = new PlaceAdapter(getActivity(), placeList);
setListAdapter(adapter);
}
}
and my fragment_list.xml file
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list" />
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
When i will give input in edittext box and call place api and then parse json data. I have written Place class and then add place object in list. so please someone tell me that how to display empty text. app is showing exception in arrayadapter.getCount() method.
Your placeList is never initialized.
Moreover, in onCreateView(), you should probably create your view like this:
View view = inflater.inflate(R.layout.fragment_list, container, false);
Related
I know that there were a lot of questions like this one, but I haven't been able to find the answer I'm looking for...
I'd like to dynamically create some TextView in an already existing ScrollView. I've done something that I thought would have been okay, but the app stops when it comes to this fragment?
I can't put the whole project because the files are numerous, but here are the concerned .java and .xml:
NotesPageFragment.java
package com.a3m.Controllers.Fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.a3m.Controllers.ui.Controler;
import com.a3m.Controllers.core.Task;
import com.a3m.R;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
public class NotesPageFragment extends Fragment {
private Controler controler;
private ScrollView mScrollView;
public static NotesPageFragment newInstance() {
return(new NotesPageFragment());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_notes_page, container, false);
this.controler = Controler.getInstance();
this.mScrollView = v.findViewById(R.id.fragment_page_notes_scrollview);
//addNotes(notes,getAllTasks());
addNotes(v, getAllTasks());
return v;
}
public ArrayList<Task> getAllTasks()
{
/*
** ici le code qui se connecte à la bdd et retourne toutes les tasks disponibles
*/
ArrayList<Task> tasks = new ArrayList<>();
ArrayList<String> n =new ArrayList<>();
n.add("detail1");
n.add("detail2");
n.add("detail3");
Task t1=new Task(0,0,null,"t1",0,0,null,0,null,null,n,null);
Task t2=new Task(0,0,null,"t2",0,0,null,0,null,null,n,null);
Task t3=new Task(0,0,null,"t3",0,0,null,0,null,null,n,null);
Task t4=new Task(0,0,null,"t4",0,0,null,0,null,null,n,null);
Task t5=new Task(0,0,null,"t5",0,0,null,0,null,null,n,null);
Task t6=new Task(0,0,null,"t6",0,0,null,0,null,null,n,null);
Task t7=new Task(0,0,null,"t7",0,0,null,0,null,null,n,null);
tasks.add(t1);
tasks.add(t2);
tasks.add(t3);
tasks.add(t4);
tasks.add(t5);
tasks.add(t6);
tasks.add(t7);
return tasks;
}
public void addNotes(View view, ArrayList<Task> tasks) {
Iterator<Task> itr_tasks = tasks.iterator();
Task task;
String taskNote;
while(itr_tasks.hasNext()) {
taskNote = "";
task = itr_tasks.next();
taskNote += task.getName();
Iterator<String> itr_notes = task.getNotes().iterator();
while(itr_notes.hasNext()) {
taskNote += "\t\t\t" + itr_notes.next() + "\t\t\t";
}
final TextView taskNotes = new TextView(getActivity());
taskNotes.setText(taskNote);
mScrollView.addView(taskNotes);
}
}
}
fragment_notes_page.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:id="#+id/fragment_page_news_rootview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EDD9CF"
android:contentDescription="NotesPage"
android:gravity="center"
tools:context="com.a3m.Controllers.Fragments.NotesPageFragment">
<ScrollView
android:id="#+id/fragment_page_notes_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/nav_header_marginLeft">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="171dp"
android:src="#android:drawable/ic_menu_info_details" />
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="65dp"
android:text="Notes"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="30sp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</ScrollView>
</LinearLayout>
I'm aware that the IDs in the .xml aren't really good, but I'll improve them later...
If someone has an idea or can show me were the answer is, I'll gladly accept this help!!
ScrollView and all it's relatives that scroll the content (i.e. NestedScrollView) allow only for one child. You cannot add new views directly to ScrollView if it already has one child view.
Quote from java doc to ScrollView class:
/**
* A view group that allows the view hierarchy placed within it to be scrolled.
* Scroll view may have only one direct child placed within it.
* To add multiple views within the scroll view, make
* the direct child you add a view group, for example {#link LinearLayout}, and
* place additional views within that LinearLayout.
...
*/
And if you look inside of the class you will find that it overrides all addView methods:
To fix the issue what you need to do is to add id to your LinearLayout and insert new views into it.
I'd recommend changing it all to RecyclerView + Adapter.
Official tutorials from Google on how to use RecyclerView and Adapters
i have one list view with two text views inside it, one edit text that is in the same activity but not in the list view and two buttons one to add to the list view and the other to delete from it.
how to add integers to the first text view, the sum of all integers to the second one, and to be from a custom adapter.
thank you.
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"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="#+id/edit_ten"
android:hint="Score"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn_add"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Add"/>
<Button
android:id="#+id/btn_delete"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Undo"/>
</LinearLayout>
<ListView
android:id="#+id/list_sinhvien"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
item_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="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_ten"
android:textSize="20dp"
android:text="Score"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text_sdt"
android:textSize="20dp"
android:text="Total"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.addanddelete;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ListView listSinhvien;
EditText editTen;
Button btnThem , btnSua;
ArrayList<Sinhvien> arraySinhvien;
CustomAdapter myadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
anhxa();
arraySinhvien = new ArrayList<Sinhvien>();
myadapter = new CustomAdapter(this , R.layout.item_layout,arraySinhvien);
listSinhvien.setAdapter(myadapter);
btnSua.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final int count = myadapter.getCount();
myadapter.remove(myadapter.getItem(count -1));
myadapter.notifyDataSetChanged();
return;}});
}
private void anhxa(){
listSinhvien = (ListView)findViewById(R.id.list_sinhvien);
editTen = (EditText)findViewById(R.id.edit_ten);
btnThem = (Button)findViewById(R.id.btn_add);
btnSua = (Button)findViewById(R.id.btn_undo);
btnThem.setOnClickListener(this);
btnSua.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_add:
Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
String ten = editTen.getText().toString();
String sdt = editTen.getText().toString();
Sinhvien temp = new Sinhvien(R.mipmap.ic_launcher,ten , sdt);
arraySinhvien.add(temp);
myadapter.notifyDataSetChanged();
break;
}
}
}
CustomAdapter.java
package com.example.addanddelete;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class CustomAdapter extends ArrayAdapter {
Activity activity;
int layout;
ArrayList<Sinhvien> arrSinhVien;
public CustomAdapter(#NonNull Activity activity, int layout, #NonNull ArrayList<Sinhvien> arrSinhVien) {
super(activity, layout, arrSinhVien);
this.activity = activity;
this.layout = layout;
this.arrSinhVien = arrSinhVien;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = activity.getLayoutInflater();
convertView = layoutInflater.inflate(layout, null);
TextView ten = (TextView) convertView.findViewById(R.id.text_score);
TextView sdt = (TextView) convertView.findViewById(R.id.text_total);
ten.setText(arrSinhVien.get(position).getTenSinhvien());
sdt.setText(arrSinhVien.get(position).getSdtSinhvien());
return convertView;
}
}
Sinhvien.java
package com.example.addanddelete;
public class Sinhvien {
String tenSinhvien;
String sdtSinhvien;
public Sinhvien(String iclauncher,String ten, String sdt) {
}
public Sinhvien(int iclauncher,String tenSinhvien, String sdtSinhvien) {
this.tenSinhvien = tenSinhvien;
this.sdtSinhvien = sdtSinhvien;
}
public String getTenSinhvien() {
return tenSinhvien;
}
public void setTenSinhvien(String tenSinhvien) {
this.tenSinhvien = tenSinhvien;
}
public String getSdtSinhvien() {
return sdtSinhvien;
}
public void setSdtSinhvien(String sdtSinhvien) {
this.sdtSinhvien = sdtSinhvien;
}
}
The general behaviour/implementation that you've outlined in your question is very well documented. That said, I'd suggest considering utilising a RecyclerView with associated custom item view and adapter, as opposed to a ListView. There are a few reasons why I'd suggest this.
I did a little searching and found several examples that cover the general idea of your implementation. This example does a great job of illustrating how to achieve what you're seeking. The article begins by outlining some reasons to work with a RecyclerView over a ListView or GridView, then proceeds to give an in-depth run-down on how to implement a RecyclerView with custom adapter (and associated item view and item class).
At a glance, your implementation would require:
An Activity containing your RecyclerView, two Buttons (used for adding and deleting elements from the RecyclerView) and an EditText for taking user input.
A custom item View representing individual items of your RecyclerView list. This would contain the two TextView views (one for displaying the integer and the other for displaying the sum of all integers).
A custom item model Class to represent the data model for the above custom item View. This would hold an integer value and likely some logic for displaying the sum.
A custom RecyclerView adapter (which ties all of the above together). This will need to handle the task of binding data from your dataset (that grows and shrinks based on user input) to instances of your custom items that are to appear in the RecyclerView list. This adapter could also be used by your add and delete item buttons to modify the elements in the RecyclerView list.
The above is outlined in far greater depth in the link I provided earlier.
I sincerely hope that helps!
I am testing making a Tabbed Layout with Different Content each Tab.
In this Tab i want make layout with Image and Text inside Listview.
This is code i wrote so far but not working.
Layout_One.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"
tools:context="com.example.kreuzell.projecttest.MainActivity$PlaceholderFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/menuText"/>
</RelativeLayout>
Layout_One.java
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ImageView;
import java.util.List;
public class Layout_One extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_one, container, false);
String[] menuText = {
"Text 1",
"Text 2",
"Text 3"
};
Integer[] menuImage = {
R.drawable.image_1,
R.drawable.image_2,
R.drawable.image_3
}
ListView listView = (ListView) rootView.findViewById(R.id.menuText);
ListView listView = (ListView) rootView.findViewById(R.id.menuImage);
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
getActivity();
android.R.layout.simple_list_item_1,
menuText,
menuImage
)
listView.setAdapter(listViewAdapter);
return rootView;
}
}
I am new of this, Anybody can help me? Thank you
Use adapter for the list view and edit the layout of the adapter
You need to make a custom layout(layout1) which has your image and text positioned as you want. Then make a custom list adapter(mAdapter), use layout1 inside your mAdapter and fill it with data.Then set mAdapter to the list.
Please have a look at these tutorials, they will help you to understand how all this custom lists work:
Vogella List Tutorial
Journal Dev List Tutorial
i know i can make textviews clickable so i can let the user call a number with an intent. but how do i achieve this when inside a fragment. i have multiple fragments and telephone numbers in my string.xml. every fragment has a telephone number, for example the code for one of my fragments:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Simmering extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.simmering_layout, container, false);
}
}
and here the xml for this class (fragment):
<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"
tools:context="at.co.ccc.mondel.Simmering" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="#string/add_sim" />
<TextView
android:id="#+id/call_sim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:clickable="true"
android:text="#string/tel_sim" />
</FrameLayout>
please help me, thank you!
I am assuming you are having trouble finding the Text View in that case you can try the following way.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Simmering extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.simmering_layout, container, false);
// Here you can access the edit text like this
mTextView = (EditText) rootView.findViewById(R.id.call_sim);
return rootView;
}
}
I'm trying to get my head around fragments in order to have my apps prepped for ICS.
I have the following files to just get the most basic fragment app you can have. It should have this when launched: One Fragment Layout with a text view "Fragment 1" and next to it another Fragment Layout with "Fragment2".
My package name is com.mwerner.fragments
My files are:
FragmentsActivity.java
ExamplesFragment.java
ExamplesFragment2.java
examples_fragment.xml
examples_fragment2.xml
main.xml
The code for FragmentsActivity.java is:
package com.mwerner.fragments;
import android.app.Activity;
import android.os.Bundle;
public class FragmentsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
ExamplesFragment.java
package com.mwerner.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExamplesFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.examples_fragment, container, false);
}
}
ExamplesFragment2.java
package com.mwerner.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ExamplesFragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.examples_fragment2, container, false);
}
}
The examples_fragment.xml files just have a linear layout with a textview in it...
Here is the code for the main.xml:
<?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" >
<fragment
class="com.mwerner.fragments$ExamplesFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
/>
<fragment
class="com.mwerner.fragments$ExamplesFragment2"
android:id="#+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="fill_parent" />
</LinearLayout>
The app crashes on startup with the error
11-07 18:12:12.519: E/AndroidRuntime(696): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mwerner.fragments/com.mwerner.fragments.FragmentsActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
Can you please tell me what is wrong here? I pretty much copied / pasted the code from the google developers page for fragments.
You defined the path to your fragments incorrectly in your layout xml. Correct the class attributes. Try 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="fill_parent"
android:orientation="vertical" >
<fragment
class="com.mwerner.fragments.ExamplesFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
/>
<fragment
class="com.mwerner.fragments.ExamplesFragment2"
android:id="#+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="fill_parent" />
</LinearLayout>
Try extending FragmentActivity
public class FragmentsActivity extends FragmentActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
When I was writing my first program on Fragments using I incurred same error. Instead of extending "Activity" extend "FragmentActivity" in your launcher activity.