Detailed explanation of the android form library SortableTableView, which helps you get started

Source: Internet
Author: User
Tags getcolor

Detailed explanation of the android form library SortableTableView, which helps you get started
 

Now the financial processes of various companies are on the mobile phone end. There are not many libraries similar to xls in Android (maybe I have little knowledge). I can see a Git library, I think it is very useful. I try to transplant it to the product and write my experiences.

Project Structure

Effect:

Gradle:

  compile 'de.codecrafters.tableview:tableview:0.9.5'

Minimum SDK-Version: 11 latest Version 0.9.5

How to use such a tableview?


  

Table: columnCount is the number of columns in your form. Of course, xmlns: table = "http://schemas.android.com/apk/res-auto” must be introduced"

Other attributes are defined in the attributes. xml file.


      
                               
   
  

I use the Demo package code as an example to explain.

MainActivity

Public class MainActivity extends AppCompatActivity {// container for storing data, each object is actually equal to a private static List
  
   
CAR_LIST = new ArrayList <> (); @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Toolbar toolbar = (Toolbar) findViewById (R. id. toolbar); if (toolbar! = Null) {setsuppactionactionbar (toolbar);} // custom View binding Id SortableCarTableView carTableView = (SortableCarTableView) findViewById (R. id. tableView); // fill in the carTableView data. setDataAdapter (new CarTableDataAdapter (this, CAR_LIST); // Add a listening event carTableView. addDataClickListener (new CarClickListener ();} // The trigger time in the form. The unit of action is private class CarClickListener implements TableDataClickListener.
   
    
{@ Override public void onDataClicked (int rowIndex, Car clickedData) {String carString = clickedData. getProducer (). getName () ++ clickedData. getName (); Toast. makeText (MainActivity. this, carString, Toast. LENGTH_SHORT ). show () ;}// plug Data !!! Static {CarProducer audi = new CarProducer (R. mipmap. audi, Audi); Car audiA1 = new Car (audi, A1, 150,250 00); Car audiA3 = new Car (audi, A3, 120,350 00 ); car audiA4 = new Car (audi, A4, 210,420 00); Car audiA5 = new Car (audi, S5, 333,600 00); Car audiA6 = new Car (audi, A6, 250,550 00); Car audiA7 = new Car (audi, A7, 420,870 00); Car audiA8 = new Car (audi, A8, 320,110 000); CarProducer bmw = new CarProducer (R. mipmap. bmw, BMW); Car bmw1 = new Car (bmw, 1er, 170,250 00); Car bmw3 = new Car (bmw, 3er, 230,420 00 ); car bmwX3 = new Car (bmw, X3, 230,450 00); Car bmw4 = new Car (bmw, 4er, 250,390 00); Car bmwM4 = new Car (bmw, M4, 350,600 00); Car bmw5 = new Car (bmw, 5er, 230,460 00); CarProducer porsche = new CarProducer (R. mipmap. porsche, Porsche); Car porsche911 = new Car (porsche, 911,280,450 00); Car porscheCayman = new Car (porsche, Cayman, 330,520 00); Car porscheCaymanGT4 = new Car (porsche, cayman GT4, 385,860 00); CAR_LIST.add (audiA3); CAR_LIST.add (audiA1); then (porscheCayman); then (audiA7); CAR_LIST.add (audiA8); CAR_LIST.add (audiA4 ); values (bmwX3); CAR_LIST.add (values); CAR_LIST.add (bmw1); CAR_LIST.add (audiA6); values (audiA5); values (bmwM4); values (bmw5); CAR_LIST.add (values ); CAR_LIST.add (bmw3); CAR_LIST.add (bmw4 );}}
   
  

Main activity Layout


          
   
  

SortableCarTableView

Public class SortableCarTableView extends SortableTableView
  
   
{// Constructor public SortableCarTableView (Context context) {this (context, null);} // constructor public SortableCarTableView (Context context, AttributeSet attributes) {this (context, attributes, 0);} // constructor public SortableCarTableView (Context context, AttributeSet attributes, int styleAttributes) {super (context, attributes, styleAttributes); // initialize the first menu, public SimpleTableHeaderAdapter (Context context, String... headers) SimpleTableHeaderAdapter simpleTableHeaderAdapter = new SimpleTableHeaderAdapter (context, Hersteller, Bezeichung, Leistung, Preis); // set the font color simpleTableHeaderAdapter. setTextColor (context. getResources (). getColor (R. color. table_header_text); setHeaderAdapter (simpleTableHeaderAdapter); // The operation int rowColorEven = context for each item color differentiation. getResources (). getColor (R. color. table_data_row_even); int rowColorOdd = context. getResources (). getColor (R. color. table_data_row_odd); setDataRowColoriser (TableDataRowColorizers. alternatingRows (rowColorEven, rowColorOdd); setHeaderSortStateViewProvider (SortStateViewProviders. brightArrows (); // the weight of each column in the menu setColumnWeight (0, 2); setColumnWeight (1, 3); setColumnWeight (2, 3); setColumnWeight (3, 2); // click business logic public void setColumnComparator (int columnIndex, Comparator
   
    
ColumnComparator), so you need to create a class to implement the specific business logic setColumnComparator (0, CarComparators. getCarProducerComparator (); setColumnComparator (1, CarComparators. getCarNameComparator (); setColumnComparator (2, CarComparators. getCarPowerComparator (); setColumnComparator (3, CarComparators. getCarPriceComparator ());}}
   
  

Adapter CarTableDataAdapter

Public class CarTableDataAdapter extends TableDataAdapter
  
   
{// Font size: private static final int TEXT_SIZE = 14; // format: private static final NumberFormat PRICE_FORMATTER = NumberFormat. getNumberInstance (); public CarTableDataAdapter (Context context, List
   
    
Data) {super (context, data) ;}@ Override public View getCellView (int rowIndex, int columnIndex, ViewGroup parentView) {// get the object Car car = getRowData (rowIndex) for each row; View renderedView = null; // obtain the specific position of the column. For more information, see XLS and CSV, the style of each row can be different. You can customize the switch (columnIndex) {case 0: renderedView = renderProducerLogo (car, parentView); break; case 1: renderedView = renderCatName (car); break; case 2: renderedView = renderPower (car, parentView); break; case 3: renderedView = renderPrice (car); break;} return renderedView ;} private View renderPrice (Car car) {String priceString = PRICE_FORMATTER.format (car. getPrice () + €; TextView textView = new TextView (getContext (); textView. setText (priceString); textView. setPadding (20, 10, 20, 10); textView. setTextSize (TEXT_SIZE); if (car. getPrice () <50000) {textView. setTextColor (0xFF2E7D32);} else if (car. getPrice () & gt; 100000) {textView. setTextColor (0xFFC62828);} return textView;} private View renderPower (Car, ViewGroup parentView) {View view = getLayoutInflater (). inflate (R. layout. table_cell_power, parentView, false); TextView kwView = (TextView) view. findViewById (R. id. kw_view); TextView psView = (TextView) view. findViewById (R. id. ps_view); kwView. setText (car. getKw () + kW); psView. setText (car. getPs () + PS); return view;} private View renderCatName (Car car) {return renderString (car. getName ();} private View renderProducerLogo (Car car, ViewGroup parentView) {View view = getLayoutInflater (). inflate (R. layout. table_cell_image, parentView, false); ImageView imageView = (ImageView) view. findViewById (R. id. imageView); imageView. setImageResource (car. getProducer (). getLogo (); return view;} private View renderString (String value) {TextView textView = new TextView (getContext (); textView. setText (value); textView. setPadding (20, 10, 20, 10); textView. setTextSize (TEXT_SIZE); return textView ;}}
   
  

Sort by CarComparators
The business logic that can be implemented in each column is written here, but you can also write this part without changing, according to your specific business needs.

public class CarComparators {    public static Comparator
  
    getCarProducerComparator() {        return new CarProducerComparator();    }    public static Comparator
   
     getCarPowerComparator() {        return new CarPowerComparator();    }    public static Comparator
    
      getCarNameComparator() {        return new CarNameComparator();    }    public static Comparator
     
       getCarPriceComparator() {        return new CarPriceComparator();    }    private static class CarProducerComparator implements Comparator
      
        { @Override public int compare(Car car1, Car car2) { return car1.getProducer().getName().compareTo(car2.getProducer().getName()); } } private static class CarPowerComparator implements Comparator
       
         { @Override public int compare(Car car1, Car car2) { return car1.getPs() - car2.getPs(); } } private static class CarNameComparator implements Comparator
        
          { @Override public int compare(Car car1, Car car2) { return car1.getName().compareTo(car2.getName()); } } private static class CarPriceComparator implements Comparator
         
           { @Override public int compare(Car car1, Car car2) { if (car1.getPrice() < car2.getPrice()) return -1; if (car1.getPrice() > car2.getPrice()) return 1; return 0; } }}
         
        
       
      
     
    
   
  

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.