2

I'm doing an admin page that can view the users that exist in the system. My idea is to show a list of the users and the details in one page. I heard that using listview is very tedious. Is there any other way that is easy?

Maybe view in textview but I dont know how it works. I must put in a lot of textview on my xml?

A simple task that I need to know how to do. I just need to know how to view datas in a page, using any method.

Thank you very much! Hope to get responds soooon! =D

Dayne


Hello again

Here's how a listview looks like:

package log1.log2;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class AdminMain extends ListActivity {


static final String[] COUNTRIES = new String[] {
"Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands","Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru"
  };


@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

  ListView lv = getListView();
  lv.setTextFilterEnabled(true);

  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, 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();
    }
  });
}

For the code above, the data is written in the codes itself. My data is in my database and I need it to be shown on the listview. I do not know how to do it. I tried a few ideas I had:

public class AdminMain extends ListActivity {

    DBAdapter db = new DBAdapter(this);

    private String TEST;




    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          db.open();
          Cursor c = (Cursor) db.getAllUser();  
          final String[] COUNTRIES = new String[] {

            };


        TEST=c.getString(1).toString();

          setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, TEST));
          getListView().setTextFilterEnabled(true);

    }

}

I declare a string and I tried retrieving data from database and save it to the string which is TEST. And then I tried showing it on the listview :

setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, TEST));
              getListView().setTextFilterEnabled(true);

But I cant put 'TEST' in the listview. The error, The constructor ArrayAdapter<String>(AdminMain, int, String) is undefined. will appear.

I hope somebody will give me a good answer for this. I'm looking forward to your replies. Thank you for taking your time reading this.

-Dayne


Hello Sephy

Here's what I edited.

public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          db.open();
          Cursor c = (Cursor) db.getAllUser();  
          /*final String[] COUNTRIES = new String[] {

            };*/

          final String[] TEST = c.getString(1);

        //TEST[]=c.getString(1).toString();

          setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1));
          getListView().setTextFilterEnabled(true);

    }

As you can see I tried to put data from my database straight into the array but it can't work; final String[] TEST = c.getString(1);. The error message Type mismatch: cannot convert from String to String[] appeared.

3
  • Indeed, It's going bad because you try to put a String in something that's supposed to take an Array of String. Create a new ArrayList of String for instance and put it in your adapter instead of TEST. How many users do you have in your database? Commented Aug 13, 2010 at 8:31
  • There's no limit number of users you see.... people can just come and create a new account.. :/ Commented Aug 16, 2010 at 1:09
  • I edited after reading your kind comment Sephy... read my question above. Edited. Still can't work hough.. :/ Commented Aug 16, 2010 at 1:26

1 Answer 1

1

ListView is tedious to use, but sometimes, if for instance your dataset is quite big and repetitive, you might want to consider using ListView.
If you still don't want to, you can create a ScrollView plus a header in a TextView in your xml for instance, and then in your java, you use a for loop or a while to loop on your database result, and every time you have one set of data ready, you create a new TextView and add it to the ScroolView. It would be something like that :

if (yourCursor.moveToFirst()) {
do {
      //prepare your data       
      TextView tv = new TextView(yourContext);
      tv.setText("yourData");
      // you can set id, tag, text color, font, ...
      yourScrollView.addView(tv);
    } while (yourCursor.moveToNext());
}
yourCursor.close();
yourDB.close();
Sign up to request clarification or add additional context in comments.

1 Comment

I have the code for listview but it;s hardcoded. Can you help me with that? I need to retrieve data from database and show it in the listview instead of hardcoding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.