Reputation: 2492
I have a table in Parse with 4 columns (except the built-in columns).
I want to get these 4 columns and arrange this data in a table, inside TextViews.
Each table row on my app needs to contain different data. How can I create this table and arrange the data in this table?
I already know pretty much how to create a table. the important is to fill the table with the data from Parse.com table.
EDIT #1:
I don't know how many rows there are in the database, so the table needs to be endless
Upvotes: 2
Views: 694
Reputation: 3734
The important is to fill the table with the data from Parse.com table
This is how you query data from Parse: Retrieving Objects
Example:
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());
}
}
});
As you are populating data from database, you don't need to hard code Rows and Columns because you don't know rows in database. The solution is to create layout dynamically:
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);
}
Call buildRow(...)
for every object you obtain from Parse
Upvotes: 0
Reputation: 2518
Lets assume you have a row layout defined like this:
<?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>
After that, now i would use a single RecyclerView to inflate and populate that Data from your Database. With some Gaps in the code it should look somewhat like this.
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;
}
Upvotes: 1