ListView not appearing in application - android

im trying to populate my listview beneath my tab function but for some reason its not appearing in my app. I Sampled the code in a external project and it worked so Im not sure why it is not appearing. Have I implemented the activity wrong in the manifest or do I have contrary codes. No errors or logcat issues when the button is selected. Its just a blank page beneath the tab. The page loads but with no list. Thanks
XML File
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<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>
ListmoreActivity
package workout.fitty;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class ListmoreActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.main, R.id.label, adobe_products));
}
}
The Activity in the manifest is added like;
<activity android:name=".ListmoreActivity" />
Just in case its wrong

You are wrong from the inception of the code refer the link below to learn listview in android...
http://www.vogella.de/articles/AndroidListView/article.html

Related

How to make it easy to select a database?

I am new to stack, but I figured I need help. I hope someone can help me :).
What I want to do is the following:
the user of the App is saving some database (db3) files via PC in a folder he has to create. In my case he needs to call it "My Custom Foler".
implement a Spinner which lets the user choose from all db3 files of this directory and then be able to do db_selected.getReadableDatabase
Just to put it in context: I am gonna implement a ContenProvider and the populate a ListView with Items from specific table using SQliteOpenHelper and LoaderManager.LoaderCallbacks<Cursor>. BUT THAT IS NOT MY QUESTION.
I want to setup this spinner. Maybe someone can help?
Here is my code so far with still a lot missing but the basic structure should be clear.
Greetings :)
I added:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
In 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"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list">
</ListView>
<LinearLayout
android:id="#+id/login_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="8dp">
<Spinner
android:id="#+id/select_database"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
MainActivity.class
public class MainActivity extends AppCompatActivity {
private final String DATABASE_PATH = "My Custom Folder";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//either we show login page or the populated ListView
ListView populatedListView = (ListView) findViewById(R.id.list);
View logInView = findViewById(R.id.login_view);
populatedListView.setEmptyView(logInView);
//How to setup a Spinner which lets select a ".db3" from a custom created Folder
Spinner spinner = findViewById(R.id.select_database);
//When Spinner item is clicked/touched I am gonna load it into my ListView
}
}
}

Android app crashes with weird error

I have button that launches a ListActivity, but every time I click on it, the app crashes with the following LogCat error:
01-10 13:12:51.327: E/AndroidRuntime(2970): FATAL EXCEPTION: main
01-10 13:12:51.327: E/AndroidRuntime(2970): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.Settings}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
This is the xml code:
<?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:background="#color/background"
android:orientation="vertical"
android:padding="15dip" >
<ListView
android:id="#+id/listViewSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And this is the Java code:
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class Settings extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
ArrayList<String> listItems=new ArrayList<String>();
listItems.add("foo");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
}
I have already tried changing the list id to list but if I do this I get the following error:
If I do this I get the following error: `Error: No resource found that matches the given name (at 'id' with value '#id/list').`
ListActivity does not require that you assign a layout to it via the setContentView() method, if you only want to show a ListView ListActivity contains by default a ListView.
In case you need to include more Views than a ListView in your ListActivity you can still assign a layout to your Activity. In this case your layout must contain a ListView with the android:id attribute set to #android:id/list.
So change your list id to:
android:id="#android:id/list"
This row in your XML:
android:id="#+id/listViewSettings"
...has to be like this:
android:id="#android:id/list"
Why? Well, the ListActivity requires you to use the id "android.R.id.list" if you want it to maintain your ListView (same thing applies when using a ListFragment).
Simply change your xml ListView-id
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
In this case your layout must contain a ListView with the android:id attribute set to #android:id/list

Android Null Pointer Exception on OnClickListener line

I have a simple test application with a GridView containing an array of buttons, I'm trying to perform an operation (any operation) touching one of these buttons.
The main code is this:
package com.example.convert;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
public class convert extends Activity {
//private ListView List;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String [] elenco = {
"ciao","questo","รจ","un esempio","Configurazione"
};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,R.layout.oggetto,R.id.testogg,elenco);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(arrayAdapter);
Button btn = (Button) (findViewById(R.id.testogg));
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
setContentView(R.layout.oggetto);
}
});
}
}
The main.xml is the following:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
And oggetto.xml contains the following lines:
<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:text="#+id/TextView01"
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/testogg">
</Button>
But when I try to run the application it return me a NullPointerException on the btn.setOnClickListener(new OnClickListener()) line.
May be that happen because the buttons have the same id? If this is the case, how can I obtain repeated buttons with different ids?
Thanks for your help!
May be that happen cause the buttons
have the same id
That could be. But it also shows something is wrong if that's even the case. Each button specified in XML is only created once. You can't create multiple buttons just by calling findViewById() multiple times or by passing in the resource ID.
But the problem comes because you call setContentView(R.layout.main);
findViewById() will ONLY return non-null views for views contained in main.xml. Your context knows nothing about your other XML layouts because they're not inflated.
You should probably start with simpler programs and go through the tutorials to get a feeling for how android views work.
What are you trying to accomplish? You're originally setting a layout that only contains a GridView, then you're trying to find a Button that doesn't even exist (at the faulting line, your content view is your main.xml document, which does not contain any buttons, let alone one with the id of testogg). This means that btn is null, as the button does not exist. That is the cause of your NullPointerException.
If you want to intercept a click event for an item in your GridView, you need to be using the OnItemClickListener, and attaching that to your GridView. See the link for the onItemClick method definition.

Android AutoCompleteTextView onClick problem

I've created an AutoCompleteTextView to search through a list of course titles (obtained from an sqlite db) and what I want to do is, when the user clicks on a title from the drop-down menu, the whole information from the database about his selection appears in a text view created below the AutoCompleteTextView.
I am pretty new to programming, especially for android and I would really appreciate it if someone could explain me how exactly to use setOnItemClickListener to call an instance in the database in the TextView below.
The code for the layout (R.layout.main_courses) is
<?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="wrap_content"
android:padding="5dp">
<AutoCompleteTextView
android:id="#+id/autocomplete_course"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search for a course"/>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/autocomplete_course"
android:hint="Information about the course will appear here" />
</RelativeLayout>
and the code for the AutoCompleteTextView I've written so far is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_courses);
DataBase db = new DataBase(this.getApplicationContext());
db.openDataBase();
ArrayList<String> aCourses = db.getCoursesArr();
db.close();
AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.autocomplete_course);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_courses, aCourses);
search.setAdapter(adapter);
}
First of all you should try using a CursorAdapter instead of getting an array from it. Check this link for more info.
There is a method in AutoCompleteTextView that let you decide how many letters the user must type before the dropdown is shown, setThreshold. The issue is that it only allows >=1 values.
If you check this class src code, the good news is that the variable set by setThreshold() is only used in this method:
public boolean enoughToFilter() {
return getText().length() >= mThreshold;
}
So the first thing I would try is extending AutoCompleteTextView and override that method to always return true.
NOTE: Keep in mind that this might change in the future and it can get broken.

Android Binding from SQLite to Gallery

I am just starting out with Android and I am attempting my first test app, but I am a bit stuck.
I have an SQLite database and a user is selecting a picture and a reference to this a long with a short description is getting stored in the database.
This is all working fine, now I want to bind the images from to database to a gallery, but I am a bit confused how I do this.
My layout xml is as follows:
<?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">
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Weeksgallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text1"
android:layout_marginLeft="10dip"
android:text="Take a Picture"
android:onClick="TakePicture" />
</LinearLayout>
I want the gallery with a button below
My on create function I have the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDbHelper = new DbAdapter(this);
mDbHelper.open();
Cursor cur = mDbHelper.fetchDaysImages();
//array of fields to display
String[] from = new String[] { mDbHelper.KEY_ROWID,mDbHelper.KEY_PICREF};
int[] to = new int[] { R.id.Weeksgallery };
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter images = new SimpleCursorAdapter(this,
R.layout.main, cur, from, to);
setListAdapter(images);
mDbHelper.close();
}
I am using the notepad tutorial as an example and my class extends ListActivity.
When I run it I get the error
Your content must have a ListView whose id attribute is 'android.R.id.list'
I'm assuming I am using the wrong type of binding, but I can't seem to find anywhere that tells me how to bind from an sqlite DB to a gallery.
I also want to add a reference to the rowId in the gallery so when the image is clicked it will open the relevant page (so it can show the message)
Can anyone help?
Thanks
Bex
Your Activity inherits ListActivity, right? When you use ListActivity, you must make sure there is a ListView whose id is "android.R.id.list" in the layout of your Activity, that is what the log told you.
If there is no ListView in your Activity, then don't use ListActivity. If you want to set adapter to Gallery, use Gallery`s setAdapter method.

Categories

Resources