android - listview and Adapter does not show any item:( - java

I have a problem in my Android application. I have a Custom ListView Adapter, but when I launch the application the list does not show any item!
My Code :
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class AccountContents extends Activity {
private ListView ls1;
String username = fourshared.username;
String password = fourshared.password;
private AccountItem[] rootContents;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootContents = fourshared.rootContents;
CArrayAdapter adapter = new CArrayAdapter(AccountContents.this, rootContents);
setContentView(R.layout.main);
ls1 = new ListView(AccountContents.this);
ls1.setAdapter(adapter);
}
}

Does your layout contain a list view? If so, you should look that up and set the adapter on that:
ls1 = (ListView)findViewById(R.id.your_list_view_id);
Better still, make your activity a ListActivity and Android will do a lot of the work for you.

You need to add the list to your activity. To do this, add this line:
setContentView(R.layout.main);
and remove: setContentView(R.layout.main);
This will add the list into view, but remove the existing views.
or you can define a list in your xml, and, as mentioned above, find it like this:
ls1 = (ListView)findViewById(R.id.your_list_view_id);
and in xml:
<ListView android:id="#+id/ls1" ......allOtherAtributesHere...... />

Related

ArrayAdapter in an onClickListner is not working

What I am trying to do here is to display a list of strings using ArrayList<String> and ArrayAdapter<String> when a user clicks a button. I want to declare all the members i.e. adapter, ArrayList and the list layout as global because I want to add more buttons later with the same feature of displaying a list of strings.
This code has no error but it's not working. I put the Toast in the onClick to make sure the onClick is working. I can see the toast but not the listView I want to see.
class file
R.id.button_news is the button id. R.layout.activity_primary_content is the layout that I'm using in this class PrimaryContent
R.layout.list_view_secondary is the layout where the listView R.id.list_view is located.
R.layout.list_view_secondary layout and the PrimaryContent class are not related but I want to use the listView which is in the list_view_secondary layout to display from the PrimaryContent.
In this line of code
arrayAdapter = new ArrayAdapter<>(view.getContext(),android.R.layout.simple_list_item_1,list); I tried by put the context view.getContext() and getBaseContext() both of them are not working.
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class PrimaryContent extends AppCompatActivity {
private final String LOG_TAG = PrimaryContent.class.getSimpleName();
public ListView listView;
public ArrayList<String> arrayList;
public ArrayAdapter<String> arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_primary_content);
final LayoutInflater factory = getLayoutInflater();
final View rootView = factory.inflate(R.layout.list_view_secondary, null);
listView = rootView.findViewById(R.id.list_view);
arrayList = new ArrayList<>();
for (int i=0; i<20; i++) {
arrayList.add("World News");
}
Button news = findViewById(R.id.button_news);
news.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getBaseContext(),"Damn",Toast.LENGTH_SHORT).show();
for (String x: arrayList) {
Log.v(LOG_TAG,x);
System.out.println();
}
arrayAdapter = new ArrayAdapter<>(view.getContext(),android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(arrayAdapter);
}
});
}
}
R.layout.list_view_secondary
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="true"
android:orientation="vertical" />
You forgot to add the inflated layout to your parent layout, for example:
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parent_layout);
parentLayout.addView(rootView);

Where is the id of a string array stored or where can i create one, IN ANDROID Programming?

I am learning Android programming from this tutorial about ArrayAdapters and ListViews.
The output does not show the string array values but instead shows: Item1, subitem1,, Item2,subitemm2...etc.
I want to know why did it happen? Do the values of string array need to be created separately or does Eclipse do that automatically? If so, where would I put/find the id mobile_list?
Here is the main activity class where string array declared:
package com.example.ListDisplay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListDisplay extends Activity {
// Array of strings...
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
}
}
Please refer to this question. You need to create your own class that extends from ArrayAdapter and override getView() so you can inflate your views accordingly and getCount() so the list view knows how many items are drawn. You are not supposed to use the default implementation of the ArrayAdapter. The default implementation and the way you are using it is taking as second parameter a TextView reference and you appear to be passing a layout. Refer to the official docs for this. That's why you need to create your own class
Change
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);
To
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);

How can i create different pages without using activities

I'm trying to create an app that has different pages for topics and questions. I have created about two activites and am about to create more, like 40 activities. How can I do this without creating up to that number of activities?
Here is my MainActivity.java codes
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declare the text view id
mListView = (ListView) findViewById(R.id.myList);
//Adding text to the array list
String booksArray[] = new String[]{"General Questions", "Mathematics",
"Physics",
"Chemistry",
"English"};
//Initialize the array list in the adapter
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout
.simple_list_item_1, booksArray);
mListView.setAdapter(adapter);
//Set the listener for the list view item
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//if the position of item clicked is 1, it should open
//another activity
if(position == 1 ){
conditionOfIf();
}
else{
//A short message that notify user for an error
Toast.makeText(getApplicationContext(), "Please click on the first objcet",
Toast.LENGTH_LONG)
.show();}
}
});
}
//The real code that open another activity called Topics
private void conditionOfIf(){
Intent intent = new Intent(this, Topics.class);
startActivity(intent);
}
}
Fragments is your answer. your can create 40 fragments and have only one activity for managing that fragments.
And the best thing about fragments is that you can use the same UI for multiple purpose i.e reuse-ability which might come handy as you have large number of layouts.
i think this will be a great Fragment tutorial to start with,it is easy to understand yet powerful enough to teach you the fundamentals.
use fragments.
For learning fragments go to google or youtube videos.
There are many online sources available.

Android cannot be resolved or is not a field

Alright so before you go and start stating, go look around as your title matches many questions asked here. And in that case, you're correct, but... Every single thing I've tried on these asked questions didn't help me at all.
So I'm actually trying to build an RssFeed application for Android devices (Still Learning).
My problem comes in the MainActivity, mind you. All my classes, xmls, and other content are properly coded with no issues or errors. People are saying that you should get rid of the imports that contain "android.r" and I've looked throught and haven't found any imports that contain the said quotation.
My MainActivity code:
package com.ascendapps.licknriff;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.ascendapps.licknriff.R;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
RssReader rssReader = new RssReader("http://www.licknriff.com/feed/");
ListView Items = (ListView)findViewById(R.id.listView1);
// create a list adapter
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this.android.R.layout.simple_list_item_1, RssReader.getItems());
// --
Items.setAdapter(adapter);
// --
Items.setOnItemClickListener(new ListListener(RssReader.getItems(), this)); }catch (Exception e){
Log.e("SimpleRssReader", e.getMessage());
}
}
}
Replace
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this.android.R.layout.simple_list_item_1, RssReader.getItems());
with
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this, android.R.layout.simple_list_item_1, RssReader.getItems());
android is the namespace of the Android R class. It doesn't belong to your activity class.
The first parameter is of Context type, so you activity is a suitable parameter as it inherits from Context.
Change this:
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this.android.R.layout.simple_list_item_1, RssReader.getItems());
to this:
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this, android.R.layout.simple_list_item_1, RssReader.getItems());
this should be passed as context in constructor of ArrayAdapter class.
You have a full-stop, but you need a comma, after this here -
// ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(
// this.android.R.layout.simple_list_item_1, RssReader.getItems());
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(
this, android.R.layout.simple_list_item_1, RssReader.getItems());
I think you have typo error. While writing code, you simply type . ( dot ) instead of ',' ( comma ). Just replace . with , after this like below,
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this, android.R.layout.simple_list_item_1, RssReader.getItems());

how to include list along side other UI elements? (Android)

I'm new to android developing, so i apologise if this is a simple/noob-ish question, and for any incorrect terminology.
but what i need to know is how can i include a list alongside of other UI elements (such as TextView, ImageView elements etc)
upto now, all i have been able to achieve is a list activity all on its own, which to do this i have been using the ListActivity class type.
My list activity:
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class ListViewExample extends ListActivity
{
String[] exampleList = {
"Item 1",
"Item 2",
"Item 3"
//etc etc
};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, exampleList));
}
}
Which is started within my Main class/activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class NewtestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent( this, ListViewExample.class));
}
}
but with the function of "startActivity()", this seems to just switch to that activity, and not "include" it to the current, which of-corse means that any elements within "R.layout.main" (defined above the calling of "startActivity()) are not shown.
Is there anyway to include this activity within my main activity?
or is there a better way of making a list?
(my goal will eventually be to make the list array dynamic, just thought id say in case that affected on any suggested solution).
thanks for any help (:
Using the startActivity to start your ListViewExample starts a whole new activity (with a whole new view) and puts it on top of the stack. When you click the back button, then your main activity will be displayed. Please see this link to learn more about the activity lifecycle.
It sounds like what you want to do is define some other UI elements alongside your listview. I dont know if you can do this on the SIDE, but I know you can include buttons/textviews on top or bottom of a listview. See this post as a good example of how to put a button below a listview.
EDIT: As an example (taken from the second link), you would do something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/testbutton"
android:text="#string/hello" android:layout_alignParentBottom="true" />
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/list"
android:layout_alignParentTop="true" android:layout_above="#id/testbutton" />
</RelativeLayout>
Now you could also put that button on bottom if you wanted, or include a textbox on the top and a button on bottom.
Yes as Espiandev said, you would want your main activity to extend Activity. Then in your XML you would have the above. The way you would get your listview to bind to would be
ListView lv = (ListView)findViewById(R.id.list);
Then you could bind to it:
lv.setAdapter(...)
An alternative to the other answer, which is probably more scalable, is to simply make ListViewExample extend a basic Activity. Then, in the onCreate() method, retrieve the ListView by using findViewById() and then use setAdapter() on this. For example, if your ListView was given the id listview1:
public class ListViewExample extends Activity {
String[] exampleList = {
"Item 1",
"Item 2",
"Item 3"
//etc etc
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
// Get an instance of your listview in code
ListView listview = (ListView) findViewById(R.id.listview1);
// Set the listview's adapter
listview.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, exampleList));
}
}
This will give you the flexibility to have a layout with more than just a listview in it

Categories