findViewByID not finding ListView [duplicate] - java

This question already has answers here:
findViewByID returns null
(33 answers)
Closed 7 years ago.
The logs say:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.t99sdevelopment.centralized/com.t99sdevelopment.centralized.ContactBookScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at
com.t99sdevelopment.centralized.ContactBookScreen.<init>(ContactBookScreen.java:26)
It's looking for #+id/nameList, but it very obviously has this, and is of type ListView, so I don't know what the problem is. I'm trying to take an array from Parse, set that array to teacherName[], teacherPosition[], and teacherNumber[], respectively, and then create an adapter to show that data on the ListView.
package com.t99sdevelopment.centralized;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.sql.Array;
import java.util.Arrays;
import java.util.List;
public class ContactBookScreen extends AppCompatActivity {
String[] teacherName;
String[] teacherPosition;
String[] teacherNumber;
ListView nameList = (ListView) findViewById(R.id.nameList); <---the error is here.
ListView positionList = (ListView) findViewById(R.id.positionList);
ListView numberList = (ListView) findViewById(R.id.numberList);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contactbookscreen);
setActionBarText();
}
public void parseGet(View view){
ParseQuery<ParseObject> query = ParseQuery.getQuery("data");
query.getInBackground("zF3GAgdbi3", new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
teacherName = (String[]) object.get("TeacherName");
teacherPosition = (String[]) object.get("TeacherPosition");
teacherNumber = (String[]) object.get("TeacherPhone");
} else {
}
}
});
ArrayAdapter<String> nameAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, teacherName);
nameList.setAdapter(nameAdapter);
ArrayAdapter<String> positionAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, teacherPosition);
positionList.setAdapter(positionAdapter);
ArrayAdapter<String> numberAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, teacherNumber);
numberList.setAdapter(numberAdapter);
}
private void setActionBarText(){
TextView actionBarText = (TextView) findViewById(R.id.toolbar_title);
actionBarText.setText("Contact Book");
}
}
Here's the XML:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#android:style/Theme.DeviceDefault.Light.NoActionBar"
android:background="#color/trojanWhite">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/actionbar" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ListView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/nameList"/>
<ListView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/positionList"
android:layout_toRightOf="#+id/nameList"/>
<ListView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/numberList"
android:layout_toRightOf="#+id/positionList"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/trojanBlack" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/navigationdraweritems"
app:headerLayout="#layout/nav_header"
/>
</android.support.v4.widget.DrawerLayout>

'android.view.View android.view.Window.findViewById(int)' on a null
object reference
Because using Activity Context before creation of Activity at class level.
access all Views inside onCreate after calling setContentView:
ListView nameList,positionList,numberList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contactbookscreen);
// access Views here from activity_contactbookscreen layout
nameList = (ListView) findViewById(R.id.nameList);
positionList = (ListView) findViewById(R.id.positionList);
numberList = (ListView) findViewById(R.id.numberList);
}

Related

How to fix a bug where my Android app keeps crashing on start, does not open? (see description for more details)

This is the full logcat info:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a
null object reference
at cmput301.subbook.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
These are the relevant files:
activity_main.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="cmput301.subbook.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit App"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mainText"
android:background="#e6a1a1"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<!-- <Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="144dp"
android:layout_marginTop="504dp"
android:text="Add New Subscription"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" /> -->
</android.support.constraint.ConstraintLayout>
MainActivity.java
package cmput301.subbook;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;
import android.widget.ArrayAdapter;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView text;
private List<String> subListVal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.mainText);
ArrayList<String> subListVal = new ArrayList<String>();
// subListVal = new ArrayList<String>();
subListVal.add("Netflix");
subListVal.add("Github");
subListVal.add("Spotify");
subListVal.add("Gym");
/* ArrayAdapter<String> adapter;
adapter = new ArrayAdapter <String>(this, R.layout.list_rows, R.id.listText, subListVal);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter); */
CustomAdapter adapter = new CustomAdapter(subListVal, this);
//handle listview and assign adapter
ListView lView = (ListView)findViewById(android.R.id.list);
lView.setAdapter(adapter);
/* Button add_btn = new Button(this);
add_btn.setText("Add New");
lView.addFooterView(add_btn); */
}
}
list_rows.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" >
<TextView
android:id="#+id/listText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp"
android:textColor="#ffe600" />
<Button
android:id="#+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:text="Delete" />
<Button
android:id="#+id/edit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/delete_btn"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="Edit" />
</LinearLayout>
CustomerAdapter.java
package cmput301.subbook;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by thesh on 2/3/2018.
*/
public class CustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list= new ArrayList<String>();
private Context context;
public CustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_rows, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.listText);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
Button editBtn = (Button)view.findViewById(R.id.edit_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
editBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
notifyDataSetChanged();
}
});
return view;
}
}
Literally my app was working, and suddenly I got this bug, without changing much. The last change I made was trying to add a button at the footer_view of the list. I tried googling the logchat error to no avail.
Would appreciate the help. Thanks.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at cmput301.subbook.MainActivity.onCreate(MainActivity.java:49)
This is telling you that line 49 in MainActivity has your problem, in a setAdapter() call, where you are calling it on something that is null.
That would appear to be this line:
lView.setAdapter(adapter);
That means lView is null.
You attempt to assign a value to lView in the preceding line:
ListView lView = (ListView)findViewById(android.R.id.list);
If lView is null, then findViewById(android.R.id.list) is returning null. This means that Android cannot find a widget in your layout that has an android:id value of #android:id/list.
That is because your ListView has a different ID value:
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/mainText"
android:background="#e6a1a1"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
These need to line up. So, either:
Change android:id in the <ListView> to #android:id/list, or
Change your findViewById() call to look for R.id.listView
change this ListView lView = (ListView)findViewById(android.R.id.list);
to ListView lView = (ListView)findViewById(R.id.listView);
Change -
ListView lView = (ListView)findViewById(android.R.id.list);
with -
ListView lView = (ListView)findViewById(R.id.listview);

I was trying to add fragment via xml and print data fetched from fragment to MainActivity,but the code isn't running

Main Activity XML File
<?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"
android:orientation="vertical"
tools:context="com.harsh1507.pc.t_1372.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="horizontal"
android:id="#+id/l1">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.harsh1507.pc.t_1372.Frag_a"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/tv"/>
</LinearLayout>
Main Activity JAVA File
package com.harsh1507.pc.t_1372;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements Frag_a.comm
{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= (TextView) findViewById(R.id.tv);
}
#Override
public void com(double s) {
tv.setText("Result is : "+Double.toString(s));
}
}
Fragment XML File
<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="horizontal"
tools:context="com.harsh1507.pc.t_1372.Frag_a">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:inputType="number"
android:id="#+id/ed1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/ed2"
android:inputType="number"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="cal"
android:id="#+id/btn"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rg"
android:orientation="vertical">
<RadioButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="ADD"
android:checked="true"
android:id="#+id/rb1"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="SUBSTRACT"
android:id="#+id/rb2"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="MULTIPLY"
android:id="#+id/rb3"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="DIVIDE"
android:id="#+id/rb4"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
Fragment JAVA File ,main problem may be present in this part but i am not able to debug that ,please go through the code and let me know where i missed something.
package com.harsh1507.pc.t_1372;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class Frag_a extends Fragment
{
View v;
comm c;
EditText ed1,ed2;
Button btn;
RadioGroup rg;
RadioButton rb1,rb2,rb3,rb4;
double d1,d2,comp;
#Override
public void onAttach(Context context)
{
super.onAttach(context);
c=(comm) context;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
v= inflater.inflate(R.layout.fragment_frag_a, container, false);
return v;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
ed1=v.findViewById(R.id.ed1);
ed2=v.findViewById(R.id.ed2);
btn=v.findViewById(R.id.btn);
rb1=v.findViewById(R.id.rb1);
rb2=v.findViewById(R.id.rb2);
rb3=v.findViewById(R.id.rb3);
rb4=v.findViewById(R.id.rb4);
rg= v.findViewById(R.id.rg);
d1=Double.parseDouble(ed1.getText().toString());
d2=Double.parseDouble(ed2.getText().toString());
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
if (rb1.isChecked()) {
comp = d1 + d2;
c.com(comp);
}
if (rb2.isChecked()) {
comp = d1 - d2;
c.com(comp);
}
if (rb3.isChecked()) {
comp = d1 * d2;
c.com(comp);
}
if (rb4.isChecked()) {
comp = d1 / d2;
c.com(comp);
}
}
});
}
public interface comm{
void com(double s);
}
}
Make sure that ur main activity extends FragmentActivity instead of AppCompat.
The corresponding import would be :
import android.support.v4.app.FragmentActivity;
Any activities using fragments must be implemented as a subclass of FragmentActivity instead of the traditional Activity class

Android ListView Integer array

I am trying to get an integer array into ListView using an adapter but do not seem to know where to begin.
I want the user to be able to open the listview, select a number from 1-99 and have the selected number be deducted from the current year and result displayed in a TextView.
Any help in the right direction would be fantastic!
first you have to create xml of listview given below
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</RelativeLayout>
make one arraylist<String> list=new arraylist<string>;
add value 1-99 using for loop
and then set the array adpter on that listview as like
package com.example.arraydemo;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
ArrayList<String> list;
ListView li;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=new ArrayList<String>();
li=(ListView)findViewById(R.id.listView1);
for(int i=1;i<=99;i++){
list.add(""+i);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list );
li.setAdapter(arrayAdapter);
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
}
});
}
}
and ontemclicklistener you get current date from system deduct position of listview from that date

My text is half visible when i want to add a name to my list (SQLite) Android

Hello im making an assignment list with database to add assignments. When i push on the add button i can type my assignment. But i only see half of it. When i click add, it correctly adds the new assignment to the list and i can see the whole text.
How can i solve this ?
This is my XML layout file
<?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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="be.ehb.android.arnojansens.DetailGroupsActivity"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listDetails"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4.60"></ListView>
<EditText
android:id="#+id/editDetails"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="Details Assignment"/>
<Button
android:id="#+id/btnAddDetailsOpdracht"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Add Details Assignment"/>
</LinearLayout>
</RelativeLayout>
And this is the java of the class
package be.ehb.arnojansens.fragmentexampleii;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
public class DetailGroupsActivity extends Activity implements View.OnClickListener {
static Dbhelper opdrDbHelper;
static Button btnAddDetailsOpdracht;
static EditText editDetails;
static ListView listDetails;
static ArrayList<Detail> details;
static long detailId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_groups);
btnAddDetailsOpdracht = (Button)findViewById(R.id.btnAddDetailsOpdracht);
editDetails = (EditText)findViewById(R.id.editDetails);
listDetails = (ListView)findViewById(R.id.listDetails);
btnAddDetailsOpdracht.setOnClickListener(this);
opdrDbHelper = new Dbhelper(this);
detailId = (Long)getIntent().getSerializableExtra("detailId");
String opdrachtName = (String)getIntent().getStringExtra("opdrachtName");
this.setTitle(opdrachtName);
if(detailId != -1)
details = opdrDbHelper.findDetailsForOpdracht(detailId);
else
details = new ArrayList<Detail>();
loadDetails();
}
public void loadDetails(){
ArrayAdapter<Detail> adapter =
new ArrayAdapter<Detail>(
this,
android.R.layout.simple_list_item_1,
details);
listDetails.setAdapter(adapter);
}
#Override
public void onClick(View v) {
String detailString = editDetails.getText().toString();
editDetails.setText("");
Detail alb = opdrDbHelper.addAlbum(detailId,detailString);
details.add(alb);
loadDetails();
}
}
Sound to me like the text doesn't fit in the EditText container. Make it wrap_content instead of a fixed size:
<EditText
android:id="#+id/editDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Details Assignment"/>
Also keep in mind that fill_parent was deprecated, use match_parent instead.

listview does not update with notifydatasetchanged() call

My Android listview does not update with notifydatasetchanged() call.
The Main Code Activity:
package com.example.jokesbook;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.jokesbook.MESSAGE";
CustomAdapter Adapter;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JokeDB.jokesList = new ArrayList<Joke>();
JokeDB.jokesList.add(new Joke("DDD"));
lv = (ListView)findViewById(android.R.id.list);
Adapter= new CustomAdapter(MainActivity.this, R.layout.joke_list_item, JokeDB.jokesList);
lv.setAdapter(Adapter);
}//onCreate
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
Log.d("jokesbook", "onResume ");
Adapter.notifyDataSetChanged();
}
class CustomAdapter extends ArrayAdapter<Joke>{
Context context;
int layoutResourceId;
ArrayList<Joke> data = null;
private LayoutInflater mInflater;
public CustomAdapter(Context customAdapter, int layoutResourceId, ArrayList<Joke> data) {
super(customAdapter, layoutResourceId, data);
Log.d("jokesbook", "CustomAdapter ");
this.layoutResourceId = layoutResourceId;
this.context = customAdapter;
this.data = data;
this.mInflater = LayoutInflater.from(customAdapter);
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
//item_list
convertView = mInflater.inflate(R.layout.joke_list_item, null);
holder = new ViewHolder();
//fill the views
holder.joke = (TextView) convertView.findViewById(R.id.ListTextView1);
convertView.setTag(holder);
}
else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();//
}
holder.joke.setText(data.get(position).jokeStr);
return convertView;
}
class ViewHolder {
TextView joke;
}
}
/** Called when the user clicks the Add a new joke button */
public void goNewJoke(View view) {
Intent intent = new Intent(this, New_joke.class);
startActivity(intent);
}
}
Its 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="#+id/ButtonAboveList"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:onClick="goNewJoke"
android:text="#string/Add_a_new_joke"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
When adding content to JokeDB.jokesList in another activity that its code is:
package com.example.jokesbook;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class New_joke extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_joke);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_joke, menu);
return true;
}
public void addJoke(View view){
EditText editTextJoke= (EditText)findViewById(R.id.edit_text_jokeToAdd);
JokeDB.jokesList.add(new Joke(editTextJoke.getText().toString()));
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
and its XML is:
<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:background="#color/green"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="3dp"
tools:context=".New_joke" >
<EditText
android:id="#+id/edit_text_jokeToAdd"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginBottom="40dp"
android:background="#color/white"
android:hint="#string/edit_message" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
tools:context=".New_joke" >
<TextView
android:id="#+id/AuthorText"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/Author"
android:textColor="#android:color/white"
android:textSize="25sp" />
<EditText
android:id="#+id/edit_text_Author"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#color/white"
android:hint="#string/Only_letters_allowed"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
tools:context=".New_joke" >
<TextView
android:id="#+id/DateText"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/Date"
android:textColor="#android:color/white"
android:textSize="25sp" />
<EditText
android:id="#+id/edit_text_Date"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#color/white"
android:hint="#string/Only_digits_allowed"
android:inputType="number" />
</LinearLayout>
<Button
android:id="#+id/ButtonAboveList"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="addJoke"
android:text="#string/Add" />
</LinearLayout>
I cannot see the updated list in the main activity eveb though in onResume I used notifydatasetchanged() function.
Try This....
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
Log.d("jokesbook", "onResume ");
notifyDataSetChanged();
}

Categories

Resources