Hi I'm new to Android i want to create application similar to app given in this link
http://www.androiddom.com/2011/02/android-shopping-cart-tutorial.html?m=1
Only difference is that instead of list view in catalog activity I have image buttons.so wen I click any image button it goes to product details activity(same as example in link).Other activities are same as example in link.
Only thing I want is how to add item to list view by using button add to cart if there are image buttons in catalog activity.
Please do provide me help how to go about.
Main Layout 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">
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainListView">
</ListView>
Activity File
package com.example.android;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SimpleListViewActivity extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// Create and populate a List of context names.
String[] context = new String[] { "context1", "context2", "context3", "context4","context5"};
ArrayList<String> context = new ArrayList<String>();
contextList.addAll( Arrays.asList(context) );
// Create ArrayAdapter using the context list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, contextList);
// Add more contexts. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "context8" );
listAdapter.add( "context9" );
listAdapter.add( "context10" );
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
}
}
Related
So, what I'm trying to do is have a main menu and then press a button, it loads a list of countries.
now I've seemed to have set it up right, no errors I can see, but the app crashes before I load this activity (the lists) the main menu is fine, and I added another button with a blank activity which loads just fine.
logCat is giving me these errors
E/ArrayAdapter: You must supply a resource ID for a TextView
D/AndroidRuntime: Shutting down VM
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
and I'm not sure what it's asking for beyond the textView
heres my XML:
<ListView
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#f897"
android:dividerHeight="1dp"
android:listSelector="#0f0"/>
and my Java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class countries extends AppCompatActivity {
ListView simpleList;
String countryList[] = {"India", "China", "Australia", "Portugal", "USA","England", "NewZealand", "Germany", "France","South Africa"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countries);
simpleList = (ListView)findViewById(R.id.text1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_countries, countryList);
simpleList.setAdapter(arrayAdapter);
}
}
any help?
change:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_countries, countryList);
to:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,countryList);
your code not work because you use default ArrayAdapter with custom item layout
You array adapter expects the layout to be a TextView, but it look like R.layout.activity_countries is an activity layout?
You should create a custom adapter and custom view for each listview's row. Check it here: Custom Adapter for List View
I have the following codes as below. I am trying to run a simple list view all with not error and output is empty white screen.
MainActivity.java
public class MainActivity extends ActionBarActivity {
String[] stateArray = {"jh","kd"};
private ListView stateListView;
private ArrayAdapter<string> arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stateListView = (ListView) findViewById(R.id.state_list);
arrayAdapter = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1);
stateListView.setAdapter(arrayAdapter);
}
}
activity_main.xml
<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: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.example.lab4_1.MainActivity" >
<ListView
android:id="#+id/state_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
Al works fine no error and the emulator just show empty which screen nothing inside. What could be missing?
Try this way
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stateArray);
Passed your Array as third argument of ArrayAdapter
For more information go to AndroidListView/Vogella
You have not passed your actual data in your adapter that is why its showing listview empty. Simple pass your stateArray as last parameter of your adapter as below:
Change your adapter line as below:
String[] stateArray = {"jh","kd"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, stateArray);
Make your <string> as <String> you need to make your 'S' capital.
Note: The <String> part of it all means that the ArrayAdapter will be working with String[] data (the paths parameter). In other words, each element in the array will be a String.
It is empty because you are not submitting the dataset to the Adapter
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
Change it in
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stateArray);
Here you can find the documentation
Change
arrayAdapter = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1);
to
arrayAdapter = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,stateArray );
Use like this
adapter.setAdapter(new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,stringarray));
Try this
arrayAdapter = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,stateArray);
this is empty because you are not passing any data to adapter, the adapter acts as a bridge and is responsible of making the view see this See Adapter
See these examples Examples this will help you.
Best of luck!
First of all you definition of the generics on the ArrayList are wrong. It needs to say ArrayList<String>. Note the capital S.
You need to add Items to you list. The UI Designer is adding sample items automatically, that might have confused you into thinking there would already be items.
Try adding something like arrayAdapter.add("Test"); at the end of you onCreate().
If that works you can either add your stateArray using:
arrayAdapter.addAll(stateArray)
Full MainActivity.java:
package com.example.test;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] stateArray = {"jh","kd"};
private ListView stateListView;
private ArrayAdapter<String> arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stateListView = (ListView) findViewById(R.id.state_list);
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
stateListView.setAdapter(arrayAdapter);
arrayAdapter.add("Test");
arrayAdapter.addAll(stateArray);
}
}
I'm lost, so can I please have some help on how to link the list items to seperate activities? I have it to explain the different species available for what I am trying to do. It displays stuff such as dwarves, humans, all the fun stuff. Thanks in advance.
package com.apw.games.rpg.medieval;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Species extends Activity {
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.species);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// Create and populate a List of planet names.
String[] planets = new String[] { "Human", "Dwarf", "Earth-Born", "Elf",
"Sky Spirit", "Water Spritit", "Death Spirit", "War Spirit"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(planets) );
// Create ArrayAdapter using the planet list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
// Add more planets. If you passed a String[] instead of a List<String>
// into the ArrayAdapter constructor, you must not add more items.
// Otherwise an exception will occur.
listAdapter.add( "Troll- Coming Soon" );
listAdapter.add( "Giant- Coming Soon" );
listAdapter.add( "God- Coming Soon" );
listAdapter.add( "Monster- Coming Soon" );
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
}
}
Call setOnItemClickListener() on your ListView, then call startActivity() from the onItemClick() method of that listener.
More often, though, you will find developers using ListActivity, instead of Activity, and overriding onListItemClick() in their ListActivity. See: https://github.com/commonsguy/cw-omnibus/tree/master/Selection/List
I am creating my very first Android application, but i stuck unfortunately. The application would be very simple: On the starting page there is a ListView with items like:
1st group
2nd group
3rd group
...
By clicking on any of these items a new page would show up with a single textview element that would have some description. Like you click on '1st group' item, the listview gets hidden, and a new page appears with '1st group description' text.
So far I can show the listview with the items, but when I click on them, nothing happens (i guess I miss some basic stuff, but as a very newby, i cannot find it out easily).
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import android.view.*;
public class SimpleListViewActivity extends Activity {
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.mainListView );
// populate the List of groups
String[] GROUP = getResources().getStringArray(R.array.group);
ArrayList<String> GrList = new ArrayList<String>();
GrList.addAll( Arrays.asList(GROUP) );
// Create ArrayAdapter using the list.
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, GrList);
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter( listAdapter );
ll = (LinearLayout)findViewById(R.id.LinearLayout);
layoutParams = new LinearLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mainListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView t = (TextView) findViewById(R.id.textView1);
String[] DESC = getResources().getStringArray(R.array.desc);
t.setText(DESC[position]);
ll.addView(t);
//This is the point that is wrong for sure (and others maybe also). I cannot get the textview shown
}
});
}
}
Thanks for your help.
Have you tried displaying a toast message or setting a breakpoint within your onItemClick() method to verify that its not being reached? My guess is that it is and you are running into one of the issues described here:
Refreshing a LinearLayout after adding a view
I am assuming your R.layout.main is holding a listview and a linear layout with ids R.id.mainListView, and R.id.LinearLayout respectively.
Example: I left out some of the obvious attributes you would need like height width etc..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView android:id="#+id/mainListView" />
<LinearLayout android:id="#+id/LinearLayout" />
</RelativeLayout>
In your on item click, all you will do is add a textview as you have done, then set the mainListView.setVisibility(View.INVISIBLE) and ll.setVisibility(View.VISIBLE).
If your R.layout.main is not using a RelativeLayout as the root node, but is instead using a LinearLayout you should still be able to achieve the same effect by setting the Visibilities to View.GONE if you want it to hide, and View.VISIBLE if you want it to show.
To revert back to being able to see the list view I would override onBackPressed() in the activity, to invert the Visibilities on the two items. Also remember to remove all views from the linear layout so that the next time an item in the group is selected it will be the only item in the linear layout when it is added.
There are much easier ways to accomplish this, such as firing off a new activity for viewing the next item, but seems you are keeping everything within one activity. I would also think about using a ListActivity instead of base activity class.
Hope this helps.
First off stop using the word page. Call it an activity (gotta get you in the Android zone)
Once the click happens start a new activity like so:
mainListView.setOnItemClickListener(new OnItemClickListener() {
String textToPass = GrList.get(position)
Intent i = new Intent(getBaseContext(), SecondActivity.class);
i.putExtra("textToPass", textToPass);
startActivity(i);
}
You'll obviously need to have that second activity with its corresponding layout file defined. Also in the second activity look up how to get the bundle and extras from the first activity in order to get the textToPass String
please guide me with this program. Why do we need to use an array adapter to show the list? What is this "adapter", and can we display things directly in the ListView, without an adapter? Like, can we set setListAdapter(names) instead of setListAdapter(adapter);? Thanks.
Here is the code:
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class Episode7 extends ListActivity {
String[] names = {
"Elliot","Geoffrey","Samuel","Harvey","Ian","Nina","Jessica",
"John","Kathleen","Keith","Laura","Lloyd"
};
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ArrayAdapter that will contain all list items
ArrayAdapter<String> adapter;
/* Assign the name array to that adapter and
also choose a simple layout for the list items */
adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
names);
// Assign the adapter to this ListActivity
setListAdapter(adapter);
}
}
From the android API reference,
An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.
It basically a set of interfaces that determine how the data will be handled by the list. You can use different pre-made adapter classes in your lists or create your own if you want to present custom data.
Take a look at this page in the Dev Guide: http://developer.android.com/guide/topics/ui/binding.html
Lars Vogel has a nice tutorial also: http://www.vogella.de/articles/AndroidListView/article.html
The Adapter acts as both a container for the information you want to display, and allows you to change how it is displayed by over-riding the getView() method of the adapter. Normally, by default, the adapter will call the toString() method of the Object used to create the Adapter and set the text in the TextView that is referenced in the layout provided by android.R.layout.simple_list_item_1... but by over-riding the adapter's getView(), you can have a more complicated layout display for the list.
To answer the initial question... you must use an adapter with a ListView.
This is how I do it and it works for me:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListViewDemo extends Activity {
// --------- Create your string array, adapter and ListView
String[] items = {"Cars", "Money","Vacation","Electronics",
"Shoes","Jewelry", "Buku bucks","Cash","Ham","Swag","Straight Cash","Homies","Roll Dawgs","Nate Dogg","Wiz Khalifa","Mac Miller","Chitty Bang",
"Sam Adams","Technine","Kanye West","Rims","Escalade","Spreewells","Chrome Rims","24's",
"Lebron James","Dwayne Wade","Andre Iguodala","Allen Iverson","Jodi Meeks",
"Levoy Allen","Mo Williams","Eric Snow","Alien Iverson","Laptop","Phone","Tablet"};
ArrayAdapter<String> adapter;
ListView cashList;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cashList = new ListView(this);
// create the array adapter<String>(context, layout, array)
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
// add the adapter to the list
cashList.setAdapter(adapter);
// set the list as the content view
setContentView(cashList);
}
}