I select data from a database.
Data in one column, you must convert to kilograms, I need to multiply by a factor of one column. The data in column sets_weight in the pound, and it is necessary to display in kilograms, with without modifying to the database
How to do it correctly. Any ideas! Thanks!
onSets = db.getSets(exesIdsColExes, toprog_dif);
listSets.setAdapter(new SimpleCursorAdapter(this,
R.layout.itemsets, onSets,
new String[] {"sets_ids", "sets_weight", "sets_ones"},
new int[] {R.id.itemsets_ids, R.id.itemsets_weight, R.id.itemsets_ones}) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
// Here we get the textview and set the color
Typeface fontTitleProg = Typeface.createFromAsset(getAssets(), "AGHELVETICA.TTF");
TextView itemsets_ids = (TextView) row.findViewById(R.id.itemsets_ids);
itemsets_ids.setTypeface(fontTitleProg, 1);
itemsets_ids.setGravity(0x05);
TextView itemsets_weight = (TextView) row.findViewById(R.id.itemsets_weight);
itemsets_weight.setTypeface(fontTitleProg, 1);
itemsets_weight.setGravity(0x01);
TextView itemsets_ones = (TextView) row.findViewById(R.id.itemsets_ones);
itemsets_ones.setTypeface(fontTitleProg, 1);
itemsets_ones.setGravity(0x01);
return row;
}
});
I've never done any android development, but the documentation of SimpleCursorAdapter says:
First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked.
So you just need to pass a custom instance of SimpleCursorAdapter which will, if the column index is 1 (second column), get the value in pounds from the cursor, multiply it to transform pounds into kg, set the result as the text of the view, and return true. For the other columns, return false.
Related
Context: I am trying to make an application that allows me to have multiple edit texts (in a to do list format). I am using a linear layout with a scroll view inside of it so that I can allow users to have as many notes as they need.
Question: How can I put each editText inside an array, furthermore, how can I put the string contents of each of the editText in an array.
Any help would be much appreciated!
You need to get text from your EditText like this
EditText et = FindViewById<EditText>(Resource.Id.et_nnnn);
string a = et.Text;
And then add to your array
You can get all child views of parent layout in an array. use something as below. pass parent layout view to follow function
SparseArray<Edittext> array = new SparseArray<Edittext>();
.
.
.
private void findAllEdittexts(ViewGroup viewGroup) {
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View view = viewGroup.getChildAt(i);
if (view instanceof ViewGroup)
findAllEdittexts((ViewGroup) view);
else if (view instanceof Edittext) {
Edittext edittext = (Edittext) view;
array.put(edittext.getId(), edittext);
}
}
}
To get all edittexts text you can loop over that array and store in different array or list using gettext on each child.
Hello how can i set the item liner Layout background color from this array that it have just 3 color , position one color one , position two color two , position three color three and then position one color one and so on..
the position of the list view
int[] androidColors = context.getResources().getIntArray(R.array.randomColor);
viewHolder.linearLayout.setBackgroundColor(androidColors[position]);
You can try to create your own custom adapter and implement getView function like this :
public View getView (int position, View convertView, ViewGroup parent){
if( convertView == null ){
//We must create a View:
convertView = inflater.inflate(R.layout.my_list_item, parent, false);
}
//Here we can do changes to the convertView, such as set a text on a TextView or set the color of every single item of your view.
//or an image on an ImageView.
return convertView;
}
try to have a look to this post:
Change background colour of current listview item in adapter getView method
I am trying to set the value of a textview based on what is selected in the spinner. For example if the cubs were the first element in the list and selected it would set the textview to their hometown. This is what I have so far but it seems to not update the textview.
public void onItemSelected (AdapterView < ? > parent, View view,int position, long id){
TextView mTextView = (TextView) findViewById(R.id.textView);
if(R.id.spinner == 0){
mTextView.setText("Chicago");
}
This condition doesn't seem to make sense:
if(R.id.spinner == 0){
mTextView.setText("Chicago");
}
I think what you want to do is check the int position parameter instead:
if(position == 0){
mTextView.setText("Chicago");
}
I am developing an app which basically should download a list of events from a website and then views them in a in a calendar like layout. I already covered the first part. But I don't have an idea how to make a layout which would be a grid (tume on one axis and date on second axis) and the events in form of listview would be placed on the grid based on the start and end time. I just need a hint how to start this. The problem is that i have to add the events depending on user choices.
This will create calender like grid programtically..
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
/* INFLATE MAIN TABLE LAYOUT */
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.table_root, null);
TableLayout tbl = (TableLayout) root.findViewById(R.id.tblLayout);
this.setContentView(root);
ViewGroup row = (ViewGroup) inflater.inflate(R.layout.table_row, root, false);
/* GENERIC LOOP FOR CREATING TABLE */
int count = 1;
for (int day = 0; day < calendarCount; day++)
{
row.addView(day);
if (count <= weekCount) {
root.addView(row);
row = (ViewGroup) inflater.inflate(R.layout.popup_row, root, false);
}
count++;
}
Also you can see..
http://w2davids.wordpress.com/android-simple-calendar/
I have a ViewGroup defined in XML with a view inside, at onCreate time I'd like to have a variable of those.
I don't want to go through the hassle of using a listview+adapter cause its clearly overkill as I know the list won't change since onCreate()
This is more or less the code I'd like to have.
TextView mytextview = myViewGroup.findViewById(R.id.mytext);
for(String test : strings){
mytextview = mytextview.clone();
mytextview.setText(test);
myViewGroup.addView(mytextview);
}
But it is not working.
Maybe use an inflater, and put the textview in an external layout file:
View v = LayoutInflater.from(this).inflate(R.layout.textview_include, null);
viewGroup.addView(v);
Using code of Mathias Lin and using the hint from javahead76:
LinearLayout x = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 5; i++) {
View c = LayoutInflater.from(this).inflate(R.layout.singlerow, x);
TextView t = ((TextView)findViewById(R.id.textView1));
t.setId(i+10000);
t.setText("text"+i);
}
TextView b = (TextView) findViewById(10003);
b.setText("10003");
If you do this, you will most likely get the exact same id for every view created this way. This means doing things like ((TextView)v).setText("some text"); will be called on every TextView previously inflated from the same layout. You can still do it this way, but you should call setId() and have some reasonable method for ensuring you do not get the same id twice in a row - incrementation or universal time, etc.
Also, I think Android reserves a certain range of id's for dynamically created id's. You might avoid ID's in this range; but honestly, I don't know the id system works so I could be wrong on this point.