I have a spinner, and have a button right to the spinner. When the button is clicked, new spinner is created under the existed spinner. It is created with a remove button at the right.
so my problem is :-
1) How can I get the value of the new spinner if I added two or three new spinners. Because if I added more than one new spinner. The value will be replaced all over.
2) How can I remove the value of new spinner when i clicked on remove button.
Here are my codes:
btnAddRow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.add_row, null);
spBidang2 = (Spinner) addView.findViewById(R.id.spSpecification2);
Button btnRemRow = (Button) addView.findViewById(R.id.btnRemRow);
btnRemRow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
spBidang2.setVisibility(View.GONE);
}
});
container.addView(addView);
}
});
FOr xml :-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.25"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="36dp"
android:background="#206531"
android:gravity="center"
android:text="Pengkhususan"
android:textColor="#ffffff" />
<Space
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.75"
android:orientation="vertical"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5" >
<Spinner
android:id="#+id/spSpecification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="0.5"
android:background="#drawable/field_shape"
android:entries="#array/pengkhususan"
android:singleLine="false" />
<Button
android:id="#+id/btnAddRow"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="+" />
</LinearLayout>
<LinearLayout
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
This is the xml for new spinner row :-
<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="wrap_content"
android:weightSum="2" >
<Spinner
android:id="#+id/spSpecification2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="0.5"
android:background="#drawable/field_shape"
android:entries="#array/pengkhususan"
android:singleLine="false" />
<Button
android:id="#+id/btnRemRow"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:text="-" />
</LinearLayout>
For getting value of new spinners use ArrayList of Spinners
eg.
ArrayList<Spinner> spinners = new ArrayList<Spinner>();
then when you create new spinner add it to the list
i.e. spinners.add(newSpinner)
you can get value by
spinners.get(index).getSelectedItem().toString()
Better create a layout with all the spinners you need and make their visibility gone. Make only the ones needed initially visible, hide rest others and when you need to add new row, make it visible. This way you can access the value of spinner using its id and even make it disappear while clicking right clear button.
Hope this helps :)
Refer this :
http://www.edumobile.org/android/android-development/dynamic-spinner/
This example will show how to create spinner with list view at run time in android. In this example you will be able to assign the contents of a spinner to another spinner at run time on a button click.
Hope this may help you!
Related
I would like to set text for the spinners in the layout reused. However, only the first spinner is set. How to set text for all spinners with same id?
Also, I would like to ask how add another skillfield.xml to fragment_skill.xml when clicking the imageview?
Thank you.
fragment_skill.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/skillInfo" >
<RelativeLayout
android:id="#+id/OCC"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/occ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="#string/occSkill"
android:textSize="20sp" />
<TextView
android:id="#+id/occPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toEndOf="#id/occ"
android:text=""
android:textSize="20sp" />
<include
android:id="#+id/oocSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/occ"
layout="#layout/skillfield" />
<ImageView
android:id="#+id/addOccSkill"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#id/oocSkill"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/ORI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/OCC" >
<TextView
android:id="#+id/ori"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="#string/oriSkill"
android:textSize="20sp" />
<TextView
android:id="#+id/oriPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toEndOf="#id/ori"
android:text=""
android:textSize="20sp" />
<include
android:id="#+id/oriSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ori"
layout="#layout/skillfield" />
<ImageView
android:id="#+id/addOriSkill"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#id/oriSkill"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
</RelativeLayout>
skillfield.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/addSkillField" >
<Spinner
android:id="#+id/selectSkill"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp" />
<TextView
android:id="#+id/skillPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text=""
android:textSize="20sp" />
<ImageView
android:id="#+id/addSkill"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_delete" />
</LinearLayout>
In MainActivity.class
skill = (Spinner) findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
how to setup Spinner with the same ID
It is true that if you do this:
skill = (Spinner) findViewById(R.id.selectSkill);
You will be only able to get the first Spinner. I don't think you can add new Spinner dynamically by using <include>.
Here's the main idea of how to achieve what you need:
In fragment_skill.xml, you need to add an empty container view. This container should be inserted in the place you want to add your Spinner every time the button is clicked. This container will hold your Spinners that are added.
Let's say you want to insert here:
<RelativeLayout...>
<TextView.../>
<TextView.../>
<!-- here is where you want to insert your Spinner -->
<ImageView.../>
</RelativeLayout...>
So you add a LinearLayout at that spot, like this:
<RelativeLayout...>
<TextView.../>
<TextView.../>
<LinearLayout
android:id="#+id/container"
android:orientation="vertical
... />
<ImageView.../>
</RelativeLayout...>
You can add any other type of layout, depending what you need, it doesn't have to be LinearLayout. After that, you grab that LinearLayout from your activity/fragment like this:
LinearLayout mContainer; // make this instance variable
mContainer = (LinearLayout)findViewById(R.id.container);
Also grab the button so that you can set up a listener, let's assume it's called Button mButton:
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNewSpinner();
});
So when the button is clicked, you will call addNewSpinner(). Now, make a method called addNewSpinner(). Inside, the method, we will be inflating skillfield.xml. The method should look something like this:
public void addNewSpinner(){
View view = getLayoutInflater().inflate(R.layout.skillfield, null);
Spinner skill = (Spinner) view.findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
mContainer.addView(skill);
}
If you are not sure what inflate means, here is the explanation:
Instantiates a layout XML file into its corresponding View objects.
Hope it helps!
Good day.
I have an android application. In my application, I have a custom listView with 5 columns that are textViews. The user can click the rows, once he does, the row layout will change, changing the last 2 textViews to EditTexts. I then register the new EditTexts onto my custom keyboard taken from this example - kindly note that I did a functional copy-paste of his example with regards to the custom keyboard class and how to make it work in the main layout. However, when I click the EditText in the row, my custom keyboard does not show up at all.
I have a global variable as such:
CustomKeyboard mCustomKeyboard;
And in my onCreate() method in the activity, I do:
mCustomKeyboard= new CustomKeyboard(this, R.id.keyboardview, R.xml.custom_keyboard);
This is my layout, I have the KeyboardView at the bottom of the Layout:
<RelativeLayout 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"
tools:context=".SearchResult" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- A lot of views go here, enclosed in my linear layout -->
</LinearLayout>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>
Here is the code that changes the layout. What I do is that I take the row values from the old layout, get the new layout search_result_inflate, then set the texts of the new layout using the values I got. Kindly note the mCustomKeyboard.registerEditText(R.id.qtyInputSearchResult); line after inflating the layout:
private void changeLayout(final View view){
//get views from old layout
TextView textViewQuantity = (TextView)view.findViewById(R.id.qtyInput);
TextView textViewDiscountReq = (TextView)view.findViewById(R.id.discInput);
TextView textViewName = (TextView)view.findViewById(R.id.dialogItemName);
TextView textViewPrice = (TextView)view.findViewById(R.id.price);
TextView textViewDiscount = (TextView)view.findViewById(R.id.discount);
//store values in strings
String itemName = textViewName.getText().toString();
String itemPrice = textViewPrice.getText().toString();
String itemDiscount = textViewDiscount.getText().toString();
String itemQty = textViewQuantity.getText().toString();
String itemDisc = textViewDiscountReq.getText().toString();
//set the view to gone
textViewQuantity.setVisibility(View.GONE);
textViewDiscountReq.setVisibility(View.GONE);
textViewName.setVisibility(View.GONE);
textViewPrice.setVisibility(View.GONE);
textViewDiscount.setVisibility(View.GONE);
//get the old layout
LinearLayout ll_inflate = (LinearLayout)view.findViewById(R.id.search_result_layout);
//get the inflate/new view
View child = getLayoutInflater().inflate(R.layout.search_result_inflate, null);
//get the views in the new view, populate them
TextView newName = (TextView)child.findViewById(R.id.dialogItemName);
newName.setText(itemName);
TextView newDiscount = (TextView)child.findViewById(R.id.discount);
newDiscount.setText(itemDiscount);
TextView newPrice = (TextView)child.findViewById(R.id.price);
newPrice.setText(itemPrice);
EditText qtyInput = (EditText)child.findViewById(R.id.qtyInputSearchResult);
qtyInput.setText(itemQty);
EditText discInput = (EditText)child.findViewById(R.id.discInputSearchResult);
discInput.setText(itemDisc);
//show new layout
ll_inflate.removeAllViews();
ll_inflate.removeAllViewsInLayout();
ll_inflate.addView(child);
mCustomKeyboard.registerEditText(R.id.qtyInputSearchResult);
}
Here is my search_result_inflate.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_result_inflate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="1dip"
android:weightSum="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:padding="1dip" >
<TextView
android:id="#+id/dialogItemName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_weight="0.54"
android:gravity="center"
android:text="Item Name"
android:textSize="23sp" />
<TextView
android:id="#+id/price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_weight="0.14"
android:gravity="center"
android:text="Price"
android:textSize="23sp" />
<TextView
android:id="#+id/discount"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_weight="0.10"
android:gravity="center"
android:text="Discount"
android:textSize="23sp" />
<EditText
android:id="#+id/qtyInputSearchResult"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"
android:layout_weight="0.14"
android:background="#layout/edittext_selector"
android:gravity="center"
android:hint="qtyInput"
android:textColorHighlight="#color/white_opaque"
android:textSize="23sp" >
</EditText>
<EditText
android:id="#+id/discInputSearchResult"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.11"
android:background="#layout/edittext_selector"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="discInput"
android:textColorHighlight="#color/white_opaque"
android:textSize="23sp" />
</LinearLayout>
</LinearLayout>
As you can see, I have 2 editTexts and I registered qtyInputSearchResult to the custom keyboard class. However, the custom keyboard does not show up.
I also tried to use the custom keyboard class on an editText in another activity and it works just fine. Am I missing something here? I'm confused as to why the custom keyboard does not show up properly.
Any help is very much appreciated, thank you.
Got it, I placed the keyboard layout in the search_result_inflate.xml like so:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_result_inflate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:minHeight="50dp"
android:orientation="horizontal"
android:padding="1dip"
android:weightSum="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:padding="1dip" >
<!-- a lot of components here -->
</LinearLayout>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>
I have a custom layout item for my listview. There is a spinner in the layout item which needs to be populated with values, usually by android:entries
The problem I have with this method is that the end user can't modify the values, which is something I would like to include. As the layout item and subsequently the spinner, is repeated multiple times on the same listview, I imagine there must be a way to populate it once, programmatically. I just can't figure it out.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customListItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exerciselbl" />
<Spinner
android:id="#+id/Exercise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/workout_items" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/repslbl" />
<Spinner
android:id="#+id/Reps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/reps_count" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/weightlbl" />
<EditText
android:id="#+id/Weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
</LinearLayout>
So yeah, I want to avoid using android:entries="#array/workout_items" as this means manually typing out every single item for the spinner in an XML resources file AND I can't dynamically add items while the program is running.
Here is a simple example of how it would be done, obviously the generics can change and there are other ways of loading the spinner.
public void populateSpinner () {
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
List<CharSequence> list = new ArrayList<CharSequence>();
for (int i = 0; i < 10; i++) {
list.add("Item " + i);
}
adapter.addAll(list);
adapter.notifyDataSetChanged();
}
One of the more important lines is adapter.notifyDataSetChanged(), this is because it tells the view that it needs to refresh the data associated with this spinner.
For more information, see the ArrayAdapter and Spinner classes.
I want to create a multirow Button list. Something like this :
but I want to do it dynamically(in code). Is there a way to tell layout to do this automatically? Or i have to do this myself usingRelativeLayout.LayoutParams. I can do this by code but I should control so many things and I was wondering if there is another easier way to do this. For example tell layout to add elements in the next row when the current one is full!
You could also do this with LinearLayout and make all the buttons the same size using weight.
As for your question:
i can do this by code but i should control so many things and i was
wondering if there is another easier way to do this. for example tell
layout to add elements in the next row when the current one is full!
This is potentially possible if you measure the screen width and height and use the Functions in in the View class to figure out the specifics of that particular view and its children.
Alternative
But as mentioned in the comments, there are other views that you can use to solve your problem like GridView.
You can also use a table layout ,
Create first row of tabllayout in xml like this
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/goBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginTop="12dp"
/>
</LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="0.80">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:layout_weight="0.80"
android:background="#f0ffff" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/data_table"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#006400"
android:orientation="vertical" >
<TableRow
android:id="#+id/second"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:text="Number1"
android:textColor="#006400" >
</TextView>
<Button
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:text="Number2"
android:textColor="#006400" />
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="#drawable/textbg"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingTop="2dp"
android:text="Distance"
android:textColor="#006400" >
</TextView>
<TextView
android:layout_width="105dp"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="5dp"
android:paddingTop="8dp"
android:text="F/G/H/S"
android:textColor="#006400" >
</TextView>
</TableRow>
</TableLayout>
</ScrollView>
</FrameLayout>
</HorizontalScrollView>
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/savescore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I created this xml for four three texts and on button in a single row
refer to the table in onCreate
TableLayout extendeedTable = (TableLayout)findViewById(R.id.data_table);
add rows like
while (extendeedTable.getChildCount() > 1)
{
// while there are at least two rows in the table widget, delete
// the second row.
extendeedTable.removeViewAt(1);
}
// collect the current row information from the database and
// store it in a two dimensional ArrayList
// iterate the ArrayList, create new rows each time and add them
// to the table widget.
// Here value is the number of rows you want in table
for (int position=0; position < value ; position++)
{
TableRow tableRow= new TableRow(this);
tableRow.setBackgroundDrawable(null);
// ArrayList<Object> row = data.get(position);
TextView idText = new TextView(this);
idText.setText(Integer.toString(position + 1));
idText.setGravity(Gravity.CENTER);
idText.setTextColor(Color.BLACK);
idText.setWidth(10);
idText.setHeight(45);
idText.setBackgroundResource(R.drawable.text2);
tableRow.addView(idText);
textOne = new Button(this);
textOne.setText("CLUB");
textOne.setBackgroundResource(R.drawable.text2);
textOne.setGravity(Gravity.CENTER);
textOne.setTextColor(Color.BLACK);//left top right bottom
textOne.setWidth(10);
textOne.setHeight(45);
textOne.setId(1+position);
tableRow.addView(textOne);
allbtns.add(textOne);
// textOne.setOnClickListener(this);
textOne.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// do something when the button is clicked
final Button button = (Button) arg0;
System.out.println("value of button is "+
button.getId());
dialog1.setTitle(" SELECT CLUB ");
textTwo = new EditText(this);
textTwo.setText("");
textTwo.setBackgroundResource(R.drawable.text2);
textTwo.setGravity(Gravity.CENTER);
textTwo.setTextColor(Color.BLACK);
textTwo.setWidth(10);
textTwo.setHeight(45);
textTwo.setInputType(InputType.TYPE_CLASS_NUMBER);
tableRow.addView(textTwo);
allEds1.add(textTwo);
textTwo.setId(position +1);
textThree = new EditText(this);
textThree.setText("");
textThree.setWidth(10);
textThree.setHeight(45);
textThree.setTextColor(Color.BLACK);
textThree.setBackgroundResource(R.drawable.text2);
textThree.setGravity(Gravity.CENTER);
textThree.setInputType(InputType.TYPE_CLASS_TEXT);
tableRow.addView(textThree);
allEds2.add(textThree);
textThree.setId(position +1);
extendeedTable.addView(tableRow);
}
for this i took help from here
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/7/
and
its xml
http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/6/
after some more google search i finally found the best way to do this. it's so clean and simple, using Adapters and grids.
thanks for all the answers
here is a Tutorial: Creating a Custom Adapter for Gridview(ButtonAdapter)
-- This is my layout thus far for the input --
<?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="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/etClassName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:hint="#string/className"/>
<EditText
android:id="#+id/etAssignmentName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:ems="10"
android:hint="#string/assignmentName"/>
<EditText
android:id="#+id/etDueDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginBottom="20dp"
android:hint="#string/dueDate" />
<EditText
android:id="#+id/etNotes"
android:layout_width="match_parent"
android:layout_height="194dp"
android:ems="10"
android:inputType="textMultiLine"
android:layout_marginBottom="5dp"
android:hint="#string/notes"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/bAddAssignments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/addAssignment"
android:layout_weight="1" />
<Button
android:id="#+id/bViewAssignments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/viewAssignments"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
-- Clicking the add button brings you to this page which is supposed to list the assignment name that you can click to view your assignment --
<?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" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.63" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/bNewAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/newAssignment" />
<Button
android:id="#+id/bdeleteAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/deleteAssignment" />
</LinearLayout>
</LinearLayout>
My problem is that I don't know how to populate the second layout when my first layout keeps track of the data. How does the second layout know what the user inputted etc in order to populate the listview
I currently have a dbadapter class(with helper class inside), an entry class(creates entry objects), and an assignmentclass(supposed to display listview)
Any Ideas, please?
Pass the data you need in an Intent bundle.
When you set the intent, put the data you need in ther next class as an extra (assuming strings here, but can be other forms):
Intent myIntent = new Intent(YourActivity.this, NewActivity.class);
myIntent.putExtra("Data1", data1var);
myIntent.putExtra("Data2", data2var);
YourActivity.this.startActivity(myIntent);
In the new class:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value1 = extras.getString("Data1");
String value2 = extras.getString("Data2");
}
//use your passed strings
}
Your description of the problem is rather vague. I can refer you to one of the tutorials Lars Vogella made about how to use SQLLite together with a ListView. They explain everything nice and simple. I am sure that you will find your answer there!
Update:
onClickListener of the Add-button in InputActivity:
Add your user input to the database (with you DatabaseHelper)
List item Start you second activity including the ListView
onCreate of the ListActivity:
Create for example an Array
Let your DatabaseHelper fill the array with the records you want in the database
Create an Adapter giving you filled array with it