-1

I have two TextViews in a layout using a row layout for a SimpleCursorAdapter. The TextViews have their values populated from the SimpleCursorAdapter.

The first view displays the name of a computer host, that's fine but the second displays the mac address of the host, at current in the database I've got I'm storing it as a string in Hex format.

What I want to do is not have the formatting for the mac address in the database but format it programatically using a custom TextView or other else that will work. So basically AABBCCEEFF00 will become AA:BB:CC:EE:FF:00 when the SimpleCursorAdapter populates that TextView extened class.

What's the best way forward with this and where should I plug in the code if I do use custom TextView?

I've also seen reference to View Binding? but unsure if that's the route to go, I'm very much about making code modular and rather not override complex code when extending a simple class is possible.

Peter Fox
  • 1,809
  • 2
  • 20
  • 34

2 Answers2

1

As far as I could understand your question,You want to convert AABBCCDDEEFF into AA:BB:CC:DD:EE:FF programmatically.let me know if I didn't get your question.

look at following code, from here,you are going to call a function to get formatted mac

String formattedMac = new String();
formattedMac=getFormattedMac("AABBCCDDEEFF");
Log.e("formatted Mac", formattedMac);//it will be look like, AA:BB:CC:DD:EE:FF

and here is the function returning formatted mac,

    private String getFormattedMac(String mac) {
    String result = new String();
    result="";
    int start=0;
    int end=2;
    int max=mac.length()+2;

    while(end<max)
    {
        result = result+mac.substring(start, end);
        if(end<mac.length())
        {
            result = result+":";
        }
        Log.e("result", result);
        start=start+2;
        end=end+2;
    }

    return result;
}
Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
  • 1
    This does not answer the question. The question was how to format a String in an adapter, which takes a cursor from a database, not how to perform the actual formatting. –  Jan 20 '13 at 06:07
  • @MichaelHerbig: In my answer,I have started with lines "As far as I could understand your question and let me know if I didn't get your question".and I think ultimately he wants to format the string,and I provided the solution.even you are using that piece of code.and you are downvoting.I'm surprised!! – Mehul Joisar Jan 20 '13 at 06:11
  • Downvoting is a way of preventing poor, suboptimal, or incorrect solutions from being noticed. I have removed the offending reference. –  Jan 20 '13 at 06:17
  • @MichaelHerbig: then kindly remove my function from your code and then make your answer complete,correct solution. – Mehul Joisar Jan 20 '13 at 06:22
  • Yeh, sorry, I can see why my question is confusing, it's the reason I've not found a solution myself so far as it's difficult to find the right terms to look for. MichaelHerbig is correct on what I wish to do. I already know how to format a string, what I want to know is how to create a middleware kind of function between when a SimpleCursorAdapter populates a TextView with a string so it can be formatted correctly. – Peter Fox Jan 20 '13 at 12:18
0

Turns out the best ways was with a ViewBinder setup, I followed this answer in the end.

Android, using SimpleCursorAdapter to set colour not just strings

Once I knew this I also changed the column to be a Blob instead of VarChar to make it a bit nicer as well.

    SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            String name = cursor.getColumnName(columnIndex);
            if ("mac_address".equals(name)) {
                byte[] macAddress = cursor.getBlob(columnIndex);
                TextView textView = (TextView)view;
                textView.setText(Target.getMacAddress(macAddress));
                return true;
            }
            return false;
        }
    };
Community
  • 1
  • 1
Peter Fox
  • 1,809
  • 2
  • 20
  • 34