I have one fragment contain list view, buttons and images.Buttons,images and other widget loaded correctly but instead of list view it shows only loading icon
public class ListDetails extends SherlockFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
ListView listView = (ListView)container.findViewById(R.id.list_view2);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
return inflater.inflate(
R.layout.listdetails_fragement, container, false);
}
I have check other Button,Images are Loaded but List view gives error on my ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1, android.R.id.text1, values); line
here i provided my logcat error.
Here's what I did:
Instead of doing everything in onCreateView(), I used onActivityCreated().
In onActivityCreated(), I just added this code:Here's the implementation:
public class Database extends Fragment {
public ViewPager viewPager;
private AllPagesAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "Tab1", "Tab2" };
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
ArrayAdapter<String> mArrayAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View databaseview = inflater.inflate(R.layout.database, container,
false);
return databaseview;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
ListView mListView = (ListView)getActivity().findViewById(R.id.databaselist);
mArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, values);
mListView.setAdapter(mArrayAdapter);
}
}
And the layout I used was :
<?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" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<ListView
android:id="#+id/databaselist"
android:layout_width="match_parent"
android:layout_height="254dp" >
</ListView>
<TextView
android:id="#+id/newmainlist"
android:layout_width="match_parent"
android:layout_height="250dp"
android:gravity="center"
android:text="database" />
</LinearLayout>
Also, here's the image for it:
Hope this helps .. :)
The error because of this line..
ListView listView = (ListView)container.findViewById(R.id.list_view2);
here listview is null
change your code like this..
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.listdetails_fragement, container,
false);
ListView listView = (ListView) view.findViewById(R.id.list_view2);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation", "Simple List View In Android",
"Create List View Android", "Android Example",
"List View Source Code", "List View Array Adapter",
"Android Example List View" };
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity()
.getApplicationContext(), android.R.layout.simple_list_item_1,
android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
return view;
}
Change onCreateView(....) with this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v=inflater.inflate(
R.layout.listdetails_fragement, container, false);
ListView listView = (ListView)v.findViewById(R.id.list_view2);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
return v;
}
Related
I am trying to have a checkable list view fragment pop up with a button at the top of the list view. After user clicks an item in the navigation menu, the main activity will be populated with my button and list view fragment. I'm getting 'invoke virtual method' error, even though the element in declared. Here's some code.
My list fragment class:
public class ThingsManager extends Fragment {
ArrayList<String> selectedItems;
ListView checkable_list;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(layout.things_manager_fragment, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//create an ArrayList object to store selected items
selectedItems = new ArrayList<String>();
//create an instance of ListView
checkable_list.findViewById(R.id.checkable_list);
//set multiple selection mode
checkable_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
String[] things_factory = {"Sports", "Politics", "Food", "Television", "Movies", "Fashion",
"Theoretical Physics"};
//supply data itmes to ListView
//ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.row, R.id.things_check, things_factory);
ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, things_factory);
checkable_list.setAdapter(aa);
//set OnItemClickListener
checkable_list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// selected item
String selectedItem = ((TextView) view).getText().toString();
if(selectedItems.contains(selectedItem))
selectedItems.remove(selectedItem); //remove deselected item from the list of selected items
else
selectedItems.add(selectedItem); //add selected item to the list of selected items
}
});
}
public ThingsManager() {
// Required empty public constructor
}
}
List fragment layout:
<?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"
tools:context=".ThingsManager">
<TextView
android:id="#+id/things_manager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Things Manager"
android:textSize="25dp"/>
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="#+id/checkable_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
Should I be controlling the listview in MainActivity or in the FragmentClass ?
Move your all your view initialization and listview setup code from onActivityCreated to onCreateView method.
And your this statement
//create an instance of ListView
checkable_list.findViewById(R.id.checkable_list);
is wrong. You have to find ListView from the view you have inflated in onCreateView.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(layout.things_manager_fragment, container, false);
init(rootView);
return rootView;
}
public void init(View rootView){
//create an ArrayList object to store selected items
selectedItems = new ArrayList<String>();
//create an instance of ListView
checkable_list = rootView.findViewById(R.id.checkable_list);
//set multiple selection mode
checkable_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
String[] things_factory = {"Sports", "Politics", "Food", "Television", "Movies", "Fashion",
"Theoretical Physics"};
//supply data itmes to ListView
//ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.row, R.id.things_check, things_factory);
ArrayAdapter<String> aa = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, things_factory);
checkable_list.setAdapter(aa);
//set OnItemClickListener
checkable_list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// selected item
String selectedItem = ((TextView) view).getText().toString();
if(selectedItems.contains(selectedItem))
selectedItems.remove(selectedItem); //remove deselected item from the list of selected items
else
selectedItems.add(selectedItem); //add selected item to the list of selected items
}
});
}
I made a listview and I wanted to make an user input a choice. How can I do this? Atm I only managed to create the ListView and display the data.
Is there an easy way to get the value of a selected item of the ListView and use it later? Something like a ListView RadioButton.
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"
tools:context="com.example.tiagosilva.amob_android.TubeDataArchive" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv_tubeData"
android:choiceMode="singleChoice">
</ListView>
Listview:
public class TubeDataArchive extends Fragment {
public TubeDataArchive() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tube_data_archive, container, false);
ListView tubeDataList = (ListView) view.findViewById(R.id.lv_tubeData);
//load tube data
SharedPreferences settings = getActivity().getSharedPreferences("PREFS",0);
String tubeDataString = settings.getString("tubeData", "");
String[] tubeDataSplit = tubeDataString.split("\n");
List<String> tubeDataItems = new ArrayList<>();
for(int i=0; i<tubeDataSplit.length;i++)
{
tubeDataItems.add(tubeDataSplit[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, tubeDataItems);
// Assign adapter to ListView
tubeDataList.setAdapter(adapter);
return view;
}
}
Is there an easy way to get the value of a selected item of the
ListView and use it later?
Add onItemClickListener after the for loop.
tubeDataList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// do whatever you want
Log.d("############","Items " + tubeDataSplit[arg2] );
}
});
Something like a ListView RadioButton
If you want to have a listview with a radio button, then you need to create a custom listview layout.
Add ItemClickListener to your listview
tubeDataList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getapplicationcontext(), tubeDataItems.get(id),
Toast.LENGTH_LONG).show();
}
});
I have a spinner that let user choose a meal from a set of string array in xml. After the user selected the item, I put the selection into the database, and display on a Listview.
In my model, I have a toString(),
#Override
public String toString() {
return id + " " + note +"\n meal: " + this.meal;
}
In the controller class for editing the the meal, I have this,
public class EditNoteFragment extends Fragment implements
OnItemSelectedListener{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_edit,
container, false);
// populating the spinner with string array of time category
spinner = (Spinner) rootView.findViewById(R.id.spinner_meal_category);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity().getApplicationContext(), R.array.meal_array,
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
meal = parent.getItemAtPosition(pos).toString();
Log.i(LOGTAG, "Got the meal: " + meal);
}
public boolean onOptionsItemSelected(MenuItem item) {
// I added the field variable meal into the datebase in this block of code
//note.setMeal(meal.toString());
}
}
I used log to verified that I got the user choice and stored in the field variable "meal" in my class, but when I run the application and the list displays meal as null.
I am certain that I have successfully added the meal into the database.
Here is the view class
public static class NoteFragment extends ListFragment {
TextView tx_list_note;
NoteDataSource datasource;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
datasource = new NotesDataSource(getActivity());
datasource.open();
List<Note> notes = datasource.findAll();
if (notes.size() == 0) {
createData();
notes = datasource.findAll();
}
ArrayAdapter<Note> adapter = new ArrayAdapter<Note>(getActivity(),
android.R.layout.simple_list_item_1, notes);
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_notes,
container, false);
return rootView;
}
}
}
in fragment_notes.xml, I have one single listview in the relative layout
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_alignParentLeft="true"
>
</ListView>
Thank you very much for you help in advance. Any suggestion would be appreciated.
I have some issues with a listview in a Fragment.
I want to show a List with Fruits in the Fragment, tried a few things but.
In a later version the String[] should contain data from a json object.
This is the current status:
public class fragmentA extends Fragment
implements View.OnClickListener {
TextView textView;
ViewStub viewStub;
ListFragment listView;
static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana",
"Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
"Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// das Layout fuer dieses Fragment laden
View view= inflater.inflate(R.layout.fragmenta, container, false);
// inflate layout
Button notfallbtn = (Button) view.findViewById(R.id.notfallbtn);
textView = (TextView) view.findViewById(R.id.textView);
// listView = (ListFragment) view.findViewById(R.id.einkaufsliste);
viewStub = (ViewStub) view.findViewById(R.id.viewStub);
viewStub.setVisibility(View.GONE);
// initialize button using the inflated view object
notfallbtn.setOnClickListener(this);
// listener for button
setListAdapter(new ArrayAdapter<String>(this, R.layout.einkaufsliste,FRUITS));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
return view; // return inflated view
}
The error messages I get:
Cannot resolve 'setListAdapter'
Cannot resolve 'einkaufsliste'
Any help will be greatly appreciated.
public class fragmentA extends Fragment
does not extend ListFragment. setListAdapter is a method of ListFragment.
http://developer.android.com/reference/android/app/ListFragment.html
Also you probably need
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,FRUITS)
Instead of this use getActivity() which returns the activity this fragment is associated with.
Also read
http://developer.android.com/reference/android/app/ListActivity.html
Also override onActivityCreated and use ListView lv = getListView().
I have a ListFragment in my android application, I have got it to work, but the OnClick Listener is not working, I tried just making it so that when any item on the list is selcted a Toast appears and it is not happening, there is no Error so I have no LogCat to post
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.main, container, false);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
//...
ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
R.id.name, R.id.desciption, R.id.cost });
setListAdapter(adapter);
ListView lv = (ListView)v.findViewById(android.R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
Toast.makeText(getActivity().getApplicationContext(), "Not Configured",
Toast.LENGTH_SHORT).show();
}
});
return v;
}
Thanks
if your class extends ListFragment than everything you need to do is just overriding its onListItemClick method.
#Override
public void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
Toast.makeText(getActivity(), "Item " + pos + " was clicked", Toast.LENGTH_SHORT).show();
}
The ListFragment subclass already has it's overriden onListItemClick method.
The doc says:
This method will be called when an item in the list is selected. Subclasses should override
So there is no need to declare another listner for your listview.
Removing .getApplicationContext() should work. I have some code similar to yours from an app I made. Its from inside a fragment as well though works without problem. The db.remove is probably irrelevant to your code though because this code was written for an app with a database. Also maybe try changing new OnItemClickListener to new AdapterView.OnItemClickListener
listItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
db.remove( (int) l);
Toast.makeText(getActivity(), "Item Deleted", Toast.LENGTH_LONG).show();
}
});
If that doesn't work, maybe try making a Context instance variable like:
private Context ctx = getActivity();
or
private final Context ctx = getActivity();
I have never worked with ListFragments before though, so I am not sure if anything I wrote will work.
put
android:focusable="false"
android:clickable="false"
to all itens in your row
Make sure you
1.shouldn't have onclicklistener inside your Adapter
2.inside your XML layout
R.layout.main
Listview should be initialized like
<ListView
android:onClick="#id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/textHeader"
android:layout_margin="5dp"
android:divider="#color/DeepPink"
android:dividerHeight="1sp"
android:gravity="center"
android:horizontalSpacing="1dp"
android:visibility="visible" >
here id android:onClick="#id/list" important
ListView should be initialize like ListView lv = getListView if your extending your fragment by ListFragment instead Fragment