My app crashes when trying to use listview.
The xml (called activity_feedbackresults)
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true" >
</ListView>
the java
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feedbackresults);
lv = (ListView) findViewById(R.id.listView1);
String[] arr = {"A","B","C"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_feedbackresults, arr);
lv.setAdapter(adapter);
}
edit: logcat says ArrayAdapter You must supply a resource ID for a TextView.
I don't understand what that means.
use the constructor that lets you enter textview id and give the id of your textview that you specified int the layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.activity_feedbackresults,
R.id.textviewId,
arr);
Related
This is my main Activity. XML file is also provided. I am Retrieving Data from Fire base Database in a Spinner. It is Viewing in Drop down menu but the selected item is not setting to the Spinner.Class showFacultySpinner is being used in the main activity to retrieve data from Firebase. If someone want to also check that, i will provide.
public class TestSpinnerAddCourse extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
DatabaseReference databaseReference;
showFacultySpinner showFacultySpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_spinner_add_course);
databaseReference = FirebaseDatabase.getInstance().getReference();
showFacultySpinner = new showFacultySpinner(databaseReference);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter =
new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_item,
showFacultySpinner.retreive()
);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "Selected: " + item , Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
This is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Category:"
android:layout_marginBottom="5dp"/>
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
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 have two activities. MainActivity.java and HomeActivity.java. I am trying to create a ListView in HomeActivity.java just with "One", "Two", "Three", and "Four" as the List items but the ListView is not showing up at all.
HomeActivity.java:
public class HomeActivity extends AppCompatActivity {
String personName;
String email;
String rfid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
populateListView();
Bundle extras = getIntent().getExtras();
if (extras != null) {
personName = extras.getString("Name");
email = extras.getString("Email");
rfid = extras.getString("RFID");
}
String params = (email + "+" + rfid);
new MyAsyncTask().execute(new Pair<Context, String>(this, params));
toolbar.findViewById(R.id.ButtonSettings).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(HomeActivity.this, findViewById(R.id.ButtonSettings));
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.getMenu().add(personName);
popup.getMenu().add(email);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(
HomeActivity.this, "You Clicked" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
}
});
}
private void populateListView(){
String [] myItems = {"One", "Two", "Three", "Four"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.show_tasks, myItems);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
}
content_home.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="ie.myy3p.ticktask.HomeActivity"
tools:showIn="#layout/activity_home">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/profile_layout"
android:layout_below="#+id/toolbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tView"
android:layout_alignBottom="#id/toolbar"
android:layout_alignParentEnd="true" />
<ListView android:id = "#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
show_tasks.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TextView>
EDIT:
So I made a few changes to my code and now my ListView is showing up in content_home.xml and activity_home.xml in Android Studio, which it wasn't doing before. However, when I run the app on my phone the ListView doesn't show up. I've debugged it and checked my populateList method and it seems to be working fine. Debug at the end of populateList method reads:
this = {HomeActivity#20765}
myItems = {String[4]#20825}
adapter = {ArrayAdapter#20841}
list = {ListView#20853} "android.widget.ListView... etc
By giving a custom view to the adapter, you need an extra step to populate how that custom view uses the array adapter data to populate. The easiest solution is just ditch the custom view and use the simple list:
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
myItems);
If you want to continue to use a custom view for your cells, you'd need to create a custom adapter class that overrides getView() to tell the listview how given your string value, how it populates the view, essentially it sets the text on your textView (however this is the default behavior of android.R.layout.simple_list_item_1).
Here's a great example if you want to keep your custom view: https://stackoverflow.com/a/15832564/1316346
EDIT:
Also looks like you are inflating the wrong view:
setContentView(R.layout.activity_home);
is inflating with a resource called activity_home, from what you've posted it should probably read:
setContentView(R.layout.content_home);
Change here.
private void populateListView(){
String [] myItems = {"One", "Two", "Three", "Four"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.show_tasks, myItems);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
To this
private void populateListView(){
String [] myItems = {"One", "Two", "Three", "Four"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, myItems);
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
Alternative 2
Change set content view from this
setContentView(R.layout.activity_home);
To this
setContentView(R.layout.content_home);
It's Done.
I am trying to add a spinner dynamically to a RelativeLayout but I am having problems.
This is my code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/Relativ"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
And in my class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.Relativ);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,1);
adapter.add("1");
adapter.add("2");
adapter.add("3");
adapter.add("4");
adapter.add("5");
Spinner list = new Spinner(this);
list.setAdapter(adapter);
rl.addView(list);
}
But it does't run. I would like to know what is wrong in my code and how I can fix it.
I have tryed with RelativeLayout params in the RelativeLayout.addView and it doesn't run.
Thank you.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = new RelativeLayout(this);
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
Spinner list = new Spinner(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, arrayList);
list.setAdapter(adapter);
rl.addView(list);
setContentView(rl);
}
main.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
adapter.add("1");
adapter.add("2");
adapter.add("3");
adapter.add("4");
adapter.add("5");
final Spinner list= (Spinner) findViewById(R.id.spinner1);
list.setAdapter(adapter);
}
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: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.prova.MainActivity" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp" />
</RelativeLayout>
Check with your parameters.
I'm trying to set an adapter to a ListFragment, but no matter what I do it always appears as an "Empty List". I'm instantiating studentList just before setting the adapter. There must be something very simple that I'm missing. How can I write this so that my code displays the given studentList?
Main Screen.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#android:id/empty"
android:layout_gravity="center"
android:text="#string/empty_list"
style="#style/font_large"/>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainScreenFrag.java
public class MainScreenFrag extends ListFragment {
public final static String TITLE = "Student List";
public final static String TAG = "MainScreenFrag";
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
String[] studentList = {"me", "you", "everybody", "nobody","some people", "most people", "just me", "ok you too"};
setListAdapter(new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
studentList));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_screen, container, false);
}
}
You're not calling
super.onViewCreated(view, savedInstanceState);
in your onViewCreated
Give that a try
Its mostly because you have not given an id for your list Adapter.
ListView lv = (ListView) findViewById(R.id.list);
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, studentList);
lv.setAdapter(arrayAdapter);
This should work, though the method seems redundant.