-- 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
Related
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!
I am working on an Android home screen app widget that fills a listview with custom defined row layouts.
The listview gets populated correctly, but when I click any of the items, nothing happens.
Here is some of the relevant parts
From the AppWidgetManager's onUpdate()
...
final Intent contactIntent = new Intent(context, ContactService.class);
contactIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
contactIntent.setData(Uri.parse(contactIntent.toUri(Intent.URI_INTENT_SCHEME)));
view.setRemoteAdapter(widgetId, R.id.listview, contactIntent);
//works fine up to here, populates ListView
//Set up pendingIntentTemplate
final Intent contactClick = new Intent(context,ContactDial.class);
contactClick.setData(Uri.parse(contactClick.toUri(Intent.URI_INTENT_SCHEME)));
final PendingIntent contactClickPI = PendingIntent.getBroadcast(context,
0,contactClick,PendingIntent.FLAG_UPDATE_CURRENT);
view.setPendingIntentTemplate(R.id.listview, contactClickPI);
Log.d("TEMPLATE", "set template");
...
From the RemoteViewsFactory's getViewAt(int position)
final Intent i = new Intent();
final Bundle extra = new Bundle();
extra.putString("number", phoneNumber);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtras(extra);
row.setOnClickFillInIntent(R.id.contact_row,i);
Log.d("FACTORY", "Set Intent Template");
The logs show that the code was executed correctly, but for some reason it doesn't seem to register my clicks. I put a Log.d in the onCreate of the class that the intent is supposed to be sent to, but it never happens.
Here are the important parts of my layouts.
The main layout:
<LinearLayout
android:id="#+id/layout_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="260dp" />
<!-- Some other views -->
</LinearLayout>
The custom defined row:
<LinearLayout android:id="#+id/contact_row"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#xml/buttoncolor_selector"
android:paddingBottom="2dp"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingLeft="10dip" >
<ImageView
android:id="#+id/contact_photo"
android:layout_width="#android:dimen/notification_large_icon_width"
android:layout_height="match_parent"
android:layout_marginRight="5dip"
android:src="#drawable/ic_action_person />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="#+id/contact_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="18sp"
android:gravity="center_vertical"
android:singleLine="true"
android:ellipsize="marquee"
android:textIsSelectable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>
<TextView
android:id="#+id/contact_number"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:textIsSelectable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false" />
</LinearLayout>
</LinearLayout>
I've looked at just at just about every example I can find on the Internet, but none of the solutions have changed anything.
Please help.
In your custom defined row, try moving android:id="#+id/contact_row" down to a child view (even if it's a "useless" viewgroup that just surrounds everything else). Or, surround your top level LinearLayout with a FrameLayout. I didn't have luck having the setOnClickFillInIntent() set to target the ID on the very top level viewgroup like that.
Like so,
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout android:id="#+id/contact_row"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#xml/buttoncolor_selector"
android:paddingBottom="2dp"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingLeft="10dip" >
<ImageView
android:id="#+id/contact_photo"
android:layout_width="#android:dimen/notification_large_icon_width"
android:layout_height="match_parent"
android:layout_marginRight="5dip"
android:src="#drawable/ic_action_person" />
........
Depending on your needs, you may be able to eat your cake and have it too, and have a non-useless viewgroup if you rearrange your layout.
I have one Activity with a list of items. By clicking an item, it brings up a second Activity with details of the item clicked in the first Activity.
My first activity: (I am passing a string[] via Intent)
Bundle bundle= new Bundle();
bundle.putStringArray("item_details", new String[]{item_name, item_desc, item_date, item_loc});
Intent intent = new Intent(getApplicationContext(), DataProductDetail.class);
intent.putExtras(extras);
My second activity: (receiving string[])
Bundle bundle = this.getIntent().getExtras();
String[] itemArray = bundle.getStringArray("item_details");
ArrayAdapter<String> itemArrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, itemArray );
ListView list = (ListView)findViewById(R.id.list);
detailsList.setAdapter(itemArrayAdapter );
Layout of list_item:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="18sp"
android:textColor="#0c2d63" />
My Question/Problem:
I want my first screen on the app to look like this (which is working):
item1
item2
item3
Problem:
The second screen when item1 is clicked in the first Activity should be like this (not sure how to get this, above code doesn't work):
Name: xyz is "item_name" from array "itemArray" received from intent
Description: This is the description from "item_desc" from array "itemArray"
Date: This is the date string grabbed from item_date from array "itemArray"
How can I get such a layout as shown in the second screen above? I want Name, Description, Date to be shown on the second screen and grab certain elements of an array into each for display as item details.
As you asked here is an example of layout xml file for the second Activity:
<?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"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Name: " />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xyz is item_name from array itemArray received from intent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Description: " />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the description from item_desc from array itemArray" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Date: " />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the date string grabbed from item_date from array itemArray" />
</LinearLayout>
</LinearLayout>
And the screenshot:
For the TextViews with IDs name, description and date you set text programmatically calling setText() method.
Of course, as it's just an example you might need to add some attributes for aligning the text etc.
So I've been trying to figure out layouts and using the layout inflater, but I'm running into some issues. I have two relative xml layout files, the one xml file has two textViews, an editText field, and a spinner object:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/goalNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dip"
android:text="#string/goalName" />
<EditText
android:id="#+id/goal_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/goalNameView"
android:hint="#string/editText"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/goalTasksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/goal_tasks"
android:layout_alignBottom="#+id/goal_tasks"
android:layout_alignLeft="#+id/goalNameView"
android:text="#string/goalTasks" />
<Spinner
android:id="#+id/goal_tasks"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/goalTasksView"
android:layout_marginTop="51dp" />
</RelativeLayout>
the other xml file has one textView and one editText field:
<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:id="#+id/tView"
>
<EditText
android:id="#+id/taskNameField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/taskNameView"
android:ems="10"
android:hint="#string/editText" />
<TextView
android:id="#+id/taskNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/taskNameField"
android:layout_alignBottom="#+id/taskNameField"
android:layout_alignParentLeft="true"
android:text="#string/taskName"
/>
</RelativeLayout>
I'm trying to patch the two layouts together with a third xml file (main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/theMain"
>
<include android:id="#id/mainLayout" layout="#layout/activity_goal_keeper" />
</LinearLayout>
The way I'm looking to have the layout work is to display the first layout (#id/firstLayout) and then the second layout (#id/tView) directly beneath it. I've read that I need to implement the layouts on a LinearLayout to achieve this which is why I have the third layout (#id/theMain).
As you can see I have an include statement for #id/firstLayout, but I don't have an include statement for #id/tView because I'm trying to inflate multiple versions of #id/tView via my main activity:
public class GoalKeeperActivity extends Activity implements AdapterView.OnItemSelectedListener {
public Integer[] items= {1,2,3,4,5,6,7,8};
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.activity_goal_keeper);
setContentView(R.layout.main);
Spinner numOfTasks=(Spinner) findViewById(R.id.goal_tasks);
numOfTasks.setOnItemSelectedListener(this);
ArrayAdapter<Integer> aa = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, items);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
numOfTasks.setAdapter(aa);
ViewGroup item = (ViewGroup) findViewById(R.id.theMain);
for(int i=0; i<3;i++)
{
View child = getLayoutInflater().inflate(R.layout.tasks_view, item, false);
child.setId(i);
item.addView(child);
}
My problem is that #firstLayout displays properly, but #tView doesn't show up at all. Can someone please explain what I'm missing?
Thanks in advance.
I figured it out, it was simply an issue of setting the "android:layout_height" attribute equal to wrap_content on the #id/tView xml file. Silly little mistakes...
The answer to this question might be really obvious but its giving me a headache. I have a simple LinearLayout with one single ListView in it. I do this: onCreate
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.friends);
ListView listView = (ListView) findViewById(R.id.friend_list);
listAdapter = new CheckinListAdapter(checkins, listView, R.layout.checkin_list_item);
listView.setAdapter(listAdapter);
if (getLastNonConfigurationInstance() != null) {
FriendsActivity last = (FriendsActivity) getLastNonConfigurationInstance();
this.checkins.addAll(last.checkins);
this.sort = last.sort;
} else {
refresh();
}
registerForContextMenu(listView);
}
But for some reason onCreateContextMenu never gets called! So I did some research and since I am loading the list after the register, perhaps it doesn't register it correctly. If I go in my ListAdapter and do registerForContextMenu it does show up. But it doesn't behave correctly with the keyboard. So I am now confused on what can be the error because it seems a little non intuitive for me to have to register each child item. All the examples I find online are using ArrayAdapter. :(
Any suggestions?
Edit
Here is more detail, in case it something I don't see:
My XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="#string/check_in"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onCheckInClicked"/>
<ListView android:id="#+id/friend_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
List item xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<ImageView android:id="#+id/user_photo"
android:layout_width="40dip"
android:layout_height="40dip"
android:scaleType="centerCrop"/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="8dip">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="#+id/user" style="#style/TextButton"/>
<TextView android:text="#string/at"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="#+id/venue"
android:singleLine="true"
android:ellipsize="end"
style="#style/TextButton"/>
</LinearLayout>
<TextView android:id="#+id/venue_address" style="#style/GreyLarge"/>
<LinearLayout android:id="#+id/checkin_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip">
<ImageView android:id="#+id/checkin_image"
android:layout_width="70dip"
android:layout_height="60dip"
android:layout_marginRight="8dip"
android:scaleType="centerCrop"/>
<TextView android:id="#+id/checkin_shout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView android:id="#+id/elapsedTime" style="#style/GreySmall"/>
</LinearLayout>
</LinearLayout>
This took me 6 hours to figure out but it turns out I had to add:
android:focusable="false"
to all my <Button/> tags.
Related post: TextView and Button in each row and onListItemClick()
Do, registerForContextMenu(listView);
before setting the adapter, that is before:
listView.setAdapter(listAdapter);
You have mentioned that the menu is not responding correctly with the keyboard actions. Can you tell me what exactly the problem is?