ListView and ArrayAdapter with ConstraintLayout crashes the app - android

It works perfectly with linear layout but when I choose ConstraintLayout it doesn't work, I don't know why the app crashes
NumbersActivity.java
package com.example.bharat.learnmiwok;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class NumbersAct extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
ArrayList<String> words = new ArrayList<String>();
words.add("one");
words.add("two");
words.add("three");
words.add("four");
words.add("five");
words.add("six");
words.add("seven");
words.add("eight");
words.add("nine");
words.add("ten");
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, words);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
}
}
activity_numbers.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="com.example.bharat.learnmiwok.MainActivity"
android:id="#+id/list"
android:orientation="vertical">
</android.support.constraint.ConstraintLayout>
Thank you

You need to have a ListView with id list in your layout. Remove/Change the id from your ConstraintLayout, add a listview and try again.
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
Your ConstraintLayout is not a ListView.

Probably on this line you getting crash
ListView listView = (ListView) findViewById(R.id.list);
You tried to cast ConstraintLayout to ListView

You are using this:
ListView listView = (ListView) findViewById(R.id.list);
to find a ConstraintLayout. Try this instead:
ConstraintLayout listView = (ConstraintLayout) findViewById(R.id.activity_main);
This would only resolve your crash, but it's best to use a listView or contain one into the ConstraintLayout to achieve your goal.

Related

what is the difference between notifyDataSetChanged and add(T object) or addAll(T... items) methods in array adapter?

I'm trying to learn List view and adapters, made a very basic app to learn in which I have a button that inserts a String object to the data source of an array adapter and the other view is list view which shows this data set. add() and addAll() method apends the data to the data set but when I do that list view shows it but I didn't call notifyDataSetChanged method, so why list view refreshed itself and if it did so then why do I need the method? Could anyone help me by giving some clarity? add(), addAll() and insert() methods already making the list view show the new added data but how? so what's the difference?
the app's only layout:
<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="8dp">
<Button
android:id="#+id/insert_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="insert data"/>
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
the main activity code:
package famiddle.smart.apps.learningandroid;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button insertButton;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> data = new ArrayList<>();
data.add("hello world");
ListView listView = findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
insertButton = findViewById(R.id.insert_button);
insertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
adapter.add("hi there");
}
});
}
}

Listview widget isn't working in Android Studio

I'm new to Android Studio.For me there is a problem with listview. I've set all the ids properly and yet the problem occurs.
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.prashanthwagle.myapplication.MainActivity">
<ListView
android:id="#+id/sample"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp" />
</RelativeLayout>
Java:
package com.example.prashanthwagle.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
String[] s={"asfasaf","bafaf","cbfbd"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView)findViewById(R.id.sample);
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,R.layout.activity_main,s);
listView.setAdapter(a);
}
}
I Think that you should change
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,R.layout.activity_main,s)
;
to
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,android.R. android.R.layout.simple_list_item_1,s);
You have set the wrong layout for ArrayAdapter. Use this:
ArrayAdapter<String> a = new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_1,s);
The layout reference in the adapter initialization and assignment should not be the layout reference of your activity. Basically, this parameter tells the ListView what type of layout will each row of the list have. Read more about this from here: https://developer.android.com/guide/topics/ui/layout/listview.html

How change Color and Font On Listview [duplicate]

This question already has answers here:
Change ListView's textcolor
(2 answers)
Closed 7 years ago.
I am trying to change my font(color and size) and the back ground on my ListView. I want to change it with code lines not on xml. my list view looks like: the xml:
please answer to my ask.
Thankyou
<?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"
style="#style/styleName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="10dp" >
</ListView>
</LinearLayout>
code is:
package ir.Rubin.SecondTerm;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ActivityListView extends Activity {
ListView mListView;
List<String> mList = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_listview);
// findView
mListView = (ListView) findViewById(R.id.listview);
// for adding items
for (int i = 0; i < 200; i++) {
mList.add("Android Class" + i);
}
// Creating Data
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mList);
// Setting Adapter
mListView.setAdapter(mAdapter);
}
}
Check the properties present in the listview component , the link below(link one).
The Android documentation is extensive and very complete . She is the great friends of the developers.
If you want examples, take a look at Vogela(link two) site. There are good tutorials there.
But always give priority to official documentation .
hug =]
I hope this helps
Links:
1 -http://developer.android.com/reference/android/widget/ListView.html
2 -http://www.vogella.com/
<ListView>
android:id="#+id/listview"
style="#style/styleName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="10dp"
android:textsize="#sp"
android:textColor="[hex color code here]"
</ListView>
this should work

Showing error in ArrayAdapter ListView

I have written this code but it shows me error and i cannot identify where the problem is.My code is as follows-
package com.example.adapterexample;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.os.Build;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listAdapter();
}
public void listAdapter() {
String[] myItems = { "Apple", "Orange", "Grape", "Watermilon" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.da_items, myItems);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
}
xml for da_items
<?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>
xml for activity_main
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
//tools:context="com.example.adapterexample.MainActivity"
//tools:ignore="MergeRootFrame" >
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</FrameLayout>
It shows me error: E/ArrayAdapter(30552): You must supply a resource ID for a TextView
I do not add any text view then why this error is coming?
JavaExperts need your valuable suggestion to solve this issue.
Check official docs of ArrayAdapter.
public ArrayAdapter (Context context, int resource, T[] objects)
Added in API level 1
Constructor
Parameters
context - The current context.
resource - The resource ID for a layout file containing a TextView to use when instantiating views.
objects - The objects to represent in the ListView.
You will notice that the array adapter is expecting that the second parameter is a resource id of a text view not a layout.
So, when you use this constructor:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.da_items, myItems);
R.Layout.da_items must be an xml layout where the first element must be a TextView, something like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
//other attributes
/>
To be more sort of a custom textview, you can also use -
new ArrayAdapter<String>(this, R.layout.da_items,
R.id.the_id_of_a_textview_from_the_layout, myItems)
where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings.
The layout that you provide in the adapter - R.layout.da_items - should only contain a TextView because that's what android supports by default. So either use android.R.layout.simple_list_item_1 (this layout is provided by android) in place of your custom layout or make sure that your R.layout.da_items looks something like :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
Try this.
Add this in da_item.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" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/icon"
android:paddingBottom="10dp"
android:textColor="#CC0033"
android:textSize="16sp" />
</LinearLayout>
And in MainActivity.java
listView = (ListView) findViewById(R.id.list1);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.da_item, myItems);
listView.setAdapter(adapter);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.simple_list_item_1, myItems);
it will help you.
As you have used below code is creating problem
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.da_items, myItems);
So there is no need to make another layout and use it as R.layout.da_items
instead use
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.simple_list_item_1, myItems);

android:How to add a button to the top of the list view

In My application I want to add a button to the top of the List view. It means at the top button is there after that list view is continued.
Following is my code. using this I am getting
05-23 11:44:34.407:
ERROR/AndroidRuntime(1348):
java.lang.RuntimeException: Unable to
start activity
ComponentInfo{com.Elgifto/com.Elgifto.Egender}:
java.lang.NullPointerException.
Egender.java
package com.Elgifto;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class Egender extends ListActivity{
Button b1;
ListView lv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lv=(ListView) findViewById(R.id.list);
b1=new Button(this);
b1.setText("Done");
lv.addHeaderView(b1);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, GENDER));
lv.setItemsCanFocus(false);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setContentView(lv);
}
private static final String[] GENDER = new String[] {
"Male","Female"
};
}
gender.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">
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
Thanks.
your should write setContentView(R.layout.gender) first below super and ListView in xml should have the id like this android:id="#+id/android:list" not
android:id="#+id/list" because you class extends ListView .
super.onCreate(savedInstanceState);
lv=(ListView) findViewById(R.id.list);
You cannot search for any views until they are inflated. Call setContentView() within your layout first, and modify it after.
you must setContentView before use any view..
so, try this..
super.onCreate(savedInstanceState);
setContentView(R.layout.gender);
lv=(ListView) findViewById(R.id.list);
b1=new Button(this);
b1.setText("Done");
.
.
.
etc
and you alos missed to define GENDER variable..
so first define it like as
String GENDER = new String{"Male","Female"};

Categories

Resources