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.
Related
I have a ListView with a TextView (which has an integer as string) and a Seekbar.
I need to extract the integer value and set it to the Seekbar.
Unfortunately, when I do this, I get a NullPointer exception because the content that populates the ListView item is not recognized by the onCreateView of the PlaceholderFragment.
String progressValue = percentileTextView.getText().toString();
bar.setProgress(Integer.parseInt(progressValue));
I have an activity layout for this activity, then a fragment layout which contains the listview and then I have a content.xml where the actual TextView is present. My question is how to reference this TextView? Am I doing this right, i.e. Activity layout contains a ConstraintLayout with Toolbars etc, then the Fragment Layout contains the ListView and I am using a separate content.xml and applying that to the ListView via a SimpleAdapter.
newAdapter = new SimpleAdapter(getContext(), newList,
R.layout.content, new String[] {
"name",
"percent"
},
new int[] {
R.id.name1,
R.id.percentile1
});
I hope you understand my problem.
When I do the following, it works (i.e. Android Studio understand that is the TextView) - but throws a null pointer exception upon build.
TextView percentileTextView = (TextView) rootView.findViewById(R.id.percentile1);
UPDATE 1 for pskink:
Newlist contents:
[{TA=Item1, IA=46}, {TA=Item2, IA=98}, {TA=Item3, IA=70}, {TA=Item4, IA=54}, {TA=Item5, IA=99}, {TA=Item6, IA=98}]
Row item layout (content.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/name1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textStyle="bold"
android:textColor="#color/colorAccent" />
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="5dp">
<TextView
android:id="#+id/greater_than"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="#string/greater_than" />
<TextView
android:id="#+id/percentile1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_toRightOf="#id/greater_than"
android:layout_toEndOf="#id/greater_than"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_toRightOf="#id/percentile1"
android:layout_toEndOf="#id/percentile1"
android:text="#string/percentile_text"/>
</RelativeLayout>
<SeekBar
android:id="#+id/seekBar2"
android:paddingTop="12dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end|bottom"
android:paddingTop="12dip"
android:orientation="horizontal">
<ImageView
android:contentDescription="info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/someimg" />
</LinearLayout>
</LinearLayout>
I'd like to be able to display the name and price of the product on the same line. Right now, it's displayed as:
Product name
Product price
I've tried adding a linearlayout with the orientation horizontal, tried using weightsum / weight and can't figure out how to make it show properly, like in a normal menu, with the name at the left and the price at the right. I also tried setting their gravity, but it always displays them on two rows. I'd like to display them as such:
Product name...........................Product Price
The layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#ffffff">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/Remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="Remove product(s)" />
</LinearLayout>
</LinearLayout>
How I'm adding the items:
oslist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put(PROD_NAME, name);
map.put(PROD_PRICE, price);
oslist.add(map);
adapter = new SimpleAdapter(shopping_cart.this, oslist,
R.layout.shoppingcart,
new String[] {PROD_NAME, PROD_PRICE}, new int[] {R.id.name, R.id.Price});
setListAdapter(adapter);
It's often easier to create a layout like this using a RelativeLayout as it gives you more control over the placement of views. You would give the name element layout_alignParentLeft="true" and the price element layout_alignParentRight="true" (and probably layout_gravity="right" to right align the text).
EDIT
Here's what your XML might look like. You won't actually need the layout_gravity attribute if you are using layout_width="wrap_parent", and I've omitted the remove button because I'm not sure where you want it to be.
This layout allows the name and price to overlap if they are too long - probably the best option to fix that is to put the name element after the price element in the XML and add layout_toLeftOf="#id/Price" to it. That forces it to wrap the name instead of overlap the price.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#ffffff">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="#+id/Price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</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 have a ListActivity where I have some items sorted by datetime.
I have 3 textviews. Here what I've done:
Here is the code for the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/bpSysHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" >
</TextView>
<TextView
android:id="#+id/bpDiaHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" >
</TextView>
<TextView
android:id="#+id/bpDtHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" >
</TextView>
</LinearLayout>
For calling them from my list activity i use:
String[] from = new String[] { BpDAO.bp_SYS, BpDAO.bp_DIA, BpDAO.bp_DT };
int[] target = new int[] { R.id.bpSysHolder, R.id.bpDiaHolder,
R.id.bpDtHolder };
dbAdapter = new SimpleCursorAdapter(this, R.layout.history_bp, bpList,
from, target);
setListAdapter(dbAdapter);
Now it is like this:
---120952012-09-20 15:05 ---
---145902012-09-20 10:44 ---
---145952012-09-20 10:42 ---
...
---14595 2012-09-20 10:42 ---
---------------------------------
How to have space between them and some little space up and down of the row so to be easier to touch?
you can do this also in same layout.
Change android:orientation="vertical" > to android:orientation="horizontal"
and define use the property "Android:layout_weight" for text-view to aline according to your reqirement.
Use this on the linear layout to have all in one line
android:orientation="horizontal"
On the picture you already have a title on the action bar, if you want on the list you can add a header to the list
you are setting the orientation as vertical:
android:orientation="vertical"
remove it, and you should get the textview horizzontal
-- 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