ListView not showing up / appearing android studio - java

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.

Related

Android FrameLayout ListView elements don't appear clickable

I have an application that loads items into a ListView that is inside a FrameLayout. That part works fine but the click functionality doesn't seem to work, when I hover over the items they don't have the clickable hover appearance and when clicked do nothing.
I've Googled around and there's some mentions of focus and clickable types in the xml but having tried adding these they don't seem to make any difference.
Is there something wrong with the layout structure that I'm using? or can I add something to ensure the ListView elements are clickable?
LansFragment
Map<String,String> myMap = new HashMap<>();
JSONArray lanArray = data; //json arrray, blank for example
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
ArrayList<String> itemArray = new ArrayList<String>();
try {
JSONArray jsonMainArr = jsonObj.getJSONArray(lanArray);
for (int i = 0; i < jsonMainArr.length(); i++) {
JSONObject json_data = jsonMainArr.getJSONObject(i);
String name = json_data.getString("lanName");
String key = json_data.getString("lanKey");
itemArray.add(name);
myMap.put(name, key);
}
} catch (Exception e) {
// catch here
}
ArrayAdapter adapter = new ArrayAdapter<String>(view.getContext(), R.layout.activity_listview, itemArray);
ListView listView = (ListView) getActivity().findViewById(R.id.listview);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
String lan = l.getItemAtPosition(position).toString();
String key = myMap.get(lan);
// Not currently working due to no click registering
}
});
return view;
}
activity_main
<?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:id="#+id/activity_main"
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="app.stats.LansFragment"
android:orientation="horizontal">
<FrameLayout
android:id="#+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listview" />
</FrameLayout>
</LinearLayout>
activity_listview
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
Your call the to the ListView id inside your fragment calls:
ListView listView = (ListView) getActivity().findViewById(R.id.listview);
But inside your fragment you should reference the Fragment view. It should be:
ListView listView = (ListView) view.findViewById(R.id.listview);

How to make ListView items clickable?

I am wondering how to make ListView items clickable. And when pressed on item it sends you to another activity.
Here is my java coding:
public class Cantos extends AppCompatActivity {
ListView lv;
SearchView sv;
String[] cantos={"1: Abre Tu Oido", "2: A Cristo Quiero", "3: Acerquese Mi Clamor", "4: A Cristo Yo Alabare",
"5: Acude Dios", "6: Adelante", "7: A Dios Canto", "8: Adios Para Siempre", "9: Ahora Senor", "10: A Jesucristo Ven",
"11: Alabad A Dios" };
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cantos);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setLogo(R.mipmap.ic_launcher);
toolbar.setTitle("Cantos");
lv = (ListView) findViewById(R.id.listView);
sv = (SearchView) findViewById(R.id.searchView);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, cantos);
lv.setAdapter(adapter);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
return false;
}
#Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
}
}
Here is XML coding:
<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:background="#E0E0E0"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ccb.lldm.lldmhimnario.Cantos">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:elevation="25dp"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<SearchView
android:layout_width="match_parent"
android:id="#+id/searchView"
android:queryHint="Busque Canto..."
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</SearchView>
<ListView
android:layout_width="match_parent"
android:id="#+id/listView"
android:layout_height="wrap_content"
android:layout_below="#+id/searchView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true">
</ListView>
</RelativeLayout>
Thank You in advance.
P.s How can I change the font for the ListView items and color?
Thank you!
Use listview's OnItemClickListener
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// based on the item clicked go to the relevant activity
String clickedItem = (String)adapter.getItemAtPosition(position);
}
});
Use this code make clickable listview.
lv = (ListView) findViewById(R.id.listView);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView <? > arg0, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});

NullPointerException when trying to add a Navigation Drawer (Android)

I'm a bit stuck. I have been followng some tutorials and ended up making a DrawerLayout like below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:id="#+id/list"
android:entries="#array/menuitems"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left">
</ListView>
</android.support.v4.DrawerLayout>
I want this menu to slide in whenever the hamburger icon in my toolbar gets pressed, so I set an onClickListener in my Activity:
hamburgerIcon = (ImageView) findViewById(R.id.hamburger);
hamburgerIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] osArray = { "Bla", "Bla", "alB", "alB", "BLA" };
mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList = (ListView) findViewById(R.id.list);
mDrawerList.setAdapter(mAdapter);
}
};
I get a NullPointer when I try to initialise the ListView (mDrawerList) - and I'm certain that's because the Activity that I'm using uses a different layout to the navigation_drawer.xml, so it is unable to find the ListView in the xml. My question is, how can I use the navigation drawer layout using my main (different) layout?
Any tips are welcome, thanks.
EDIT:
I used this tag to include the DrawerLayout in the main layout
<include layout="#layout/navigation_drawer"
android:id="#+id/navdraw"/>
However, now I'm getting a RuntimeException when opening the main layout "Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v4.DrawerLayout" - then it complains about the setContentView(..) line in the main activity.
I have tried adding the dependency in the gradle script
compile 'com.android.support:support-v4:20.0.0'
But it's underlined in red and says "The support library should not use a lower version (20) than the targetSdkVersion (22)".
Any ideas?
hamburgerIcon = (ImageView) findViewById(R.id.hamburger);
mDrawerList = (ListView) findViewById(R.id.list);
hamburgerIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] osArray = { "Bla", "Bla", "alB", "alB", "BLA" };
mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
}
};
try again
It looks like the navigation_drawer.xml is not the main layout?
You can use the include tag and set the id. Then try findViewById(include_id).findViewById(R.id.list).

Populate ListView with List of Objects

I have list if twitter statuses and I want to populate it in a listview
public class TwitterTimeline extends Activity {
List<Status> statuses;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter_timeline);
setTitle("Timeline");
ListView listview = (ListView) findViewById(R.id.listView);
Bundle b = this.getIntent().getExtras();
if (b != null) {
MyStatuses myStatuses = (MyStatuses) b.getSerializable("statuses");
statuses = myStatuses.statuses;
for (Status st : statuses) {// this prints all the status on the catlog
Log.d("Abharthan : ", st.getUser().getScreenName()+ ":" + st.getText());
}
ArrayAdapter<MyStatuses> adapter = new ArrayAdapter<MyStatuses>(this,
android.R.layout.simple_list_item_1, statuses);// this line is giving error
listview.setAdapter(adapter);
}
}
Here MyStatus Class is:
import java.io.Serializable;
import java.util.List;
import twitter4j.Status;
public class MyStatuses implements Serializable {
List<Status> statuses;
}
The layout file containing list view is android_twitter_timeline.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.android.HomeWork3.TwitterTimeline">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
How can I populate my listview with twitter statuses using ArrayAdapter or any other method.
Basically, you will have to create an ArrayAdapter with your own data type. Inside, the adapter, you will populate the view for each row in the ListView.
Then, set that adapter to your ListView.
Please check out this guide Using an ArrayAdapter with ListView. And you will got to use "Using a Custom ArrayAdapter"
You can use a ArrayAdapter or BaseAdapter, here is a example with code, HERE
Please change the ArrayAdapter from MyStatuses to Status

Can't seem to use listview properly

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

Categories

Resources