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
Related
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);
}
I have 2 activities:
the first - is just an opening page and it has a button that goes to the second activity
the second activity - will open up the contact list and will allow the user to pick contacts and retrieve their numbers - they will be saved for later use.
My problem is I can not figure out how to get it to work in the second activity. All the examples only have the contact list in the Main/first activity and I am really new to this and don't understand how to get it to move to the second.
my code I have right now
Main.java
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) this.findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick (View v){
Intent i = new Intent(MainActivity.this, Contacts.class);
startActivity(i);
}
});
}
}
Main 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" 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=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:id="#+id/button1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="93dp"/>
</RelativeLayout>
Contacts java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Contacts extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
}
}
Contacts 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" 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="akh.seniorproj.Contacts">
</RelativeLayout>
Check this solution: How to call Android contacts list?
Its pretty much what you need to do but instead name you should search the number with the cursor.
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.
I would like to add a form of input in my app where a list of options appear and multiple choices can be selected. I would use a spinner, but that only allows one option to be selected. I would then need for the options selected to be sensed for me to perform further operations with this. I think I would need something involving a checkedlistview but I am not sure. What would be the best way to go about doing this? I hope my situation is understood.
Use checkedlistview it Help you
ListMain.Java
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
#SuppressLint("ShowToast")
public class ListMain extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView mylist = (ListView) findViewById(R.id.companionsearch_listView1);
String[] list={"one","two","three"};
ArrayAdapter adapter = new ArrayAdapter<String>(ListMain.this,android.R.layout.simple_list_item_multiple_choice,list);
mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mylist.setAdapter(adapter);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/bottombarbuttonlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:baselineAligned="false"
android:weightSum="3" >
<ListView
android:id="#+id/companionsearch_listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#ffffff" >
</ListView>
</LinearLayout>
</RelativeLayout>
Output:
I need some help creating a listview inside a layout i have. im having torubles creating it properly.
Here is what i got so far.
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"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/gradient_bg"
android:src="#drawable/gs3ultimate" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/thelistarray"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#555555" />
<TextView
android:id="#+id/summary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/thelistarry_details"
android:textColor="#555555"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Now its just a image on the left, then 2 textview, Now i believe im gonna need 2 string arrys, one for the titles and the 2nd for the details. and i think a 3rd one for the images. Is that right?
Here some java for my list view
AndroidListViewActivity.java
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class AndroidListViewActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] list_array = getResources().getStringArray(R.array.list_array);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.item, R.id.label, list_array));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}
I know that my java is missing adding the details list_array_details. and i dont know how to add it so it populates. Im not yet using the onitemclick yet for my intent's. However would i determine which one is click and what activity to launch? example. i click list item b and want to like activity "b" but when i click a it launches activity "a"
Any help would be greately appreciated, Thanks in advance.
You need to have a custom adapter for this purpose to populate someother view other than a textview or multiple textviews.
Take a look on customadapter here and here