2012-01-12 2 views
0

여기 내 코드는 입니다. com.mjzone.project package;검색된 데이터가 목록보기에 표시되지 않습니다.

public class MyDatabase extends Activity { 

final protected String DATABASE_TABLE = "details"; 
private MyDatabaseHelper myHelper; 
Context mjContext; 
SQLiteDatabase myDb; 
String[] values = null; 

// ====== Constructor for MyDatabse class ========== 

public MyDatabase(Context c) { 
    mjContext = c; 

} 

private class MyDatabaseHelper extends SQLiteOpenHelper { 

    public MyDatabaseHelper(Context context) { 
     super(context, "MapDatabase", null, 1); 
     // TODO Auto-generated constructor stub 
    } 

    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE IF NOT EXISTS " 
       + DATABASE_TABLE 
       + " (" 
       + BaseColumns._ID 
       + " INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR NULL, description VARCHAR NULL,latitude VARCHAR NULL, longitude VARCHAR NULL,image BLOB NULL, category VARCHAR NULL)"); 
     // TODO Auto-generated method stub 

    } 

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

    } 

} 

// ========= to open connection ============== 

public MyDatabaseHelper open() throws Exception { 
    myHelper = new MyDatabaseHelper(mjContext); 
    myDb = myHelper.getWritableDatabase(); 

    return myHelper; 

} 

// ======== to close connection =============== 

public void close() throws Exception{ 
    myHelper.close(); 

} 

// ========= to create entry ================== 

public long createEntry(byte[] iimage, String iname, String idescription,String ilatitude, String ilongitude) { 

    ContentValues myBundle = new ContentValues(); 

    myBundle.put("name", iname); 
    myBundle.put("description", idescription); 
    myBundle.put("latitude", ilatitude); 
    myBundle.put("longitude", ilongitude); 
    myBundle.put("image", iimage); 
    myBundle.put("category", "NT"); 

    return myDb.insert(DATABASE_TABLE, null, myBundle); 

} 

// =========== get data ======================== 

public String[] getData() throws Exception { 

    final String[] columns = { BaseColumns._ID, "name", "description","latitude", "longitude", "image", "category" }; 
    Cursor c = myDb.query(DATABASE_TABLE, columns, null, null, null, null,null); 

    int xname = c.getColumnIndex("name"); 
    int xdescription = c.getColumnIndex("description"); 
    int xcategory = c.getColumnIndex("image"); 

    int i = 0; 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 

     values[i] = c.getString(xcategory) + "\t" + c.getString(xname) + "\t" + c.getString(xdescription); 
     i++; 

    } 
      c.close(); 
    return values; 

} 

} 위의

여기 데이터베이스 클래스의 코드와 내 배열 어댑터 클래스 다음의 목록 데모 클래스

public class MyListDemo extends ListActivity{ 

String[] data; 
@Override 
protected void onCreate(Bundle arg0) { 
    // TODO Auto-generated method stub 
    super.onCreate(arg0); 

    try { 
     MyDatabase dbObject = new MyDatabase(this); 
     dbObject.open(); 
     data = dbObject.getData(); 
     MyArrayAdapter adpter = new MyArrayAdapter(this, data); 
     setListAdapter(adpter); 
     dbObject.close(); 

    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 

}

보여줍니다

public class MyArrayAdapter extends ArrayAdapter<String> { 

private String[] values; 
private Context context; 

public MyArrayAdapter(Context context, String[] values) { 
    super(context, R.layout.rowlayout, values); 
    this.context = context; 
    this.values = values; 
    // TODO Auto-generated constructor stub 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false); 
    TextView textView = (TextView) rowView.findViewById(R.id.label); 
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); 
    textView.setText(values[position]); 
    // Change the icon for Windows and iPhone 
    String s = values[position]; 

    if (s.startsWith("NT")) { 
     imageView.setImageResource(R.drawable.ic_launcher); 
    } else { 
     imageView.setImageResource(R.drawable.camera); 
    } 

    return rowView; 
} 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="22px" 
    android:layout_height="22px" 
    android:layout_marginLeft="4px" 
    android:layout_marginRight="10px" 
    android:layout_marginTop="4px" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

<TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" 
    android:textSize="30px" > 
</TextView> 

내 layout.xml

하지만 목록보기는 어떤 이유로 채워지지 않습니다. 검은 색 화면 만 보여줍니다. 아무도 나를 도울 수 없습니까?

덕분에 ...

답변

0

대신 다음과 같이 SimpleCursorAdapter를 사용해보십시오 : 우리가 그것은 내가 당신의 가치를 생각

Cursor c = myDb.query(DATABASE_TABLE, columns, null, null, null, null,null); 

String[] from={"category","name","description"} 
int[] to={R.id.IV1,R.id.TV1,R.id.TV2} 

SimpleCursorAdapter adapter=new SimpleCursorAdapter(this, listview_item, c, from, to); 

adapter.setViewBinder(new ViewBinder() { 

    public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) { 
     if(aColumnIndex==aCursor.getColumnIndex("category")){ 
      String s = aCursor.getString(aCursor.getColumnIndex("category")); 

      if (s.startsWith("NT")) { 
       ImageView IV= (ImageView) aView; 
       IV.setImageResource(R.drawable.ic_launcher); 
      } else { 
       IV.setImageResource(R.drawable.camera); 
      } 
      return true; 
     } 
     return false; 
    } 
}); 
+1

나는 방금 친애하는, 여전히 동일하게 노력했다. 내 getData() 메소드가 정확합니까? 문자열 배열에 데이터를 추가 한 다음 배열 어댑터를 통해 데이터를 검색하려고했습니다. 하지만 확실하지 않은 경우이 올바른 방법 – Manoj

+1

일단 내가 그것을 변경하면, 나는이 오류 로그를 얻을. 그런 다음 c.close()를 추가했지만 여전히 01-12 20 : 43 : 36.295 : E/Cursor (686) : 비활성화되지 않았거나 닫히지 않은 커서를 마무리하는 오류가 다시 나타납니다. database = /data/data/com.mjzone.project/databases/MapDatabase, table = details, query = SELECT _id, 이름, 설명, 위도, 경도, 이미지, 카테고리 자세한 내용 – Manoj

+1

왜이 오류가 계속 발생합니까? 커서를 닫았나요? 01-12 20 : 43 : 36.295 : E/Cursor (686) : android.database.sqlite.DatabaseObjectNotClosedException : 응용 프로그램이 여기서 열린 커서 또는 데이터베이스 객체를 닫지 않았습니다. – Manoj

0

을 listview_item 부르 자, 1 개 이미지 뷰 2 TextViews (TV1, TV2)와 레이아웃 만들기 is null :

String[] values = null; 

values[i] = c.getString(xcategory) + "\t" + c.getString(xname) + "\t" + c.getString(xdescription); 
+0

나는 이것을 "String [] values;"로 설정했다. 여전히 같은 문제가 .. – Manoj

+1

그게 전부 같은. String [] values ​​= new String [ "배열에 필요한 수 또는 위치"]와 같아야합니다. – sfratini

+0

수정했습니다. 아직 나는 같은 오류가 발생 .. 내 getData() 의심. 그 방법에 결함이 있습니까? – Manoj

관련 문제