Database listview how to relate to the DB record

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Re: Database listview how to relate to the DB record

Postby blundell » Sun Dec 05, 2010 9:13 pm

You call

adapter=new DatabaseAdapter( this, R.layout.listrow, listResults);

instead of

adapter=new ArrayAdapter<String>( this,android.R.layout.simple_list_item_1,results);

That error must be a syntax error, or try Menu > project > CLEAN

If you can't get your implementation to work please start again with the orject class's I sent you as these are nice an clean and working!
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Re: Database listview how to relate to the DB record

Postby Mark Worsnop » Sun Dec 05, 2010 9:23 pm

I got it to show the list!! THANK YOU SO VERY MUCH FOR YOUR HELP!!!

Now I need to get the ID from the list, ;)

can you please toss me one more couple lines of code?

I know how to get the click and get to the sub that handles it.

I might be able to figure this one :)
Mark Worsnop
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Dec 01, 2010 12:34 am

Re: Database listview how to relate to the DB record

Postby blundell » Sun Dec 05, 2010 9:38 pm

You can get the ID easily, have a look at the onClickListener that in the project I sent you

Code: Select all
@Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      System.out.println("You have pressed ID: "+results.get(position).getId()+" Desc: "+results.get(position).getDescription());
   }
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Database listview how to relate to the DB record

Postby Mark Worsnop » Sun Dec 05, 2010 10:21 pm

Is there some quick way to add functions like the OnItemClick or do you just have to know the syntax?
Mark Worsnop
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Dec 01, 2010 12:34 am

Re: Database listview how to relate to the DB record

Postby blundell » Sun Dec 05, 2010 10:33 pm

if you write that your class implements OnClickListener, your class name will get a red underline and you can click 'add unimplemented methods'. But these come like this:

public void onItemClick(AdapterView<?> arg1, View arg1, int arg3, long arg4) {

so to get the correct method paramater names you jsut put "android onItemClick" into google find the API and copy an past it from there :-)

onItemClick(AdapterView<?> parent, View view, int position, long id)
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Database listview how to relate to the DB record

Postby Mark Worsnop » Sun Dec 05, 2010 10:42 pm

Thanks again... I dont know how to say thank you enough. I would have been here for days trying to figure this out!

Seems like I need a class for every table I am working with, does that sound right?

How about the DatabaseAdapter class. Can I pass MyObj to the class so I can use the same class for the other tables?
Mark Worsnop
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Dec 01, 2010 12:34 am

Top

Re: Database listview how to relate to the DB record

Postby blundell » Sun Dec 05, 2010 11:51 pm

Yeah if you make an object that's generic enough, that your happy with you can use one for all of them, or one for two and two others or one for each :-) that's the joy of OO programming.

Just try to give all your class's sensible names, that relate to what they are there for and what they do. It gives you a reminder and helps with your organisation as well.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Database listview how to relate to the DB record

Postby Mark Worsnop » Sun Dec 05, 2010 11:55 pm

OK I have my list up and I can click and get the ID. Now I open another class that displays the details of the record, but I need to pass the ID to the new class. I think what I have below is 2 instances of the class, one is the re that I declared and another one when the startActivity happens. I need the ID to go too the class before the class opens so it knows what record # to display


Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1.  
  2.   Rec re = new Rec();
  3.  .
  4. .
  5. .
  6. .
  7.  
  8.  @Override
  9.        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  10.                  
  11.          
  12.           Intent intent = new Intent(view.getContext(), FoodList.class);
  13.          
  14.           startActivity(intent);
  15.  
  16.        re.ShowRecord(23); // Hard code rec 23 for now
  17.        
  18.        
  19.       }
  20.      
  21.        };
  22.      
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Mark Worsnop
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Dec 01, 2010 12:34 am

Re: Database listview how to relate to the DB record

Postby blundell » Mon Dec 06, 2010 12:37 am

You shouldnt use shorthand variable names like re and rec, it will confuse you when you come back later and confuses those who help you :-) this is a new topic, pls start a new thread. This time in the right category!
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Database listview how to relate to the DB record

Postby blundell » Mon Dec 06, 2010 2:25 pm

Lol, that attachment I uploaded, did it make any sense?! I think I uploaded the wrong file! I'll upload the correct one tonight!
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Database listview how to relate to the DB record

Postby Mark Worsnop » Mon Dec 06, 2010 2:44 pm

HAHA! I was trying to understand why it didnt seem to relate to the discussion at all :) I put it on the side as I thought I just didnt know enough yet!

On the code below,
public class DatabaseAdapter extends ArrayAdapter<Grp>

I can see that I pass the results to this class in the 3rd parameter, but the declaration has <Grp> when doesnt seem to be a generic ArrayList. So even though I pass results why does the declaration have GRP?

Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1.  
  2. public class DatabaseAdapter extends ArrayAdapter<Grp> {
  3.    
  4.     private Context mContext;
  5.     private ArrayList<Grp> mItems;
  6.     private int mXmlId;
  7.            
  8.     public DatabaseAdapter(Context context, int textViewResourceId, ArrayList<Grp> items) {
  9.             super(context, textViewResourceId, items);
  10.             this.mContext = context;
  11.             this.mItems = items;
  12.             this.mXmlId = textViewResourceId;
  13.     }
  14.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Mark Worsnop
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Dec 01, 2010 12:34 am

Re: Database listview how to relate to the DB record

Postby blundell » Mon Dec 06, 2010 2:58 pm

Because the declaration is basically saying 'this is going to be an ArrayList with the Type Grp inside it'.

Ha glad you got it working, it'll be so much clearer when I upload the project
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Database listview how to relate to the DB record

Postby Mark Worsnop » Mon Dec 06, 2010 3:11 pm

But the type grp class can be different for different tables in the DB. If I always arrange for an ID and DESCRIPTION then that makes sense, but what happens if they are not those fields. So if I pass a results that has a different structure than grp does, will that still work?
Mark Worsnop
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Dec 01, 2010 12:34 am

Re: Database listview how to relate to the DB record

Postby blundell » Mon Dec 06, 2010 3:52 pm

Ah I see, try

private ArrayList<?> mItems;
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Database listview how to relate to the DB record

Postby Mark Worsnop » Mon Dec 06, 2010 6:07 pm

Ha! I was guessing that is what I needed ;) SO the ? is like a wild card?
Mark Worsnop
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Dec 01, 2010 12:34 am

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: Google Feedfetcher and 3 guests