Working on my first app for android and want to display some data in a table(or something similar). After looking around here I guess android does not support something like a JTable; someone said you have to create a custom table? What is the best way to display data that is a string in CSV format? In a Java app I created I have a function that creates a 2D Array out of the string and throws that into a JTable.
Data Format in String:
Sun 02-16-14 09:45 PM,1,REAL TIRED,JUST WACK AT IT,2 - 4,
Sun 02-23-14 08:10 PM,1,BALLERS,REAL TIRED,4 - 11,
Sun 03-02-14 09:00 PM,1,REAL TIRED,EL TRI,1 - 7,
Sun 03-09-14 05:50 PM,1,GO GO POWER RANGERS,REAL TIRED,4 - 9,
Sun 03-16-14 06:40 PM,1,REAL TIRED,GAME OF GROANS,16 - 0,
Sun 03-23-14 09:00 PM,1,HUNGOVER HAT TRICKS,REAL TIRED,
Sun 03-30-14 07:25 PM,1,REAL TIRED,PRESTIGE WORLD WIDE,
Sun 04-06-14 04:20 PM,1,REAL TIRED,BALLERS,
Not sure if there are any good options because of the limited space for mobile devices. Maybe only display one row at a time and be able to click through them?
You can try this example, you will be very comfortable with this code. Even though if you want to add rows in the table view dynamically from an server or from an API call then just create a for loop and add it according this example.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
String[] presidents = { "Dwight D. Eisenhower", "John F. Kennedy",
"Lyndon B. Johnson", "Richard Nixon", "Gerald Ford",
"Jimmy Carter", "Ronald Reagan", "George H. W. Bush",
"Bill Clinton", "George W. Bush", "Barack Obama",
"Dwight D. Eisenhower", "John F. Kennedy", "Lyndon B. Johnson",
"Richard Nixon", "Gerald Ford", "Jimmy Carter",
"Ronald Reagan", "George H. W. Bush", "Bill Clinton",
"George W. Bush", "Barack Obama", "Dwight D. Eisenhower",
"John F. Kennedy", "Lyndon B. Johnson", "Richard Nixon",
"Gerald Ford", "Jimmy Carter", "Ronald Reagan",
"George H. W. Bush", "Bill Clinton", "George W. Bush",
"Barack Obama" };
ScrollView scrollView = new ScrollView(this);
TableLayout tableLayout = new TableLayout(this);
for (int i = 0; i < presidents.length; i++) {
TableRow tableRow = new TableRow(this);
TextView tv = new TextView(this);
tv.setPadding(10, 10, 10, 10);
tv.setGravity(Gravity.CENTER);
tv.setText(presidents[i]);
tableRow.addView(tv);
tableLayout.addView(tableRow);
View v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
tableLayout.addView(v);
}
scrollView.addView(tableLayout);
setContentView(scrollView);
}
}
Hope so you would like this. If this is suitable to you then please mark as accepted answer so that it could be helpful for others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With