Android Array Adaptor - insert a checkbox into each row - android

Right so, I have a navbar layout. The <FrameLayout> in content_main is replaced with stand1.xml when the stand1 button in my navbar is pressed. This works without any issue.
Content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior" tools:showIn="#layout/app_bar_main"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"></FrameLayout>
</RelativeLayout>
stand1.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">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/Stand1list">
</ListView>
</LinearLayout>
from main activity
if (id == R.id.Stand1) {
fm.beginTransaction().replace(R.id.content_frame, new Stand1()).commit();
setTitle(R.string.Stand1);
Following tutorials online this is my Stand1.java file which is used to populate the a list with a array of strings
public class Stand1 extends Fragment {
ListView mList;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.stand1,container,false);
mList = (ListView) root.findViewById(R.id.Stand1list);
return root;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
populateListView();
}
private void populateListView() {
String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"};
//build adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.row_layout,pair);
mList.setAdapter(adapter);
}
}
and row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android" />
This is working, It displays the 5 strings. However i want to make it so i can also have a check box in each row.
If i try place linear layout in row_layout.xml , i get error
ArrayAdapter requires the resource ID to be a TextView
I was following stackoverflow which sorted other errors which suggested that i had to make the list at class level and inflate it in onCreate.
I know i prob have to make an custom array adapter, but my problem is how do i deal with the inflation of the view in the on create.
Im not really sure how i go about doing this. So can somebody tell me from here, how do i go about having a check box in each row.
Thanks for the help

ArrayAdapter needs to know where to put the text from your string array. By default, it assumes the layout you pass it is a TextView and will set its text to one of the strings. If you pass it a more complicated layout, you need to tell it the id of the TextView you want it to use. You can do that with this constructor.

Related

Android complaining about content not having Listview with id android.R.id.List despite having that

obviously not a profound question, but I think theres probably an error in my approach.
So I've added the file listview.xml to my project in the layout folder.
it contains this:
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
I've also tried:
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
And i my main activity function generally looks like this:
public class ToDoManagerActivity extends ListActivity {
private static final int ADD_TODO_ITEM_REQUEST = 0;
private static final String FILE_NAME = "TodoManagerActivityData.txt";
private static final String TAG = "Lab-UserInterface";
// IDs for menu items
private static final int MENU_DELETE = Menu.FIRST;
private static final int MENU_DUMP = Menu.FIRST + 1;
ToDoListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a new TodoListAdapter for this ListActivity's ListView
mAdapter = new ToDoListAdapter(getApplicationContext());
// Put divider between ToDoItems and FooterView
getListView().setFooterDividersEnabled(true);
// TODO - Inflate footerView for footer_view.xml file
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);
// NOTE: You can remove this block once you've implemented the assignment
if (null == footerView) {
return;
}
// TODO - Add footerView to ListView
getListView().addFooterView(footerView);
setListAdapter(mAdapter);
setContentView(R.layout.footer_view);
This keeps breaking with the error:
...java.lang.RunTimeException: content must have a ListView whose id attribute is 'android.R.id.list'
I noticed that if I comment out the setContentView(R.layout.footer_view) bit the error disappears, which is quite confusing as well, because it seems like the error should trigger before there.
This is confusing the hell out of me because as far as I can tell this element exists. Its seems like maybe I'm missing a step to load the ListView? I'm banging my head against the wall here and this seems like something really basic, and being a n00b sucks...So any help is much appreciated!
Cheers!
EDIT:
footer_view.xml contents:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/footerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/add_new_todo_item_string"
android:textSize="24sp" >
</TextView>
EDIT2:
Current onCreate after suggested edits:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a new TodoListAdapter for this ListActivity's ListView
mAdapter = new ToDoListAdapter(getApplicationContext());
// Put divider between ToDoItems and FooterView
getListView().setFooterDividersEnabled(true);
setListAdapter(mAdapter);
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, getListView(), false);
getListView().addFooterView(footerView);
}
For an Activity that is extending ListActivity, the ListView needs to exist within the xml file that you are using in the activity, in your case that's the footer_view file.
Taken from the Google Dev Site - "ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "#android:id/list""
To incorporate your footer below your list, try this:
<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="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="#+id/footerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/add_new_todo_item_string"
android:textSize="24sp" >
</TextView>
</LinearLayout>
Here, try this in your layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:id="#android:id/list" />
</LinearLayout>
That is exactly what I have in my app.
Then in your onCreate(), just this:
setContentView(R.layout.activity_main);
Try it and let me know if it works. That code should work. From there, we can continue on to isolate the problem.
EDIT:
Man, now I see your problem. Not a big deal here. Let's look at your code (edited to be shorter). The onCreate() method has this:
super.onCreate(savedInstanceState);
mAdapter = new ToDoListAdapter(getApplicationContext());
getListView().setFooterDividersEnabled(true);
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);
getListView().addFooterView(footerView);
setListAdapter(mAdapter);
setContentView(R.layout.footer_view);
Do you see R.layout.activity_main anywhere there? There's your issue! Your R.layout.footer_view only contains a textview, it doesn't contain the ListView, and your Activity keeps looking for the ListView that you promised it that you'd have by extending a ListActivity.
Try this - change your onCreate() method to this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new ToDoListAdapter(getApplicationContext());
setListAdapter(mAdapter);
}
Then your activity_main.xml layout file should look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:id="#android:id/list" />
</LinearLayout>
This should work. Let me know.
EDIT2:
Now add this to the end of your onCreate() method:
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);
getListView().addFooterView(footerView);
Voila! Should work now.

ListView not appearing in fragment

So I have two Fragments inside an Activity using a PageAdapter.
One of the Fragments is a Listview and the other is a GridView of images.
The fragments are created successfully but the ListView is empty
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = inflater.inflate(R.layout.eventlist ,container, false);
String[] x = new String[]{"AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC","AAA","BBB","CCC"};
ListView listView = (ListView) view.findViewById(R.id.listView);
ArrayAdapter<String> test = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1,x);
listView.setAdapter(test);
return view;
}
XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
You should not check null for listview. But rather check if the list is empty.
Change it to listView.getChildCount()==0.
But I think you dont need to check because it will always be empty because it's a new created view. And no items on the list. If you want to check correctly, put the condition check after you set the adapter to the list.
Check if this works.

FindViewByID(...) returns null after setting content view and cleaning project

There are a number of questions like this but the solutions haven't worked for me.
Anyways, my main activity has a button that, in its onclick method, takes me to another activity, ViewPowerActivity. I have a layout xml file for it called power_view.xml. Inside that I have some 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="wrap_content"
android:layout_marginLeft="#dimen/screen_margin"
android:layout_marginRight="#dimen/screen_margin"
android:layout_marginTop="#dimen/screen_margin"
android:orientation="vertical" >
...
ViewPowerActivity has the basic onCreateMethod:
public class ViewPowerActivity extends Activity {
private final static Powers powers=new StubPowers();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.power_view);
Power power=powers.getPowers().get(0);
View powerView = findViewById(R.layout.power_view);
...
}
...
}
The findViewById call above is returning null.
If I remove all the code after setContentView(...) and simply return there, it displays the empty layout just fine. I've setContentView, I've cleaned the project, I've tried setting the power view as the main activity, and all sorts of things. What else could it be?
From you code it is clear thar power_view is a layout ie. xml. So R.id.power_view is incorrect.
It seems you want to access the parent view of that layout. Then do the following.
You have to set an id to the parent LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/screen_margin"
android:layout_marginRight="#dimen/screen_margin"
android:layout_marginTop="#dimen/screen_margin"
android:orientation="vertical" >
...
Then,
View powerView = findViewById(R.id.parentLinear);
If you want to take some other view in powerView, you should set id to that view in xml layout and initialize your that view to power_view by findViewById(R.id.your_view)
power_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/screen_margin"
android:layout_marginRight="#dimen/screen_margin"
android:layout_marginTop="#dimen/screen_margin"
android:orientation="vertical" >
public class ViewPowerActivity extends Activity {
private final static Powers powers=new StubPowers();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.power_view);
Power power=powers.getPowers().get(0);
View powerView = (ViewGroup)findViewById(R.id.layout);
...
}
...
}

Using screen fragments on android

I am working on android app and am trying to get fragments working but I've run into a problem.
When the app launches it loads an activity which is supposed to house the two fragments. Fragment1 contains a list of stored logins and fragment 2 will show the details associated with that login.
The list fragment is supposed to retrieve data from the SQLite database and display on the screen and once the item is clicked, it loads the second fragment, but I am having problem getting the first stage working.
In my main activity class I have the following.
public class PasswordListMain extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.password_management);
}
}
The password_management XML file contains the following
<?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" >
<fragment
android:id="#+id/passwordList"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.BoardiesITSolutions.PasswordManager.PasswordListFrag">
</fragment>
</LinearLayout>
It just contains the fragment as this is the portrait screen, I thought I'd get this bit working before getting it working side by side.
The PasswordListFrag contains the following
public class PasswordListFrag extends ListFragment{
Common common;
com.BoardiesITSolutions.logic.ManagePasswordList managePasswordList;
ArrayList<String> savedPassword;
TextView txtNoRecords;
ListView myListView;
ArrayAdapter<Spanned> passwordArrayAdapter;
AdView adView;
#Override
public View onCreateView(LayoutInflater inflator, ViewGroup container, Bundle savedInstanceState)
{
View view = inflator.inflate(R.layout.password_list, container, false);
myListView = getListView();
common = new Common(getActivity().getApplicationContext());
//AdView adView = (AdView)findViewById(R.id.adView);
common.requestAdvert(adView);
managePasswordList = new ManagePasswordList(getActivity().getApplicationContext());
//txtNoRecords = (TextView)findViewById(R.id.password_noRecords);
populateListArray();
common.showToastMessage("Adapter updated", Toast.LENGTH_LONG);
//myListView.setOnItemClickListener(mListView);
return view;
}
private void populateListArray()
{
ArrayList<Spanned> passwords = managePasswordList.getPasswordList();
if (passwords != null && passwords.size() > 0)
{
passwordArrayAdapter = new ArrayAdapter<Spanned>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, passwords);
setListAdapter(passwordArrayAdapter);
passwordArrayAdapter.setNotifyOnChange(true);
myListView.setTextFilterEnabled(true);
txtNoRecords.setVisibility(View.GONE);
}
else
{
txtNoRecords.setVisibility(View.VISIBLE);
}
}
}
Below is the XML for the list fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="#+id/password_noRecords"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:text="There are currently\nno saved logins"
android:textSize="20dp"
android:textStyle="bold|italic"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/adView">
</ListView>
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="5555555"
ads:adSize="BANNER"
android:layout_alignParentBottom="true">
</com.google.ads.AdView>
</RelativeLayout>
When the app loads it instantly crashes when it tries to load this screen.
The error is
java.lang.RuntimeException: Unable to start activity ComponentInfo
PasswordListMain: android.view.InflatException: Binary XML file line
#7 Error inflating class fragment
I have no idea what's causing this or how to fix the problem.
Thanks for any help you can provide.
My best guess for the issue is this line (but admittedly I'm unsure):
myListView = getListView();
You're calling it before the fragment has a view - and I don't think that will work too well. I would recommend doing your initialization work of the views involved in onActivityCreated() instead of in onCreateView() and the other components (like your data store thing and Common) in onCreate()

android ListView doesn't get updated

As title says, my listview won't get filled.
I know that the adapter is getting filled with 3 items, debugged my way through it. But my xml isnt showing anything at all:
The adapter is a global variable if that should do anything.
Code:
private void listChange(CustomCursor c){
List<SimpleListItem> itemsList = new ArrayList<SimpleListItem>();
while (c.moveToNext()) {
String picture = c.getString("picture");
String text = c.getString("name");
itemsList.add(new SimpleListItem(text, picture));
}
productListAdapter = new ProductAdapter(getBaseContext(),R.layout.product_item, itemsList);
setListAdapter(productListAdapter);
productListAdapter.notifyDataSetChanged();
}
XML of the activity:
<?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" >
<dk.foo.views.PageHeaderView
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</dk.foo.views.PageHeaderView>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Modify the orientation of your LinearLayout to vertical if you want to see the ListView.
Right now with the orientation of your parent LinearLayout set to horizontal and the width of your custom view PageHeaderView set to FILL_PARENT, your ListView is pushed out of the screen.

Categories

Resources