List View Problem - android

i have a problem in a list view.. i want to put different fields in the same listview is this possible?
like this?
DBAdapter db = new DBAdapter(.this);
db.open();
List<String> ct = new ArrayList<String>();
ct = db.getAllAccounts();
List<String> sd = new ArrayList<String>();
sd = db.getAllAmount();
Account = (ListView) findViewById(R.id.ListView_addAccount);
Account.setAdapter(new ArrayAdapter<String>(this,R.layout.list_contas_layout,R.id.text1,R.id.text2, ct,sd));
theres something like this to use?
sorry about my english;)

Absolutely. Take a look at SimpleCursorAdapter. I'm not sure what database you're using, but using the Cursor class is pretty normal for this sort of thing. Otherwise you're going to have to create your own custom adapter.

Related

Populating Spinner from SQLite database using CursorAdapter

I've looked at similar questions but they don't explain how to use a CursorAdapter.
What I'm trying to do is populate a Spinner with a column of Strings read from a selected database.
This is my current code:
//open database depending on selected item
myDatabase = openOrCreateDatabase("Courses"+spSem.getSelectedItem().toString(),MODE_PRIVATE,null);
//read fields to be populated and store them in an arraylist, I need to use arraylist since not every database has the same size
Cursor resultSet = myDatabase.rawQuery("Select * from Subject",null);
resultSet.moveToFirst();
List<String> course = new ArrayList<String>();
for (int i = 1; i < course.size(); i++) {
course.add(resultSet.getString(i));
}
This next part is where I'm stuck, I'm thinking I need to use CursorAdapter() to populate the spinner, but I have no idea how? Previously I had this:
String[] cArr = new String[course.size()];
course.toArray(cArr);
CursorAdapter a = new CursorAdapter(this,cArr,android.R.layout.simple_spinner_item);
a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCourse.setAdapter(a);
With this code I get the error "CursorAdapter is abstract and cannot be instantiated", and "cannot resolve method'setDropDownView'".

Implementing Manually Coded Auto-Incrementing RowId/Using VACUUM in SQLite

I have run into that timeless issue of wanting to have an auto-incrementing column (in SQLITE) that remains intact after a record is deleted. I have run into a number of posts about this problem, but none of them explain exactly how to code your own column for this purpose. I need the numbers to be consecutive because I want to be able to display a record number for each item in a ListView which will also serve as a running total of how many records are in the db.
So far, I have been trying to use the VACUUM function because it's my understanding that it will reset the numbers once it's complete, and I thought I would run it every time a record is deleted, but nothing seems to happen when the code is run. I'm not getting any errors, but the VACUUM isn't happening as far as I can tell. Here's the code:
String sql = "VACUUM;" ;
db.execSQL(sql,new String [] {sql});
Maybe nothing is happening because the coding is off? So I guess what I am getting at here is am I on the right track with trying to use VACUUM or should I go with manually setting up an auto-incrementing column? Either way, any advice is greatly appreciated!
Here's the fetch and display portion of the code:
populateListViewFromDB();
}
#SuppressWarnings("deprecation")
private void populateListViewFromDB() {
Cursor cursor = db.getAllRecords();
//Allow activity to manage cursor lifetime
startManagingCursor(cursor);
//Setup mapping from cursor to view fields:
String [] fromFieldNames = new String []
{DBAdapter.KEY_ROWID, DBAdapter.KEY_GENRE, DBAdapter.KEY_TITLE, DBAdapter.KEY_PLATFORM, DBAdapter.KEY_DATE, DBAdapter.KEY_PRICE, DBAdapter.KEY_WHEREBOUGHT};
int [] toViewIDs = new int []
{R.id.RecordNumberEdit, R.id.textView1, R.id.textView2, R.id.textView3, R.id.textView4, R.id.textView5, R.id.textView6};
//Create mapping from cursor to view fields
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(
this,
R.layout.database_view_layout,
cursor, //Set of DB records
fromFieldNames, //DB field Names
toViewIDs //View IDs in which to put info
);
//Set the adapter for the list view
ListView listViewFromDB = (ListView) findViewById(R.id. listViewFromDB);
listViewFromDB.setAdapter(myCursorAdapter);
I changed the VACCUUM code to the following since the original code was actually passing the query twice :
String sql = "VACUUM;" ;
db.execSQL(sql);
Still, though, nothing seems to be happening.

Why it doesn't work (string array)?

Hi I wanted to add new string to string array by this method but it doesn't work, because when I start new activity, then favaorites array isn't updated. Why ?
Resources res = getResources();
String[] favorites = res.getStringArray(R.array.favorites);
String[] planets = res.getStringArray(R.array.planets_array);
String[] temp = new String[favorites.length+1];
System.arraycopy(favorites,0,temp,0,favorites.length);
temp[favorites.length] = planets[mCounter];
favorites = temp;
In your case what you can do is use SharedPreferences to store the string into it. No array assigns requires and it is a much cleaner way to do it. Some links to get you started:
http://saigeethamn.blogspot.in/2009/10/shared-preferences-android-developer.html
http://saigeethamn.blogspot.in/2009/10/shared-preferences-android-developer.html
http://developer.android.com/guide/topics/data/data-storage.html
To solve your problem, you should be create an SQLite Database that houses all these properties. Then you should retrieve them from the Cursor (after you query your database) and use the results as necessary
Also another note is that you CANNOT add to the already defined R.array.* because its a precompiled resource.

Parsing XML into SQLite Database in Android

This is my first Android project. I am supposed to parse an rss feed from the internet and put it into a SQLite database for offline access to the feed's posts. I have followed the xml parsing tutorial by Coding Green Robots and I have followed a SQLite database tutorial on the Code Project, but in trying to put those together, I have run into problems.
The app builds on my Droid, but then it stops unexpectedly during this method in MainActivity:
private void displayPosts(ArrayList<Post> posts) {
//Create String Arrays to separate titles and dates
Log.d("CGRParser", "Displaying Post Titles To User");
ArrayList<String> post_headlines = new ArrayList<String>();
ArrayList<String> post_pubDates = new ArrayList<String>();
ArrayList<String> post_dbids = new ArrayList<String>();
ArrayList<String> post_contents = new ArrayList<String>();
//For every post in the ArrayList posts, it puts each attribute of the post into its own ArrayList
// This should be getting stuff from the database and putting it into an ArrayList
for (Post post : posts) {
Log.d("CGRParser", "Post Title: " + post.getHeadline());
post_headlines.add(post.getHeadline());
post_pubDates.add(post.getPubDate());
post_dbids.add(post.getDbid());
post_contents.add(post.getContents());
//it's breaking here
dbh.AddPost(post);
}
this.post_headlines = post_headlines;
this.post_pubDates = post_pubDates;
this.post_dbids = post_dbids;
this.post_contents = post_contents;
//Create a ListAdapter to Display the Titles in the ListView
ListAdapter adapter = new ArrayAdapter<String>(this, R.layout.episode_row, R.id.title, post_headlines);
listview_posts.setAdapter(adapter);
//Set Progress Bar Invisible since we are done with it
progress_bar.setVisibility(ProgressBar.INVISIBLE);
}
I retained the functionality from the original tutorial. All I changed was adding dbh.AddPost(post);. dbh is a DatabaseHelper object declared in MainActivity. Each of those ArrayLists are declared in MainActivity, but they are also declared in the method. When I debug it and it gets to dbh.AddPost(post); ThreadPoolExecutor.class runs and says "Source not found. The JAR file /Users/Zach/Desktop/android-sdj-mac_x86/platforms/android-7/android.jar has no source attachment. You can attach the source by clicking Attach Source below."
My main question is what am I doing wrong here? I don't get any errors from this, and I don't understand what it means by source attachment. I feel that there is a reason the ArrayLists are declared in the method as well as MainActivity, and that I should be doing the same for dbh, but I am not sure why. Regardless, making a databaseHelper in the method wouldn't work in the long run, as the application will need to bring in new posts from the feed, and setting the old database to the new one with only the new posts would not work.
EDIT: Here's the AddPost method in DatabaseHelper:
void AddPost(Post post){
SQLiteDatabase db= this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(colDbid, post.getDbid());
cv.put(colType, post.getType());
cv.put(colSubType, post.getSubType());
cv.put(colHeadline, post.getHeadline());
cv.put(colContents, post.getContents());
cv.put(colUrl, post.getUrl());
cv.put(colImgUrl, post.getImgUrl());
cv.put(colVidUrl, post.getVidUrl());
cv.put(colPubDate, post.getPubDate());
db.insert(postTable, colHeadline, cv);
db.close();
}

ListView with Array Issue

What I'm doing is wanting to start off my ListView with 0 rows, download a list from the Internet, then populate the ListView with the downloaded contents. Seems easy enough, but arrays are proving to be an issue.
What's happening is that I'm declaring my array in the only way ListView seems to want it
static String[] CONTENTS = new String[]{};
so that it populates with no rows. After downloading the contents through a thread, I then go to fill the array with what I've downloaded. The problem is that it doesn't want to fill the array; if I initiate the array with one empty string, it will only let me fill it with exactly one row (so if I give it an array with more than one object, it doesn't do anything). How can I expand on the array the ListView is taking data from, so I can fill it with more than the number of objects I've declared it with?
The closest I've been able to come was through the code
private String[] expand(String[] array, int size) {
String[] temp = new String[size];
System.arraycopy(array, 0, temp, 0, array.length);
for(int j = array.length; j < size; j++)
temp[j] = "";
return temp;
}
to expand the array and giving it the number of objects downloaded, but that still doesn't seem to work.
Thanks in advance!
Don't use arrays, use some implementation of List. (ArrayList will do the work). Collections are far superior than old-school arrays.
//List
list = new ArrayList();
//BaseAdapter
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
//ListView
listView = (ListView) findViewByID(R.id.listview);
listView.setAdapter(adapter);
And when you add/remove elements to/from the list, you must invoke
adapter.notifyDataSetChanged();
How can I expand on the array ...
I assume you are using an ArrayAdapter. Instead of using a fixed-size array, use a dynamically-resizable ArrayList to back it using one of the ArrayAdapter constructors that take a List argument.

Categories

Resources