gpt4 book ai didi

java - Android eclipse中的ListView问题

转载 作者:行者123 更新时间:2023-12-01 14:04:45 25 4
gpt4 key购买 nike

我是 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

最佳答案

更改代码如下

 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);

关于java - Android eclipse中的ListView问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18996481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com