gpt4 book ai didi

java - Android SQLite CustomAdapter 和自定义 ListView

转载 作者:行者123 更新时间:2023-11-30 09:24:14 25 4
gpt4 key购买 nike

我是一个非常新的 android 开发者,我一直在尝试通过创建简单的应用程序来学习它。

现在我一直在尝试为我的应用程序创建自定义 ListView。

我的数据存储在 SQLite 中,我将这些数据称为列表。我似乎无法找到一个网站来演示如何让 SQLite 数据与自定义 ListView 一起工作。

以下是我的数据,我希望有人可以帮助我设置自定义适配器以处理我的 sqlite 数据。

Records.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;
}
}

DataLayout.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 数据与我的自定义 ListView 设置一起工作。

        /**
CustomAdapter adapter = new CustomAdapter(this, R.layout.data_layout_listview, values);
listView1 = (ListView)findViewById(R.id.listView1);

listView1.setAdapter(adapter);
*/

我知道我的问题是我的 NTAdapter.java 和我的 CustomAdapter.java 没有正确设置。但是,我不知道如何修复它以便它接受存储在values

中的数据列表

由于代码开始变得有点太长而难以阅读,我没有发布其他类(class)。但请注意,如果我不使用我的自定义 ListView ,一切正常,因为它还没有工作。

任何帮助将不胜感激。提前谢谢你。

编辑:此外,我知道 NTAdapter 并不是真正需要的,但我不知道如何制作它以便我的 CustomAdapter.java 将从我的 获取数据并使用它们设置我的自定义 ListView。

edit2:对于那些想知道values 是什么的人,请引用DataLayout.java

.

最佳答案

你在制作 CustomAdapter 时所做的是你试图传递一个 List到接受 NTAdapter array 的构造函数。所以你的程序不应该正常工作。

您可以做的是使您的 CustomAdapter 类类似于以下内容:

public class CustomAdapter extends ArrayAdapter<Records> {

Context context;
int layoutResourceId;
List<Records> data;


public CustomAdapter(Context context, int layoutResourceId, ArrayList<Records> 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();
}

Records records = data.get(position);
holder.dataName.setText(records.getName());
holder.dataType.setText(records.getType());

return row;
}

static class DataHolder {
TextView dataName;
TextView dataType;
}

关于java - Android SQLite CustomAdapter 和自定义 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15798624/

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