0

我是android开发的新手。我正在向我的 sqllite 表(id​​、name、number)添加详细信息。现在我尝试列出它。我只想列出名称。当点击特定名称时会显示特定名称的数量。现在我试着列出名字。我的代码是。

详细信息.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    >
<ListView
    android:cacheColorHint="#00000000"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@android:id/list"
    android:layout_alignParentLeft="true">
</ListView>
</LinearLayout>

细节操作.java

    public class DetailOperations {
private DataBase dbHelper;
private String[] DETAILS_TABLE_COLUMNS = { DataBase.DETAIL_ID, DataBase.DETAIL_NAME,DataBase.DETAIL_NO };
private SQLiteDatabase database;
public DetailOperations(Context context) {
    dbHelper = new DataBase(context);
}

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}

public List getAllDetails(){
    List Details=new ArrayList();
    Cursor cursor = database.query(DataBase.DETAILS,
            DETAILS_TABLE_COLUMNS, null, null, null, null, null);
    cursor.moveToFirst();
    while(!cursor.isAfterLast()){
        Detail de=parseDetail(cursor);
        Details.add(de);
        cursor.moveToNext();
    }
    cursor.close();
    return Details;

}
private Detail parseDetail(Cursor cursor) {
    Detail d = new Detail();
    d.setId((cursor.getInt(0)));
    d.setName(cursor.getString(1));
    d.setnumber(cursor.getString(2));
    return d;
     }
        }

详细信息.java

   public class Details extends ListActivity {
private DetailOperations detailDBoperation;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.details);
    detailDBoperation=new DetailOperations(this);
    detailDBoperation.open();
    List<Detail> values=detailDBoperation.getAllDetails();
    List<String> names=new ArrayList<String>();
    for(int i=0;i<values.size();i++)
    {
      names.add(values.get(i).getName()); 
    }
    ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,names);
   // List values=detailDBoperation.getAllDetails();
   // ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list ofDetail ->Name);
    setListAdapter(adapter);
    setListAdapter(adapter);
  }

@Override
protected void onResume(){
    detailDBoperation.open();
    super.onResume();
}
@Override
protected void onPause(){
    detailDBoperation.close();
    super.onPause();
}
}

细节.java

  public class Detail {
private int id;
private String name;
private String number;

public long getId() {   return id;}
public void setId(int id) { this.id = id;   }
public String getName() {   return this.name;   }
public void setName(String name) {  this.name = name;   }
public String getNumber(){  return this.number; }
public void setnumber(String number){   this.number=number; }
   }

但我得到了这样的输出列表

  com.example.experiments.Detail@417c38d0
  com.example.experiments.Detail@417c3a10
  com.example.experiments.Detail@417c3ab0
4

3 回答 3

1

更改代码如下

 List values=detailDBoperation.getAllDetails();
 List<String> names=new ArrayList<String>();
 for(int i=0;i<values.size();i++)
 {
   names.add(values.get(i).getName()); 
 }
 ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,names);
于 2013-09-25T05:01:12.343 回答
0

只需通过添加将您的数组转换为字符串

  Details.add(de.toString());
于 2013-09-25T05:00:11.320 回答
0

这里
getAllDetails()返回List Details,它是Detail de type Object的列表。您需要在ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list of Detail ->Name 中传递Detail List ->Name List );

于 2013-09-25T05:03:57.410 回答