I have tried so many things to get my android ListView to scroll. However I have been unsuccessful in my attempt. Here is my xml code below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fastScrollEnabled="true"
android:listSelector="#color/theme_colour"
android:scrollbarStyle="outsideInset">
</ListView>
</RelativeLayout>
I have also added my java code below. I am not sure how to fix this problem and your help would be greatly appreciated.
package com.outdeh;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
/**
* Created by horacedaley on 2/15/14.
*/
public class EventsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
populateListView();
}
public void populateListView(){
ParseQueryAdapter<ParseObject> adapter =
new ParseQueryAdapter<ParseObject>(this, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery<ParseObject> create() {
// Here we can configure a ParseQuery to our heart's desire.
ParseQuery query = new ParseQuery("Events");
query.orderByAscending("eDate");
return query;
}
});
adapter.setTextKey("eTitle");
ListView listView = (ListView) findViewById(R.id.listview);
listView.setFastScrollEnabled(true);
listView.setAdapter(adapter);
}
}
Try changing android:layout_height="wrap_content"s to android:layout_height="match_parent". I guess it doesn't scroll because when you set the height to "wrap_content" the view will go beyond the visible area in activity, and since all items are considered displayed (in spite of not being in the visible area), there won't be scrolling available.
Related
I'm trying to learn List view and adapters, made a very basic app to learn in which I have a button that inserts a String object to the data source of an array adapter and the other view is list view which shows this data set. add() and addAll() method apends the data to the data set but when I do that list view shows it but I didn't call notifyDataSetChanged method, so why list view refreshed itself and if it did so then why do I need the method? Could anyone help me by giving some clarity? add(), addAll() and insert() methods already making the list view show the new added data but how? so what's the difference?
the app's only layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="8dp">
<Button
android:id="#+id/insert_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="insert data"/>
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
the main activity code:
package famiddle.smart.apps.learningandroid;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button insertButton;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> data = new ArrayList<>();
data.add("hello world");
ListView listView = findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
listView.setAdapter(adapter);
insertButton = findViewById(R.id.insert_button);
insertButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
adapter.add("hi there");
}
});
}
}
I'm trying to dynamically set the values in a list. Originally this was taking place in the onCreate event itself. Thinking that perhaps the ListView object hadn't been initialized yet I moved this to a button click event. This simple example blows up nicely every time I tap the button with the "Unfortunately, ListViewTest has stopped" and no exception stack trace to be found. Also worth noting is that the real target app does not blow up until that ListView becomes visible when the container tab is tapped.
I am using a fully updated Android Studio 2.0 with the Nexus (5X API 23) emulator on El Capitan 10.11.4.
Any tips will be appreciated.
MainActivity.java
package com.greencountrytechnical.listviewtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onYouTappedMe();
}
}
);
}
private void onYouTappedMe()
{
String[] noob={"why","does","this","not","work","?"};
ListView lv=(ListView) findViewById(R.id.listView);
if(lv != null) {
List<String> lvs = new ArrayList<String>(Arrays.asList(noob));
// lvs is not null and it is filled with the values from the array.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_main, lvs);
if(adapter != null)
{
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
}
Layout:
<?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.greencountrytechnical.listviewtest.MainActivity">
<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_below="#+id/button" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
This is where your problem probably lies;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_main, lvs);
You can fix this by either creating your own list_item_layout or use what android gives you by default:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lvs);
I hope this helps!
I've only been working with Android for 2 weeks and I'm trying to create a simple homework assignment list to learn the basics. The code I have written displays information through an EditText and an ArrayList. When I go to one of my activities through my MainActivity and add information it shows up great. But when I go back to my MainActivity and back again to my previous activity. My data is deleted (or not displayed, I'm not sure). I'm sure I'm messing up something basic. If anyone is willing to offer some help or pointers, I'd greatly appreciate it. Thank You!
Here is my MainActivity Code:
package com.example.homeworkassignments;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
public class MainActivity extends Activity {
// instantiation of buttons
private Button btnDatabase;
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems = new ArrayList<String>();
//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseButton();
} // end onCreate()
public void databaseButton() {
//create database button
btnDatabase = (Button)findViewById(R.id.databaseBtn);
btnDatabase.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//start Database class when identify button is clicked
Intent i = new Intent(MainActivity.this, Database.class);
startActivity(i);
}
});
} // end databaseButton()
Here is my Database Activity Class Code:
package com.example.homeworkassignments;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class Database extends ListActivity {
private Button addData;
private Button removeData;
private Button back;
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems = new ArrayList<String>();
//DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_database);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems);
setListAdapter(adapter);
addButton();
removeButton();
backButton();
} // end OnCreate()
public void addButton() {
// create add button
addData = (Button)findViewById(R.id.databaseAddBtn);
addData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView myEdit = (TextView)findViewById(R.id.editText1);
String myEditValue = myEdit.getText().toString();
listItems.add(myEditValue);
adapter.notifyDataSetChanged();
myEdit.setText("");
}
});
} // end addButton()
public void removeButton() {
// create remove Button
removeData = (Button)findViewById(R.id.databaseRemoveBtn);
removeData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listItems.isEmpty()) {
listItems.clear();
}
else {
listItems.remove(0);
}
adapter.notifyDataSetChanged();
}
});
}
public void backButton() {
// create back Button
back = (Button)findViewById(R.id.databaseBackBtn);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Database.this, MainActivity.class);
startActivity(i);
}
});
} // end backButton()
} // end Database class
And finally, here is the XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="#+id/databaseAddBtn"
style="?#android:style/ButtonBar"
android:layout_width="35dp"
android:layout_height="35dp"
android:text="#string/add_button" />
<Button
android:id="#+id/databaseRemoveBtn"
style="?#android:style/ButtonBar"
android:layout_width="35dp"
android:layout_height="35dp"
android:text="#string/remove_button" />
<Button
android:id="#+id/databaseBackBtn"
style="?#android:style/ButtonBar"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="#string/back" />
</LinearLayout>
<EditText android:id="#+id/editText1"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="35dp"
android:ems="10" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" />
</LinearLayout>
I understand it's a lot and probably very poorly written, but any help or advice would be extremely appreciated (I'm just trying to learn Android!)
ok i think u want to keep the arraylist constant in Database.java...
there are 2 cases of storage i can guess :-
if u only want to store until you are executing the app and you allow the data be lost if the app is exited then in that case simply do this
a. Declare an ArrayList like this public ArrayList<String> listItems = new ArrayList<String>(); in MainActivity.java and remove the same from Database.java.
b. to add to this list you will access using MainActivity.listItems.add(object);
You can try using public static ArrayList<String> listItems = new ArrayList<String>();
instead in MainActivity note the "static" it can hold data even if u exited the app.For
more on static variables you can refer any tutorial.
it should store the data permanently so dat even if you reboot you will have the data.
if your case is this then u need to use database.
Hope it helps ....
thanks
I am basically trying to get which items have been selected within a multiple choice enabled ListActivity. I am sure that there are many beginning Android programmers that would love this to be clarified. This app runs fine until it gets to the FOR loop and tries to assign the values of the multi-choice items to ArrayList 'items'. I'm simply trying to use Toast to see if the multi-choice items are being assigned to the Arraylist 'items'. The app crashes when the button is clicked. testing is done in android 2.2 emulator
Here is my Main.xml 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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Selected Items" />
<ListView android:id="#android:id/list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:choiceMode="multipleChoice"
android:textFilterEnabled="true" android:fastScrollEnabled="true" >
</ListView>
</LinearLayout>
and here is the code
package com.tests.TestCode;
import android.app.ListActivity;
import android.os.Bundle;
import java.util.ArrayList;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] list = {"wrenches","hammers","drills","screwdrivers","saws","chisels","fasteners"};
// Initializing An ArrayAdapter Object for the ListActivity
final ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_multiple_choice, list);
setListAdapter(adapter);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ListView thelistview = (ListView) findViewById(android.R.id.list);
ArrayList<String> items = null;
SparseBooleanArray checkedItems = thelistview.getCheckedItemPositions();
if (checkedItems != null) {
for (int i=0; i<checkedItems.size(); i++) {
if (checkedItems.valueAt(i)) {
String item = adapter.getItem(checkedItems.keyAt(i)).toString();
items.add(item);
}
}
Toast.makeText(getBaseContext(), items.toString(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "checkedItems == null", Toast.LENGTH_LONG).show();
}
}
});
}
}
Much obliged to anyone that can provide a good answer as to the best way to go about accomplishing this ListActivity scenario, or anyone that can explain why the app crashes at the point of assignment to the ArrayList. Thanks!
Your items list is null and you try to use it before initializing it.
You need to use items = new ArrayList<String>(); somewhere before trying to use it, or you will keep getting NullPointerExceptions.
How to handle an empty ListView if I use simple_list_item_multiple_choice layout?
'Android Developers' say that I should use the android prefix
android:id="#android:id/empty
But cannot take in mind how to use it with simple_list_item_multiple_choice
protected ListView mFavList;
ArrayList<String> fakeFavs;
mFavList.setAdapter(new ArrayAdapter<String>(this.android.R.layout.simple_list_item_multiple_choice,fakeFavs);
if fakeFavs is null?
I solved my problem.
All I need - "empty" tag in main.xml layout and string of code:
mFavList.setEmptyView(findViewById(android.R.id.empty));
In my program fakeFavs is populating from text file.
In case of empty file, the empty list is properly handled.
Maybe it will be useful for somebody. Working example:
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class HelloAndroid3 extends Activity {
/** Called when the activity is first created. */
static final String split_symbol = "\n";
protected ListView mFavList;
protected ArrayList<String> fakeFavs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fakeFavs = new ArrayList<String>();
this.mFavList = (ListView) this.findViewById(R.id.list_favorites);
refreshFavListItems();
}
private void refreshFavListItems() {
//put some code here that populates fakeFavs
//.......
mFavList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, fakeFavs));
mFavList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mFavList.setEmptyView(findViewById(android.R.id.empty));
//if fakeFavs will be empty, list shows "No Data"
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<ListView android:id="#+id/list_favorites"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
/>
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:text="No Data"/>