2012-07-03 61 views
-1

我想要發生的是我將使用列表片段在列表視圖中顯示所有數據。我下載了一些源代碼並進行編輯以獲得我想要的輸出。繼承人我的代碼:當使用BaseAdapter子類時,ListView不顯示任何數據

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; 

    } 




} 

和我baseadapter:

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檢索數據,但我不能管理它顯示在我的列表視圖..

+0

你爲什麼不使用ArrayAdapter或SimpleCursorAdapter? – Ran

+0

@millimoose ..我明白代碼。它適用於靜態變量。但在獲取數據庫的價值和顯示它。它不工作。我只是一個初學者而不是專業人士。 – nelzkie

+0

@Ran因爲我在互聯網上看到的大多數代碼都使用基礎適配器。 – nelzkie

回答

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(一個合理的值

+0

ahm是,它已經在我的基礎適配器中。如果我使用cursoradapter,那麼您有任何教程嗎? :)謝謝 – nelzkie

+0

關鍵是你需要IMPLEMENT函數'return 0;'和'return null;'是滿足函數返回值的默認實現。您必須爲您的方案返回REAL值。示例可以在SDK中找到。 – Merlin

2

你在你的getCount將返回0 (),

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

如果您返回0,則表示沒有可用的數據。這裏你必須傳遞實際的數據長度。

也許在你的情況下,你必須通過列表的大小,

public int getCount() { 
     // TODO Auto-generated method stub 
     items.size(); 
    } 
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(); 
    } 
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; 
    } 
} 
相關問題