I wanted to generate a list view using below code. But after running this code the screen just shows a blank screen and the "hello" text which is there in main layout
package com.android.test1;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView;
public class HelloListView extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String a[] = new String[]{"a", "b", "c", "d"};
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, a));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
}
and the main layout code is as given below
<?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" >
<TextView
android:text="hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFFFF">
</ListView>
</LinearLayout>
Your ListView in XML should have this android:id="#android:id/list"
and remove that background attribute..
You are setting the adapter to the current activity, but it's view isn't a ListView but a LinearLayout. Try this to set the adapter to the correct view.
String a[] = new String[]{"a", "b", "c", "d"};
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, a));
In your code Just comment the below line
setContentView(R.layout.main);
In this case use the below link for using custom adapter.
LINK
Archie.bpgc
see this tutorial
http://www.bogotobogo.com/Android/android6ListViewSpinnerGridViewGallery.php#ListView
if you extend Listactivity you do not add
setContentView(R.layout.main);
and refer it like
ListView listView = (ListView) findViewById(R.id.list_view);
and if you do it like this
public class Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.list_view);
String a[] = new String[]{"a", "b", "c", "d"};
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
android.R.id.text1 ,
a));
// ListView lv = getListView();
listView.setTextFilterEnabled(true);
}
}
you have set the list background
android:background="#FFFFFFFF"
By default the color of list text is white
and you have given white background to list so the items does not appear
Related
I am trying to display a list of java lessons and created a listview for a button in my main java class in Android Studio but when I try to run and click the button, it opens another window but the listview doesn't show yet it is on the preview pane on Android Studio.
Here is my java class code:
package unitysoftwenteam.mymainuidesign;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.util.List;
public class JAVA_ACTIVITY extends AppCompatActivity {
ListView listview;
List list = new ArrayList();
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java__activity);
listview = (ListView)findViewById(R.id.jtutoriallist);
list.add("About Java");
list.add("Print Hello Java");
list.add("Variables");
list.add("Updating Variables");
list.add("Displaying the Output");
list.add("String Variables");
list.add("String Concatenation");
list.add("Variable Names");
list.add("Data Types");
list.add("Variable Arithmetic");
list.add("Casting");
list.add("Control Flow");
list.add("If Statement");
list.add("Variable Scope");
list.add("Else Statement");
list.add("Else-If");
list.add("Boolean Expressions");
list.add("Logical Operators");
list.add("Nested If Statements");
list.add("Switch Statement");
adapter = new ArrayAdapter(JAVA_ACTIVITY.this,
android.R.layout.simple_list_item_1);
listview.setAdapter(adapter);
}
}
This is my XML code:
<?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="unitysoftwenteam.mymainuidesign.JAVA_ACTIVITY">
<ListView
android:id="#+id/jtutoriallist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Actually you done every thing right, but you just have to specify the list that you want to adapt with the ListView:
adapter = new ArrayAdapter(JAVA_ACTIVITY.this, android.R.layout.simple_list_item_1, list);
Try this Code
ListView listview;
List list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList();
listview= (ListView) findViewById(R.id.jtutoriallist);
lists = (ListView) findViewById(R.id.lists);
list.add("About Java");
list.add("Print Hello Java");
list.add("Variables");
list.add("Updating Variables");
.
.
.
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list);
lists.setAdapter(adapter);
}
You have missed to send your data to the adapter. the 'list' that you had created. a adapter requires data and view to bind and populate the same. you have specified the layout but data is missed.
just change it into,
adapter = new ArrayAdapter(JAVA_ACTIVITY.this,
android.R.layout.simple_list_item_1,list);
listview.setAdapter(adapter);
I am working on ListViews. I decided to make a customized listview. For it, definitely another xml file (customized) is required. I built it but I just cannot call it by any means in java. Look the following line as row_layout is always RED with error "Cannot Resolve Symbol "row_layout":
ListAdapter myadapter = new ArrayAdapter<String> (this, android.R.layout.row_layout, cars);
Following is code of my customized xml file row_layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/row_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/myTextView1"
android:textSize="32dp"
android:textStyle="italic"/>
</LinearLayout>
Following is my Java file code :
package com.ranatalha.mylistapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String [] cars=
{
"Mehran",
"Corolla",
"Faw v2",
"Honda City"
};
//to link my above created array with in a list
//ListAdapter myadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, cars);
ListAdapter myadapter = new ArrayAdapter<String> (this, android.R.layout.row_layout, cars);
//referring the listview createdin xml
ListView mylistview = (ListView) findViewById(R.id.mylistView);
//connecting listview with adapter
mylistview.setAdapter(myadapter);
//Catching clicks on listview
mylistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
//implements method
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String CarsPicked = "Your Picked The Car: " + String.valueOf(adapterView.getItemAtPosition(position));
Toast.makeText(MainActivity.this, CarsPicked, Toast.LENGTH_SHORT).show();
}
}
);
}
}
android.R uses built in android resources. Try android.R.layout.simple_list_item1.
Or create your own layout and call its ID from R.layout.
Instead of android.R.layout.row_layout we should use R.layout.row_layout because android.R access the resources of the OS whereas R can help in importing custom layouts. Thanks to #BlackBelt for helping me in the comments section above :)
This is my activity class, copied by a book (Apress, "Pro Android 5"), that, by side, is a book full of typos in the code...
package com.example.list1;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView listView1;
private ArrayAdapter<String> listAdapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an object of type ArrayList from an array of strings
String[] someColors = new String[] { "Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet", "Black", "White"};
ArrayList<String> colorArrayList = new ArrayList<String>();
colorArrayList.addAll( Arrays.asList(someColors) );
// Use values from colorArraylist as values for each text1 'sbuView' used by the listView
listAdapter1 = new ArrayAdapter<String>(this, android.R.id.text1, colorArrayList);
// Tell to the listView to take data and layout from our adapter
listView1 = (ListView) findViewById(R.id.listView1);
listView1.setAdapter( listAdapter1 );
}
}
And this is my layout file
<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="com.example.list1.MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
When I save, Eclipse give me no problem. When I run the app crash and from logcat I can see an exception that i'm not able to understand/debug
08-04 13:43:34.382: E/AndroidRuntime(888):
android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020014
What am I doing wrong?
Actually you initialize arrayAdapter with view id instead of layout id.That's why you are getting this error.use this
ArrayAdapter<String> adp=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,someColors);
I am trying to make a edit text field where i enter a string and then by pressing the button beside it this string is added into a list view on a fragment.
When i compile the code i get the following error:
10-19 15:04:14.620 21147-21147/com.example.gasper.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.gasper.test.TopRatedFragment.onCreateView(TopRatedFragment.java:57)
this is the line 57
b.setOnClickListener(listener);
Does anybody now what is wrong here? I am a beginer in android development so dont judge me :)
I also tried somethong like this but still not working:
if(listener !=null){
b.setOnClickListener(listener);}
package com.example.gasper.test;
Here is the full code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import static com.example.gasper.test.R.layout.fragment_top_rated;
public class TopRatedFragment extends Fragment {
String[] myString =new String[] {"0ne","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"};
List list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(fragment_top_rated, container, false);
for(String activity : myString){
list.add(activity);
}
adapter = new ArrayAdapter<String>(this.getActivity(), fragment_top_rated,R.id.textView,list);
ListView listView = (ListView) rootView.findViewById(R.id.listView);
Button b = (Button) getActivity().findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText edit = (EditText) getActivity().findViewById(R.id.editText);
list.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
};
getActivity().setContentView(R.layout.fragment_top_rated);
b.setOnClickListener(listener);
listView.setAdapter(adapter);
return rootView;
}
}
Here is the fragment_top:rated.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffefe4fa" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="false"
android:layout_alignParentRight="false"
android:layout_below="#+id/editText" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="#string/edit_message"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick=""/>
</RelativeLayout>
replace this line of your code
Button b = (Button) getActivity().findViewById(R.id.button);
with this line and give it a try
Button b = (Button) rootView.findViewById(R.id.button1);
Edit 1:
adapter = new ArrayAdapter<String>(this.getActivity(), fragment_top_rated,R.id.textView,list);
this line of code shows you are using same xml...is there any textView defined in your this xml..just create a new xml for your list view..and use it here..
It seems that you mistyped the id:
Button b = (Button) getActivity().findViewById(R.id.button);
Your layout has button1 not button.
Edit:
Also the button is found in your fragment's layout not your activity's, so look for it there like this:
Button b = (Button) rootView.findViewById(R.id.button1);
ArrayAdapter has a an internal List of its own. The strings list you pass to its constructor is merely copied to the internal list to initialize the adapter.
User adapter object's add() or addAll() to actually add items to ArrayAdapter.
Change
list.add(edit.getText().toString());
to
adapter.add(edit.getText().toString());
Update 1:
Button b = (Button) getActivity().findViewById(R.id.button);
You are using a fragment and its not yet attached to activity, so activity can't find anything from a view that's not attached to it:
Button b = (Button) rootView.findViewById(R.id.button);
Here's my main activity:
package com.dannytsegai.worldgeography;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity {
private ListView listview;
private String[] mContinents;
private ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.list);
mContinents = getResources().getStringArray(R.array.continents_array);
adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item, mContinents);
listview.setAdapter(adapter);
}
}
Here's my main layout:
<?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="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
I've gone through the code, but I can't figure out what's wrong with it for the life of me. Can someone please help me?
Change this
R.layout.simple_list_item
to
android.R.layout.simple_list_item_1
since you are extending ListActivity, in the layout, your ListVeiw must have the id #android:id/list, otherwise you will get the following exeception:
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'.
To retrieve the ListView you do not need to call findViewById but you can use directly getListView() that returns the ListView
Try with this:
public class MainActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
}
If in doubt check this page: http://www.vogella.com/tutorials/AndroidListView/article.html#listactivit