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);
Related
I have this list of countries
Vatican City
China
Italy
Iran
S. Korea
Spain
Germany
France
i save this list in shared preferences then call it to put it in listview,
the problem is, it got only the last item, so France will saved last and it will be the only one in the listView
How can i solve that? i know with loop, but i'm not good at it
Any help with codes will be appreciated !
Try this code:
MainActivity.java
import android.arch.lifecycle.AndroidViewModel;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// By ID get the AutoCompleteTextView
// which id is assign in xml file
AutoCompleteTextView
autoCompleteTextView
= (AutoCompleteTextView)
findViewById(
R.id.autocompleteTextView);
// Create the string array
// and store the values.
String[] colors
= { "Red", "Green", "Black",
"Orange", "Blue", "Pink",
"Blush", "Brown", "Yellow" };
// Create the object of ArrayAdapter with String
// which hold the data as the list item.
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(
this,
android.R.layout.select_dialog_item,
colors);
// Give the suggestion after 1 words.
autoCompleteTextView.setThreshold(1);
// Set the adapter for data as a list
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView.setTextColor(Color.BLACK);
}
}
activity_main.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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="80dp"
android:text="Write the color name !"
android:textSize="20dp"
android:textStyle="bold" />
<AutoCompleteTextView
android:id="#+id/autocompleteTextView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:layout_marginLeft="90dp"
/>
</RelativeLayout>
See here for more details: https://www.geeksforgeeks.org/android-auto-complete-textbox-and-how-to-create-it/
If you want to get data which are saved in shared preferences use this code:
val j=adapter.count
var i=0
while (i<j){
Log.d("abc",adapter.getItem(i))
i++}
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'm currently working on attempting to make an RSS Reader using a ListView in Android studio. I've already managed to make the ListView, but i have no idea where to go next. I cant seem to find any good tutorials online on how i should tackle this, and Anything i do find is out of date by 3 years. Any tips?
activity_main.xml
<?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: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.micha.rssreader.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/listviewAD" />
MainActivity.java
package com.example.micha.rssreader;
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.ListView;
public class MainActivity extends AppCompatActivity {
public ListView AdFeed; // maakt listview aan
public String[] items;
public ArrayAdapter<String> adapt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdFeed = (ListView)findViewById(R.id.listviewAD);
items = new String[]{"blue", "red", "black", "orange", "purpol"};
adapt = new ArrayAdapter<String>(this, R.layout.items, items);
AdFeed.setAdapter(adapt);//zet de data acther de listview
AdFeed.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
Try this example
http://www.androidauthority.com/simple-rss-reader-full-tutorial-733245/
it is up to date since it uses buildToolsVersion 25.0.1.
Only difference is that it uses RecyclerView instead of ListView, which is recommended since it is lighter than 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
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);
}