-1

私がしたいことは、リストフラグメントを使用してリストビューにすべてのデータを表示することです。ソースコードをダウンロードして編集し、目的の出力を得ました。ここに私のコードがあります:

    List<ListSql> results = new ArrayList<ListSql>();

    setListAdapter(new SqlParser(getActivity(),results));

これは私のlistsqlです:

public class ListSql {


    private String Fname;
    private String Fpass;
    private ArrayList<String> arList;

    private Context myContext;
    private List<ListSql> items;
    private LayoutInflater  mInflater;
    private DBhelper myHelper;
    private SQLiteDatabase myDbase;

    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "_persons_name";
    public static final String KEY_HOTNESS = "person_hotness";

    private static final String DATABASE_NAME = "HotOrNotdb";
    private static final String DATABASE_TABLE = "peopleTable";
    private static final int DATABASE_VERSION = 1;


    public static class DBhelper extends SQLiteOpenHelper{

        public DBhelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(
                    "Create Table " + DATABASE_TABLE + " (" + 
                    KEY_ROWID + " Integer PRIMARY KEY AUTOINCREMENT, " + 
                    KEY_NAME + " TEXT NOT NULL, " + 
                    KEY_HOTNESS + " TEXT NOT NULL);"
                    );

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
            onCreate(db);
        }

    }


    public ListSql(Context context){

        this.arList = Listme(arList);
        this.myContext = context;
    }

    public String getName(){
        return Fname;
    }

    public String getPass(){

        return Fpass;
    }

    public ArrayList<String> getArr(){

        return arList;
    }



    public ListSql open(){
        myHelper = new DBhelper(myContext);
        myDbase = myHelper.getWritableDatabase();
        Log.i("open","open");
        return this;
    }
    public void close(){
        myHelper.close();

    }


    public ArrayList<String> Listme(ArrayList<String> arr){
        String sql = "select * from " + DATABASE_TABLE;
        Cursor c = myDbase.rawQuery(sql, null);
        if(c !=null){
            if  (c.moveToFirst()) {
                do {
                    String firstName = c.getString(c.getColumnIndex(KEY_NAME));
                    String pass = c.getString(c.getColumnIndex(KEY_HOTNESS));
                    arr.add("username " + firstName + ",  Password: " + pass);
                }while (c.moveToNext());
            } 
            Log.i("hahahaha","hehe");
            return arr;

        }
        Log.i("hahahaha","not here");
        return null;

    }




}

そして私のベースアダプター:

public class SqlParser extends BaseAdapter {

    private Context myContext;
    private List<ListSql> items;
    private LayoutInflater  mInflater;
    private DBhelper myHelper;
    private SQLiteDatabase myDbase;

    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "_persons_name";
    public static final String KEY_HOTNESS = "person_hotness";

    private static final String DATABASE_NAME = "HotOrNotdb";
    private static final String DATABASE_TABLE = "peopleTable";
    private static final int DATABASE_VERSION = 1;


    public SqlParser(Context context ,List<ListSql> items){
        this.myContext = context;
        this.items = items;
    }


    private class ViewHolder {
        public TextView textView;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ArrayList<String> arr = new ArrayList<String>();
        View view = convertView;
        ViewHolder viewHolder;
        TextView namef ;
        ImageView prof;
        Bitmap bMap;
        if (view == null) {
            view = mInflater.inflate(R.layout.listinflate, parent, false);

            /**
            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) view.findViewById(R.id.textView1);
            view.setTag(viewHolder); **/


            /*** You can do this manualy without using holder ***/
             namef = (TextView)view.findViewById(R.id.textView1);

            /*** You can do this manualy on setting the tag to individual components rather than using holder  ***/ 
            view.setTag(namef);

        }else {
            viewHolder = (ViewHolder) view.getTag();
            namef = (TextView) view.getTag();

        }
        namef.setText(items.get(position).Listme(arr).get(position).toString());

        return view;
    }
}

問題は、リストビューにデータが表示されないことです。私はsqliteからデータを取得していると確信していますが、それを管理してリストビューに表示することはできません..

4

4 に答える 4

2

getCount() で 0 を返しています。

  public int getCount() {
    // TODO Auto-generated method stub
    return 0;
}

0 を返す場合、利用可能なデータがないことを意味します。ここでは、実際のデータ長を渡す必要があります。

おそらくあなたの場合、リストサイズを渡す必要があります。

public int getCount() {
        // TODO Auto-generated method stub
        items.size();
    }
于 2012-07-03T10:02:53.027 に答える
1
public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

ここにあなたの問題があります。getCount()表示するデータセットのサイズを返す必要があります。0 を返すため、getView()コールバックは呼び出されません。

変更してください

   public int getCount() {
            // TODO Auto-generated method stub
            return (items == null) ? 0 : items .size();
   }
于 2012-07-03T10:03:02.573 に答える
0

私はそれを手に入れました..私は私のListSqlをこれに変更します:

public class ListSql {


    private String Fname;
    private String Fpass;
    private ArrayList<String> arList;

    private Context myContext;
    private List<ListSql> items;
    private LayoutInflater  mInflater;
    private DBhelper myHelper;
    private SQLiteDatabase myDbase;

    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "_persons_name";
    public static final String KEY_HOTNESS = "person_hotness";

    private static final String DATABASE_NAME = "HotOrNotdb";
    private static final String DATABASE_TABLE = "peopleTable";
    private static final int DATABASE_VERSION = 1;


    public static class DBhelper extends SQLiteOpenHelper{

        public DBhelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(
                    "Create Table " + DATABASE_TABLE + " (" + 
                    KEY_ROWID + " Integer PRIMARY KEY AUTOINCREMENT, " + 
                    KEY_NAME + " TEXT NOT NULL, " + 
                    KEY_HOTNESS + " TEXT NOT NULL);"
                    );

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
            onCreate(db);
        }

    }


    public ListSql(Context context, ArrayList<String> arList){

        this.arList = Listme(arList);
        this.myContext = context;
    }

    public String getName(){
        return Fname;
    }

    public String getPass(){

        return Fpass;
    }

    public ArrayList<String> getArr(ArrayList<String> arr){

        return arr;
    }



    public ListSql open(){
        myHelper = new DBhelper(myContext);
        myDbase = myHelper.getWritableDatabase();
        Log.i("open","open");
        return this;
    }
    public void close(){
        myHelper.close();

    }


    public ArrayList<String> Listme(ArrayList<String> arr){
        String sql = "select * from " + DATABASE_TABLE;
        Cursor c = myDbase.rawQuery(sql, null);
        if(c !=null){
            if  (c.moveToFirst()) {
                do {
                    String firstName = c.getString(c.getColumnIndex(KEY_NAME));
                    String pass = c.getString(c.getColumnIndex(KEY_HOTNESS));
                    arr.add("username " + firstName + ",  Password: " + pass);
                    getArr(arr);
                }while (c.moveToNext());
            } 
            Log.i("hahahaha","hehe");
            return getArr(arr);

        }
        Log.i("hahahaha","not here");
        return null;

    }

そしてこれに対する私のBaseAdapter:

public class SqlParser extends BaseAdapter {

    private Context myContext;
    private ArrayList<String> items;
    private LayoutInflater  mInflater;
    private DBhelper myHelper;
    private SQLiteDatabase myDbase;

    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "_persons_name";
    public static final String KEY_HOTNESS = "person_hotness";

    private static final String DATABASE_NAME = "HotOrNotdb";
    private static final String DATABASE_TABLE = "peopleTable";
    private static final int DATABASE_VERSION = 1;


    public SqlParser(Context context ,ArrayList<String> arr){
        this.myContext = context;
        this.items = arr;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    private class ViewHolder {
        public TextView textView;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return this.items.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return this.items.get(position);
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ArrayList<String> arr = new ArrayList<String>();
        View view = convertView;
        ViewHolder viewHolder;
        TextView namef ;
        ImageView prof;
        Bitmap bMap;
        if (view == null) {
            view = mInflater.inflate(R.layout.listinflate, parent, false);

            /**
            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) view.findViewById(R.id.textView1);
            view.setTag(viewHolder); **/


            /*** You can do this manualy without using holder ***/
             namef = (TextView)view.findViewById(R.id.textView1);

            /*** You can do this manualy on setting the tag to individual components rather than using holder  ***/ 
            view.setTag(namef);

        }else {
            viewHolder = (ViewHolder) view.getTag();
            namef = (TextView) view.getTag();

        }
        namef.setText(items.get(position));

        return view;
    }
}
于 2012-07-04T02:27:11.327 に答える
0

ここでは CursorAdaptor の方がはるかにうまく機能しますが、質問への回答として、次の関数を実装する必要があります。そうしないと、アダプターはリストに 0 個のアイテムがあると見なします

public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

getCount() でリスト サイズ、getItem() で実際のリスト アイテム、getItemId() で適切な値を返す必要があります。

于 2012-07-03T10:02:01.420 に答える