开发者

Android listview adapter show text from sqlite

开发者 https://www.devze.com 2023-04-10 20:14 出处:网络
I need a little help with my custom list view adapter. I\'m using the adapter example from vogella.de, but I need to find a way how to set text and image from sqlite database. Here is the adapter code

I need a little help with my custom list view adapter. I'm using the adapter example from vogella.de, but I need to find a way how to set text and image from sqlite database. Here is the adapter code which I am using :

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class My开发者_如何学编程ArrayAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] names;
    Cursor cursor;

    public MyArrayAdapter(Activity context, String[] names) {
        super(context, R.layout.main_listview, names);
        this.context = context;
        this.names = names;
    }

    // static to save the reference to the outer class and to avoid access to
    // any members of the containing class
    static class ViewHolder {
        public ImageView imageView;
        public TextView textView,textView2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // ViewHolder will buffer the assess to the individual fields of the row
        // layout

        ViewHolder holder;
        // Recycle existing view if passed as parameter
        // This will save memory and time on Android
        // This only works if the base layout for all classes are the same
        View rowView = convertView;
        if (rowView == null) {

            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.main_listview, null, true);

            holder = new ViewHolder();
            holder.textView = (TextView) rowView.findViewById(R.id.main_name);
            holder.textView2 = (TextView) rowView.findViewById(R.id.main_info);
            holder.imageView = (ImageView) rowView.findViewById(R.id.main_img);
            rowView.setTag(holder);

        } else {
            holder = (ViewHolder) rowView.getTag();
        }
        final Bitmap b = BitmapFactory.decodeFile("/sdcard/52dde26940e0d3081f6a086d4b54cd1c.jpg", null);

        holder.textView.setText("");
        holder.textView2.setText("");
        holder.imageView.setImageBitmap(b);


        return rowView;
    }

    public String[] getNames() {
        return names;
    }
}

And I'm trying to set the text to a text view like this :

 String sql = "SELECT title FROM collections";
        Cursor cursorTitle = userDbHelper.executeSQLQuery(sql);
        if(cursorTitle.getCount()==0){
            Log.i("Cursor Null","CURSOR TITLE NULL");
        } else if(cursorTitle.getCount()>0){
            cursorTitle.moveToFirst();
            String text = cursorTitle.getString(cursorTitle.getColumnIndex("title"));
            Log.i("title text","title text : "+text);
            String[] names = new String[] { text };
            listView.setAdapter(new MyArrayAdapter(this, names));
 }

,but when I run that code I don't get anything in my listview as a result.

So can anyone suggest me how can I set the text and image in imageview and textview in my activity using this adapter.

Thanks a lot!


Check this out

String text = cursorTitle.getString(cursorTitle.getColumnIndex("title"));
Log.i("title text","title text : "+text);
String[] names = new String[] { text };

What's the result of 'text' in LogCat? Any value stored in 'names' array?


Where did you use the String array in adapter,you passed to it? i think,you set null in your both textviews. You need to use it like holder.textview.setText(names[position]) in your adapter. And second thing,you should use for loop for creating your string array from cursor.It seems like you will always get just the first result of your cursor data.


Try to do something like this :

MyAddapterClass :

    private final Activity context;
    private final String[] names;
    private final Bitmap image;
    private final String text;
    private final String text2;
    Cursor cursor;

    public MyArrayAdapter(Activity context, String[] names, Bitmap image, String text, String text2) {
        super(context, R.layout.main_listview, names);
        this.context = context;
        this.names = names;
        this.image = image;
        this.text = text;
        this.text2 = text2;
    }

and set image, text and text2 to your holders :

    holder.textView.setText(text);
    holder.textView2.setText(text2);
    holder.imageView.setImageBitmap(image);

and initialize it in your Activity.That's it.

Hope it helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消