Listview not showing? - java

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);

Related

Built a custom file in XML, Can't call it in Java by any means

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 :)

Exception when using array adapter and listView

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);

What is wrong with the way I'm setting up my listview?

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

Error setListAdapter in simple program

I'm new to android programming and I still have a few things that I don't understand. For example, I'm with a program that when you click on a button, it tries to find any devices near. The problem is that I don't know how to solve a mistake I seem to have made from the beggining. This is what I have
This is the main activity:
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void buscar(View view){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
List<String> s = new ArrayList<String>();
for(BluetoothDevice bt : pairedDevices)
s.add(bt.getName());
setListAdapter(new ArrayAdapter<String>(this, R.id.list, s));
}
}
And here is the xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
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" >
<Button
android:id="#+id/botonvinculo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:onClick="buscar"
android:text="Buscar dispositivos" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/botonvinculo"
android:layout_below="#+id/botonvinculo" >
</ListView>
</RelativeLayout>
The thing is that I cannot make the call to setListAdapter unless my main activity extends from ListActivity, and even trying so, it returns an error saying "You must have a ListView whose attribute is "android.R.id.list".
I'd appreciate any help. Thank you very much.
If you use ListActivity then you should use android:id="#android:id/list" in your XML instead of android:id="#+id/list"
or else you can get an object of ListView in your XML like
ListView list = (ListView) findViewById(R.id.list);
and then use list.setAdapter
setListAdapter will work only when your activity extends from ListActivity. use this in your case
listview.setAdapter();
if you want to use setListAdapter() only make change these things
public class MainActivity extends Activity
to
public class MainActivity extends ListActivity
and
android:id="#android:id/list" for your list view in xml
Your class extends Activity. setListAdapter is a method of ListActivity
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.list);
}
Then
public void buscar(View view){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
List<String> s = new ArrayList<String>();
for(BluetoothDevice bt : pairedDevices)
s.add(bt.getName());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple.list_item_1,s);
lv.setAdapter(adapter);
}

ListView is not getting populated

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

Categories

Resources