What i'm trying to do is add create an application that locates some local business. So the activity I'm doing displays the business information. Some business have severals stores located on the city, and some others only have one. So here is my problem: In the View I have Scroll View with all the elements, and a linear layout inside the scrollview. If the business has more then one store, i will add each store information in a new text view and add the text view to the layout (this is done all by code). But at the moment to display the layout, it only displays me one store, instead of showing me 3 or 4. What I'm I doing wrong? Here is the method setupViews(), which is the one in chanrge of the displaying:
private void setupViews() throws SQLException {
ImageView iv = (ImageView) findViewById(R.id.negocio_logo);
try {
iv.setImageBitmap(BitmapFactory.decodeByteArray(toShow.getImgSrc(),
0, toShow.getImgSrc().length));
} catch (NullPointerException npe) {
iv.setBackgroundColor(Color.WHITE);
}
TextView nombreEmpresa = (TextView) findViewById(R.id.nombre_empresa);
nombreEmpresa.setText(toShow.getNombre());
TextView descripcionEmpresa = (TextView) findViewById(R.id.descripcion_empresa);
descripcionEmpresa.setText(toShow.getDescripcion());
TextView direccionEmpresa = (TextView) findViewById(R.id.direccion_empresa);
direccionEmpresa.setText(toShow.getDireccion());
LinearLayout rl = (LinearLayout) findViewById(R.id.linear_layout_si);
TextView suc = (TextView) findViewById(R.id.sucursales_empresa);
sucursalDAO sDAO = new sucursalDAO();
boolean tieneSucursales = sDAO.hasSucursales(toShow.getId());
if (tieneSucursales == false) {
suc.setVisibility(View.GONE);
// sucs.setVisibility(View.GONE);
} else {
suc.setVisibility(View.VISIBLE);
ArrayList<String> sucursales = sDAO.getStringSucursales(toShow
.getId());
ArrayList<TextView> tvs = new ArrayList<TextView>();
for (int i = 0; i < sucursales.size(); i++) {
TextView tv = new TextView(this);
tv.setText(sucursales.get(i));
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tvs.add(tv);
}
for (int i = 0; i < tvs.size(); i++) {
rl.addView(tvs.get(i), i);
}
}
}
And here is the XML of my view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White"
android:orientation="vertical" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widgetscroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:orientation="vertical" >
<ImageView
android:id="#+id/negocio_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:contentDescription="#string/logo_negocio" />
<TextView
android:id="#+id/datos_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/negocio_logo"
android:background="#drawable/barra"
android:text="#string/datos_empresa"
android:textColor="#color/White" />
<TextView
android:id="#+id/nombre_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/datos_empresa"
android:text="#string/testing" />
<TextView
android:id="#+id/descripcion_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/nombre_empresa"
android:text="#string/testing" />
<TextView
android:id="#+id/direccion_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/descripcion_empresa"
android:text="#string/testing" />
<TextView
android:id="#+id/sucursales_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/direccion_empresa"
android:background="#drawable/barra"
android:text="#string/sucursales"
android:visibility="gone" />
<LinearLayout
android:id="#+id/linear_layout_si"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/sucursales_empresa" >
</LinearLayout>
<TextView
android:id="#+id/contacto_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/linear_layout_si"
android:text="#string/testing" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I think you forgot to set orientation on LinearLayout and it's showing horitzontal
I bet you forgot to set the orientation of the LinearLayout.
Related
I have a custom ListView where each element has an icon and 3 TextViews. I am able to change the background to white but now I don't see the dynamic effect when I click on the text. I mean the gray color on touch (ripple effect).
this is the MainActivity
ListView mListView = findViewById(R.id.list_layout);
names = new ArrayList<>();
macs = new ArrayList<>();
adapter= new CompleteListAdapter(this, names, macs);
mListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Method in my CompleteListAdapter which extends BaseAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = LayoutInflater.from(mContext).inflate(R.layout.two_line_icon, parent, false);
TextView text1 = (TextView) rowView.findViewById(R.id.text1);
TextView text2 = (TextView) rowView.findViewById(R.id.text2);
ImageView icon = (ImageView) rowView.findViewById(R.id.icon);
text1.setText(names.get(position));
text2.setText(macs.get(position));
return rowView;
}
This is part of the main_activity.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
tools:context="com.example.myapp.MainActivity">
<ListView
android:id="#+id/list_layout"
android:layout_width="match_parent"
android:layout_height="400dp"></ListView>
</LinearLayout>
And this is the single entry of the ListView: two_line_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#android:color/white"
android:layout_height="match_parent">
<ImageView android:id="#+id/icon"
android:foregroundTint="#android:color/transparent"
android:foregroundTintMode="add"
android:scaleType="fitStart"
android:src="#drawable/img_logo"
android:tintMode="add"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Texto 1"/>
<TextView android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Tet2"/>
<TextView android:id="#+id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="text3"/>
</LinearLayout>
</LinearLayout>
I'm working on a gallery type app. The issue that I have at the moment is trying to fetch the typed text within an EditText. The error that I am getting is a casting exception because the code fetches the EditText block from the layout as a TextView block. This only occurs after I swipe, which causes the some of the LinearLayouts imbeded in the second GridLayout to be removed.
The LinearLayouts are put in at runtime into the Gridlayout (with the id product_details), the amount Layouts is calculated according to which picture is pressed on.
This part of the code fetches the current LinearLayouts that are imbeded in the GridLayout:
for (int i = 0; i<codes.size(); i++) {
//Sets the different elements to te correct texts
ViewGroup linearLayout = (ViewGroup) gridLayout.getChildAt(i);
TextView textCode = (TextView) linearLayout.getChildAt(0);
TextView textPrice = (TextView) linearLayout.getChildAt(1);
EditText editTextQ = (EditText) linearLayout.getChildAt(2);
EditText editTextC = (EditText) linearLayout.getChildAt(4);
commentsAddList.add(editTextC.getText().toString());
quantityAddList.add(editTextQ.getText().toString());
priceAddList.add(textPrice.getText().toString().substring(1));
codeAddList.add(textCode.getText().toString());
productModelAddList.add(new ProductModel());
productModelAddList.get(i).setCode(codeAddList.get(i));
if(!quantityAddList.get(i).equals("")) {
productModelAddList.get(i).setQuantity(Integer.parseInt(quantityAddList.get(i)));
}else{
productModelAddList.get(i).setQuantity(0);
}
productModelAddList.get(i).setComment(commentsAddList.get(i));
if(!priceAddList.get(i).equals("")) {
productModelAddList.get(i).setPrice(Double.parseDouble(priceAddList.get(i)));
}else{
productModelAddList.get(i).setPrice(0);
}
mRequestPermissions();
adaptToText.addToList(productModelAddList.get(i));
}
The main picture xml Layout:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageLayout"
android:columnCount="2"
android:verticalSpacing="0dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:fitsSystemWindows="true">
<ImageView
android:layout_columnSpan="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"/>
<GridLayout
android:id="#+id/product_details"
android:columnCount="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</GridLayout>
</GridLayout>
The LinearLayout that is inserted into the GridLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_columnSpan="1"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:id="#+id/orderSideLayout">
<TextView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/productCode"
android:text=""
android:padding="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/productPrice"
android:text=""
android:padding="10dp"
android:paddingLeft="20dp"/>
<EditText
android:layout_width="match_parent"
android:id="#+id/editText1"
android:hint="Quantity goes here"
android:layout_gravity="fill_horizontal"
android:layout_height="wrap_content"
android:inputType="number"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:id="#+id/comments"
android:text="Comments: "
android:padding="10dp"
android:paddingLeft="20dp"/>
<EditText
android:layout_width="match_parent"
android:id="#+id/editTextComments"
android:hint="Comments here"
android:layout_gravity="fill_horizontal"
android:layout_height="wrap_content"
android:inputType="text"/>
</LinearLayout>
I'm not sure where the casting exception is caused or why exactly it happened. If anyone is able to help, I would appreciate it a lot! Thanks in advance!
Instead of getting the view by position getting it by id would be easier.
EditText editTextQ = (EditText) linearLayout.getChildAt(2);
EditText editTextC = (EditText) linearLayout.getChildAt(4);
Try this instead:
EditText editTextQ = (EditText) linearLayout.findViewById(R.id.editText1);
EditText editTextC = (EditText) linearLayout.findViewById(R.id.editTextComments);
This will be better because if change the xml, it could change view positions, and find views by id you wouldn't need to change the code.
I will make a timetable for my organizer app. My problem is:
- I have make a first row and make the second and every other row like this, but dynamically.
- I have a layout.xml for this, but the margin between the Views doesn´t work.
I want make the second row like the blue row/ first row. The yellow row should be dynamically (on runtime).
My sourcecode:
public class StundenplanActivity extends AppCompatActivity {
TableLayout tl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stundenplan);
//Find the TableLayout defined in activity_stundenplan
tl = (TableLayout)findViewById(R.id.myTableLayout);
createRow();
}
public void createRow(){
/* Create a new row to be added. */
LayoutInflater inflater = getLayoutInflater();
TableRow tr = (TableRow) inflater.inflate(R.layout.tablerow_tabelle,null);
String[] day = {"Zeit", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"};
TextView[] spalte = new TextView[8];
for(int i=0; i<=7; i++){
TextView text = (TextView) inflater.inflate(R.layout.text_view_tabelle, null);
text.setText(day[i]);
spalte[i] = text;
}
for(int i=0; i<=7; i++) {
tr.addView(spalte[i]);
}
tl.addView(tr);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_stundenplan, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.item_einstellungen_stundenplan) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My layout xml for the dynamically textviews:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:background="#f0d000"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop">
</TextView>
Edit: xml for tablerow_tabelle
<?xml version="1.0" encoding="utf-8"?>
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1cf000">
</TableRow>
And the xml for the first Row (not dynamically):
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/mainactivity_horizontal_margin"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text0"
android:text="#string/Zeit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text1"
android:text="#string/Wochentag0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text2"
android:text="#string/Wochentag1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text3"
android:text="#string/Wochentag2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text4"
android:text="#string/Wochentag3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text5"
android:text="#string/Wochentag4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text6"
android:text="#string/Wochentag5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text7"
android:text="#string/Wochentag6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
</TableRow>
</TableLayout>
Here this code works good to make different coloring for different rows. Well, it may be helps you.
In my code, i was used two rows. The first Table layout was Header row and second table layout is value rows. I was set background of header as background image and value layouts having background color.
In XML Layout:
<LinearLayout
android:id="#+id/gridValue1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableLayout
android:id="#+id/GridFrozenTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:layout_marginTop="2dip"
android:stretchColumns="*"
/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_marginTop="2dip"
android:layout_toRightOf="#id/GridFrozenTable">
<TableLayout
android:id="#+id/GridContentTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:stretchColumns="*" />
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
In Java code :
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void PopulateMainTable(ActionCodeReport[] content) {
TableRow HeaderRow = new TableRow(this);
HeaderRow.setPadding(0, 0, 0, 2);
TextView HeaderCell1 = new TextView(this);
HeaderCell1.setText("Action code ");
HeaderCell1.setTextColor(Color.BLACK);
HeaderCell1.setHeight(65);
HeaderCell1.setGravity(Gravity.CENTER_HORIZONTAL);
HeaderCell1.setBackgroundResource(R.drawable.lt); //Here i set background image for header
HeaderRow.addView(HeaderCell1);
TextView HeaderCell2 = new TextView(this);
HeaderCell2.setText("NoOfAccnt");
HeaderCell2.setHeight(65);
HeaderCell2.setBackgroundResource(R.drawable.lt); //Same image
HeaderCell2.setGravity(Gravity.CENTER_HORIZONTAL);
HeaderRow.addView(HeaderCell2);
HeaderCell2.setTextColor(Color.BLACK);
TableRow contentRow = new TableRow(this);
contentRow.setPadding(0, 0, 0, 2);
int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
TextView Cell1 = new TextView(this);
Cell1.setText(r.getAction_Code());
Cell1.setTextColor(Color.BLACK);
Cell1.setGravity(Gravity.LEFT);
Cell1.setBackgroundColor(Color.LTGRAY); //**Here change your background color as you need**
contentRow.addView(Cell1);
TextView Cell2 = new TextView(this);
Cell2.setText(String.valueOf(r.getNoOfAccount()));
Cell2.setTextColor(Color.BLACK);
Cell2.setGravity(Gravity.CENTER_HORIZONTAL);
contentRow.addView(Cell2);
contentTable.addView(contentRow);
}
Surely, i may helps you!!
Happy Coding!!
This can help
TextView txtvew= (TextView) findViewById(R.id.ForgotPasswordText);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(50, 0, 0, 0)
txtvew.setLayoutParams(llp);
Setting the margin using layout XML file.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"/>
For changing some particular side margins use this code.
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
I need to show n number of Edit text added dynamically below a static Edit text.
I am adding the ET but some how it is not visible. What am doing wrong??
Here is my code:
private void addEditTextView(int numberOfViews){
// Using layout params same as above static ET
ViewGroup.LayoutParams layoutParams = staticEditText.getLayoutParams();
for (int index = 0; index <= numberOfViews; index++) {
final EditText newET = new EditText(getActivity());
//Added below 2 lines just to make sure width and height are coming
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
newET.setLayoutParams(layoutParams);
newET.setHint("Select ME");
newET.setFocusable(false);
newET.setId(index);
newET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO set time
Log.d("Clicked...",newET.getId()+"");
}
});
//parentLayout is Linear Layout with Vertical orientation
parentLayout.addView(newET);
}
}
XML code :
<LinearLayout
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/staticEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#drawable/rectangle_bg_dark_gray_border"
android:focusable="false"
android:hint="staticEditText"
android:padding="7dp" />
</LinearLayout>
UPDATE : parentLayout.getChildCount() is coming correctly. New views are getting added but not visible!
Hi try adding a Scrollview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/parentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- EDITTEXT here -->
</LinearLayout>
</ScrollView>
</LinearLayout>
So, I'm trying to create a screen that when clicking a button this takes the data from some EditText and add them to a ListView item, now I know there are a lot of examples on the web and I've done them and they work, but when I took them to what I want to do it just adds one item and stops working it doesn't throw any exception or anything, it just add one item, this is what I got so far...
create_class.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.eduardolaguna.mariela.app.activities.CreateClass">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/cc_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/cc_hint_class_name" />
<View
android:id="#+id/cc_divider1"
style="#style/Divider"
android:layout_below="#+id/cc_name" />
<TextView
android:id="#+id/tv_cc_professors_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cc_divider1"
android:text="#string/cc_tv_professors_data" />
<EditText
android:id="#+id/cc_professors_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_cc_professors_data"
android:hint="#string/cc__hint_professors_name"
android:inputType="textPersonName|textAutoComplete|textAutoCorrect" />
<EditText
android:id="#+id/cc_professors_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cc_professors_name"
android:hint="#string/cc_hint_professors_email"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/cc_professors_phonenumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cc_professors_email"
android:hint="#string/cc_hint_professors_phonenumber"
android:inputType="phone" />
<View
android:id="#+id/cc_divider2"
style="#style/Divider"
android:layout_below="#id/cc_professors_phonenumber" />
<TextView
android:id="#+id/tv_cc_schedule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cc_divider2"
android:text="#string/cc_tv_class_schedule" />
<Spinner
android:id="#+id/sp_cc_day_of_the_week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_cc_schedule"
android:entries="#array/dow" />
<EditText
android:id="#+id/cc_from_schedule"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/sp_cc_day_of_the_week"
android:hint="#string/cc_hint_from_schedule"
android:inputType="time" />
<EditText
android:id="#+id/cc_to_schedule"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/sp_cc_day_of_the_week"
android:layout_toRightOf="#id/cc_from_schedule"
android:hint="#string/cc_hint_to_schedule"
android:inputType="time" />
<EditText
android:id="#+id/cc_floor_number"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/sp_cc_day_of_the_week"
android:layout_toRightOf="#id/cc_to_schedule"
android:hint="#string/cc_hint_floor"
android:inputType="number" />
<EditText
android:id="#+id/cc_classroom"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="#id/sp_cc_day_of_the_week"
android:layout_toRightOf="#id/cc_floor_number"
android:hint="#string/cc_hint_classroom" />
<Button
android:id="#+id/btn_cc_add_schedule"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cc_from_schedule"
android:text="#string/cc_add_schedule" />
<ListView
android:id="#+id/cc_schedule_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/btn_cc_add_schedule" />
</RelativeLayout>
</ScrollView>
</LinearLayout>
schedule_item.xml
<?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="vertical">
<TextView
android:id="#+id/sch_day_of_the_week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Miércoles"
android:textSize="60dp" />
<TextView
android:id="#+id/sch_from"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5:45"
android:textSize="20dp" />
<TextView
android:id="#+id/sch_to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7:15"
android:textSize="20dp" />
<TextView
android:id="#+id/sch_floor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7"
android:textSize="20dp" />
<TextView
android:id="#+id/sch_clasroom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="lab1"
android:textSize="20dp" />
</LinearLayout>
ScheduleAdapter.java
public class ScheduleAdapter extends ArrayAdapter<Actividad> {
private int layoutResourceId;
private LayoutInflater inflater;
private List<Actividad> shifts;
public ScheduleAdapter(Context context, int resource, List<Actividad> objects) {
super(context, resource, objects);
layoutResourceId = resource;
shifts = objects;
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Holder holder = null;
row = inflater.inflate(layoutResourceId, null);
holder = new Holder();
holder.actividad = shifts.get(position);
holder.dow = (TextView) row.findViewById(R.id.sch_day_of_the_week);
holder.fromTXT = (TextView) row.findViewById(R.id.sch_from);
holder.toTXT = (TextView) row.findViewById(R.id.sch_to);
holder.floorTXT = (TextView) row.findViewById(R.id.sch_floor);
holder.roomTXT = (TextView) row.findViewById(R.id.sch_clasroom);
setupItem(holder);
row.setTag(holder);
return row;
}
private void setupItem(Holder holder) {
holder.dow.setText(holder.actividad.getDiaDeLaSemana());
holder.fromTXT.setText(holder.actividad.getDesdeStr());
holder.toTXT.setText(holder.actividad.getHastaStr());
holder.floorTXT.setText(holder.actividad.getPiso());
holder.roomTXT.setText(holder.actividad.getSalon());
}
public static class Holder {
Actividad actividad;
TextView dow;
TextView fromTXT;
TextView toTXT;
TextView floorTXT;
TextView roomTXT;
}
}
And finally the CreateClass.java
public class CreateClass extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_class);
ListView view = (ListView) findViewById(R.id.cc_schedule_list);
final ScheduleAdapter adapter = new ScheduleAdapter(getApplicationContext(), R.layout.schedule_item, new ArrayList<Actividad>());
view.setAdapter(adapter);
Button addSchedule = (Button) findViewById(R.id.btn_cc_add_schedule);
addSchedule.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Resources res = getResources();
ListView scheduleView = (ListView) findViewById(R.id.cc_schedule_list);
ScheduleAdapter adapter1 = (ScheduleAdapter) scheduleView.getAdapter();
Actividad act = new Actividad(TipoEvento.CLASE);
Spinner dow = (Spinner) findViewById(R.id.sp_cc_day_of_the_week);
act.setDiaDeLaSemana(dow.getSelectedItem().toString());
TextView from = (TextView) findViewById(R.id.cc_from_schedule);
act.setDesdeStr(res.getString(R.string.from) + ": " + from.getText().toString());
TextView to = (TextView) findViewById(R.id.cc_to_schedule);
act.setHastaStr(res.getString(R.string.to) + ": " + to.getText().toString());
TextView floor = (TextView) findViewById(R.id.cc_floor_number);
act.setPiso(res.getString(R.string.floor) + ": " + floor.getText().toString());
TextView classroom = (TextView) findViewById(R.id.cc_classroom);
act.setSalon(res.getString(R.string.classroom) + ": " + classroom.getText().toString());
adapter1.add(act);
}
});
}
}
You should instantiate your adapter with the Activity context, not the application context.
Storing a copy of the constructed list in shifts can be dangerous.
Don't store shifts in the Holder. Only views go in there.
When populating the convertView, reference the internal getItem(position) method to obtain the Actividad data...not your shifts variable.
You're also using the ViewHolder paradigm incorrectly. Example here.
Turns out the problem is the ListView is inside the ScrollView, this does not work properly when the list grows dynamically, I move it out the ScrollView and it works like a charm.
This was according to #Romain Guy a developer in the Android project, where he stated that
Using a ListView to make it not scroll is extremely expensive and goes against the whole purpose of ListView. You should NOT do this. Just use a LinearLayout instead.
So I use a LinearLayout to draw the new items on the layout.