gpt4 book ai didi

php - 在 Android 中显示来自 MySQL 的复选框字段

转载 作者:行者123 更新时间:2023-11-29 06:26:06 24 4
gpt4 key购买 nike

我有一个 MySQL 数据库,其中有一些在 mysql 中设置为 TINYINT(1) 的是/否字段。我正在将该表中的信息显示到 android 上的表布局中。我如何让这些字段在 android 中显示为复选框,因为这些字段中的值是 0 表示否,0 以外的任何值表示是?或者是否有通过更改 mysql 中的字段类型来执行此操作的更好方法?

如果您想查看,这是我的 MainActivity.java。它目前仅显示表格中的 3 个文本字段。

public class MainActivity extends Activity {
String data = "";
TableLayout tl;
TableRow tr;
TextView label;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tl = (TableLayout) findViewById(R.id.maintable);

final GetDataFromDB getdb = new GetDataFromDB();
new Thread(new Runnable() {
public void run() {
data = getdb.getDataFromDB();
System.out.println(data);

runOnUiThread(new Runnable() {

@Override
public void run() {
ArrayList<Users> users = parseJSON(data);
addData(users);
}
});

}
}).start();
}

public ArrayList<Users> parseJSON(String result) {
ArrayList<Users> Suppliers = new ArrayList<Users>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Users user = new Users();
// user.setId(json_data.getInt("id"));
user.setDelivery_date(json_data.getString("Delivery_Date"));
user.setOrder(json_data.getString("Order"));
user.setHauler(json_data.getString("Haulers_HaulerID"));
Suppliers.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return Suppliers;
}

void addHeader(){
/** Create a TableRow dynamically **/
tr = new TableRow(this);

/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText("Delivery Date");
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.RED);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.

TextView supplier = new TextView(this);
supplier.setText("Order");
supplier.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
supplier.setPadding(5, 5, 5, 5);
supplier.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(supplier,params);
tr.addView((View)Ll); // Adding textview to tablerow.

TextView hauler = new TextView(this);
hauler.setText("Hauler");
hauler.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
hauler.setPadding(5, 5, 5, 5);
hauler.setBackgroundColor(Color.RED);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 5, 5);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(hauler,params);
tr.addView((View)Ll); // Adding textview to tablerow.

// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}

@SuppressWarnings({ "rawtypes" })
public void addData(ArrayList<Users> users) {

addHeader();

for (Iterator i = users.iterator(); i.hasNext();) {

Users p = (Users) i.next();

/** Create a TableRow dynamically **/
tr = new TableRow(this);

/** Creating a TextView to add to the row **/
label = new TextView(this);
label.setText(p.getDelivery_date());
// label.setId(p.getId());
label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
label.setPadding(5, 5, 5, 5);
label.setBackgroundColor(Color.GRAY);
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(label,params);
tr.addView((View)Ll); // Adding textView to tablerow.

/** Creating Qty Button **/
TextView supplier = new TextView(this);
supplier.setText(p.getOrder());
supplier.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
supplier.setPadding(5, 5, 5, 5);
supplier.setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(supplier,params);
tr.addView((View)Ll); // Adding textview to tablerow.

/** Creating Qty Button **/
TextView hauler = new TextView(this);
hauler.setText(p.getHauler());
hauler.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
hauler.setPadding(5, 5, 5, 5);
hauler.setBackgroundColor(Color.GRAY);
Ll = new LinearLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(hauler,params);
tr.addView((View)Ll); // Adding textview to tablerow.

// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
}

任何建议表示赞赏。

最佳答案

你有一个 Users 数据对象,所以我认为你应该在 Users 对象中有一个 bool 属性,在解析时将 JSON 整数转换为 bool 值,并使用 bool 值来设置复选框:

class Users {

.
.
.
private boolean mFlag;

public boolean isFlag() {
return mFlag;
}

public void setFlag(boolean flag) {
mFlag = flag;
}

.
.
.
}

解析:

 user.setFlag(json_data.getInt("Flag") != 0);

显示:

CheckBox cbFlag = new CheckBox(activity);

cbFlag.setChecked(user.isFlag());

关于php - 在 Android 中显示来自 MySQL 的复选框字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30903391/

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