我在Parse中有一个包含4列的表(内置列除外)。
我想得到这4列并将这些数据排列在TextViews中的表格中。
我的应用上的每个表格行都需要包含不同的数据。如何创建此表并在此表中排列数据?
我已经非常了解如何创建表格。重要的是用Parse.com表中的数据填充表格。
编辑#1:
我不知道数据库中有多少行,所以表格需要无穷无尽
答案 0 :(得分:1)
让我们假设你有一个像这样定义的行布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/item1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"/>
<TextView
android:id="@+id/item2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"/>
<TextView
android:id="@+id/item3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"/>
<TextView
android:id="@+id/item4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"/>
</LinearLayout>
之后,现在我将使用单个RecyclerView来扩充和填充数据库中的数据。在代码中有一些差距,它应该看起来像这样。
public void doStuff() {
RecyclerView rv = findViewById();
TableAdapter ta = new TableAdapter();
query.findInBackground(new FindCallback() {
public void done(List<Data> content, ParseException pEx) {
ta.setData(content);
}
}
rv.setAdapter(ta);
}
public class TableAdapter extends RecyclerView.Adapter<RowHolder> {
List<Data> data = new ArrayList<>();
public void setData(List<Data> newData){
data.clear();
data.addAll(newData);
notifyDataSetChanged();
}
@Override
public RowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new RowHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.tablerow, parent, false));
}
@Override
public void onBindViewHolder(RowHolder holder, int position) {
holder.bind(data.get(position));
}
@Override
public int getItemCount() {
return data.size();
}
}
public class RowHolder extends RecyclerView.ViewHolder {
TextView tv1, tv2, tv3, tv4;
public RowHolder(View itemView) {
super(itemView);
tv1 = (TextView) itemView.findViewById(R.id.item1);
tv2 = (TextView) itemView.findViewById(R.id.item2);
tv3 = (TextView) itemView.findViewById(R.id.item3);
tv4 = (TextView) itemView.findViewById(R.id.item4);
}
public void bind(Data d) {
tv1.setText(d.field1);
tv2.setText(d.field2);
tv3.setText(d.field3);
tv4.setText(d.field4);
}
}
class Data {
String field1, field2, field3, field4;
}
答案 1 :(得分:0)
查询数据的方式重要的是用Parse.com表中的数据填充表格
示例:强>
ParseQuery query = new ParseQuery("Deals");
query.whereEqualTo("Brand", "Burnettes");
query.findInBackground(new FindCallback() {
@Override
public void done(List<ParseObject> objects,
ParseException e) {
if (e == null) {
Log.d("Brand", "Retrieved " + objects.size() + " Brands");
for (ParseObject dealsObject : objects) {
// use dealsObject.get('columnName') to access the properties of the Deals object.
}
} else {
Log.d("Brand", "Error: " + e.getMessage());
}
}
});
在从数据库填充数据时,您不需要对行和列进行硬编码,因为您不知道数据库中的行。 解决方案是动态创建布局:
TableLayout table_layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout....);
table_layout = (TableLayout) findViewById(R.id.TableLayout);
}
public void buildRow (Object obj){
TableRow row = new TableRow(this);
row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
TextView tv1 = new TextView(this);
tv1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
row.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
row.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
row.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
row.addView(tv4);
// Populate each textview with obj values
table_layout.addView(row);
}
为buildRow(...)
Parse