我是一个非常新的 android 开发人员,我一直在尝试通过创建简单的应用程序来学习它。
现在,我一直在尝试为我的应用创建自定义 ListView。
我的数据存储在 SQLite 中,我将这些数据称为列表。我似乎找不到一个网站来演示如何让 SQLite 数据与自定义 ListView 一起使用。
以下是我的数据,我希望有人可以帮助我设置我的自定义适配器以使用我的 sqlite 数据。
记录.java
public class Records {
private long id;
private String name;
private String type;
private float mf;
private float mftop;
private float mfbot;
private float mfleft;
private float mfright;
private float rf;
private float rftop;
private float rfbot;
private float rfleft;
private float rfright;
private float tb;
private float tbtop;
private float tbbot;
private float tbleft;
private float tbright;
private float bridge;
private float bridgel;
private float bridger;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
// Middle Finger
public float getMF() {
return mf;
}
public void setMF(float mf) {
this.mf = mf;
}
public float getMFTop() {
return mftop;
}
public void setMFTop(float mftop) {
this.mftop = mftop;
}
public float getMFBot() {
return mfbot;
}
public void setMFBot(float mfbot) {
this.mfbot = mfbot;
}
public float getMFLeft() {
return mfleft;
}
public void setMFLeft(float mfleft) {
this.mfleft = mfleft;
}
public float getMFRight() {
return mfright;
}
public void setMFRight(float mfright) {
this.mfright = mfright;
}
//Ring Finger
public float getRF() {
return rf;
}
public void setRF(float rf) {
this.rf = rf;
}
public float getRFTop() {
return rftop;
}
public void setRFTop(float rftop) {
this.rftop = rftop;
}
public float getRFBot() {
return rfbot;
}
public void setRFBot(float rfbot) {
this.rfbot = rfbot;
}
public float getRFLeft() {
return rfleft;
}
public void setRFLeft(float rfleft) {
this.rfleft = rfleft;
}
public float getRFRight() {
return rfright;
}
public void setRFRight(float rfright) {
this.rfright = rfright;
}
//Thumb
public float getTB() {
return tb;
}
public void setTB(float tb) {
this.tb = tb;
}
public float getTBTop() {
return tbtop;
}
public void setTBTop(float tbtop) {
this.tbtop = tbtop;
}
public float getTBBot() {
return tbbot;
}
public void setTBBot(float tbbot) {
this.tbbot = tbbot;
}
public float getTBLeft() {
return tbleft;
}
public void setTBLeft(float tbleft) {
this.tbleft = tbleft;
}
public float getTBRight() {
return tbright;
}
public void setTBRight(float tbright) {
this.tbright = tbright;
}
//Bridge
public float getBridge() {
return bridge;
}
public void setBridge(float bridge) {
this.bridge = bridge;
}
public float getBridgeL() {
return bridgel;
}
public void setBridgeL(float bridgel) {
this.bridgel = bridgel;
}
public float getBridgeR() {
return bridger;
}
public void setBridgeR(float bridger) {
this.bridger = bridger;
}
//Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return name;
}
}
CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<NTAdapter> {
Context context;
int layoutResourceId;
NTAdapter data[] = null;
public CustomAdapter(Context context, int layoutResourceId, NTAdapter[] data) {
super(context, layoutResourceId, data);
// TODO Auto-generated constructor stub
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
DataHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new DataHolder();
holder.dataName = (TextView)row.findViewById(R.id.dataName);
holder.dataType = (TextView)row.findViewById(R.id.dataType);
row.setTag(holder);
} else {
holder = (DataHolder)row.getTag();
}
NTAdapter ntadapter = data[position];
holder.dataName.setText(ntadapter.name);
holder.dataType.setText(ntadapter.type);
return row;
}
static class DataHolder {
TextView dataName;
TextView dataType;
}
}
NTAdapter.java
public class NTAdapter {
public int id;
public String name, type;
public NTAdapter() {
super();
}
public NTAdapter(int id, String name, String type) {
super();
this.id = id;
this.name = name;
this.type = type;
}
}
数据布局.java
public class DataLayout extends ListActivity {
private RecordsDataSource datasource;
Button btnDataCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_layout);
datasource = new RecordsDataSource(this);
datasource.open();
List<Records> values = datasource.getAllRecords();
//use the SimpleCursorAdapter to show the
//elements in a ListView
ArrayAdapter<Records> adapter = new ArrayAdapter<Records>(this, android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
/**
CustomAdapter adapter = new CustomAdapter(this, R.layout.data_layout_listview, values);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
*/
Button btnDataCancel = (Button)findViewById(R.id.btnDataCancel);
//Cancel Button
btnDataCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent goMain = new Intent(getApplicationContext(), MainActivity.class);
startActivity(goMain);
finish();
}
});
}
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
}
请注意,在我的 DataLayout.java 中,我一直在尝试让我的 sqlite 数据与我的自定义列表视图设置一起使用。
/**
CustomAdapter adapter = new CustomAdapter(this, R.layout.data_layout_listview, values);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
*/
我知道我的问题是我的 NTAdapter.java 和我的 CustomAdapter.java 没有正确设置。但是,我不知道如何修复它,以便它接受存储在值中的数据列表
由于代码开始变得太长而无法阅读,我没有发布我的其他课程。但请注意,如果我不使用自定义列表视图,一切正常,因为它还没有工作。
任何帮助将不胜感激。先感谢您。
编辑:另外,我知道 NTAdapter 并不是真正需要的,但我不知道如何制作它,以便我的 CustomAdapter.java 将从我的值中获取数据并使用它们来设置我的自定义 ListView。
编辑2:对于那些想知道什么是值的人,请参阅DataLayout.java。
.