Why my app containing listView is crashing? - android

My app contains only 2 files one is MainActivity.java and activity_main.xml file. I wanted to use to listView to display a list of string but for some my app is crashing continuously.
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> arrayList=new ArrayList<String>();
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Akshay");
arrayList.add("Pandey");
arrayList.add("Akshay");
arrayList.add("Akshay");
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,0,arrayList);
ListView listView=(ListView) findViewById(R.id.list_item);
listView.setAdapter(adapter);
}
}
Error

Your app is crashing because you are passing 0 as the second argument to your ArrayAdapter constructor. This is the "layout resource id" that your list will use for each item, so it needs to be something real.
The Android platform comes with some resources for this purpose built in, like android.R.layout.simple_list_item_1. You could try that. Change this line:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,0,arrayList);
to this instead:
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
More information can be found in the developer guide: https://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

You can't set the second argument of ArrayAdapter to 0. The constructor expects a valid resource. You need to set it to something like this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.my_resource_view, arrayList);

Related

Get the value from edittext,save it in listview

How do I get the value from EditText and save it in ListView of another Activity. Each time I give the value in EditText it should be saved permanently in the ListView one after the other. Here is my code.
MainActivity where I am retrieving edittext values into listview
public class MainActivity extends Activity {
private ListView lv;
ArrayAdapter<String> adapter;
ArrayList<String> list=new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar bar=getActionBar();
lv=(ListView)findViewById(R.id.list);
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
String item=getIntent().getStringExtra("location");
adapter.add(item);
every time you add a value in EditText along with adding it to list you also need to save it somewhere else(eg.in shared preferences) so that values don't get lost while this activity reinitilizes coz that will reinitilize the list adapter as well. If not compulsary why don't you add values to list in same activity?

ListAdapter error

Could someone tell me what is wrong with this piece of code?
It is supposed to be a ListView from an XML file that is then referred to in Java.
Alas, it crashes my application every time it enters the Menu class.
public class Menu extends ListActivity {
String Name_for_classes[] = {"- 1-9 Tabels -", "- 10-19 Tabels -", "- 20-29 Tabels -" };
String Tabel_classes[] = {"First", "Second", "Third"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.menu, Name_for_classes));
ListView list = getListView();
list.setTextFilterEnabled(true);
}
}
Okay, let's assume you have your ListView in an XML file called my_listview.xml.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_listview.xml);
ListView list = (ListView)findViewById(R.id.list);
ArrayAdapter<String> yourAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, Name_for_classes);
list.setAdapter(yourAdapter);
}
I'm not sure what is in R.layout.menu, but guessing by the naming its the activity layout. This should be used with setContentView(R.layout.menu) in onCreate. The layout that is passed into the ArrayAdapter is the TextView you are using to populate the listview.
You have forgotten to call the setContentView in your onCreate method, so your listview is not referenced yet.
When you use setContentView, it is equivalent as sayin 'for this activity I want to use the template 'myTemplate.xml'
After that, you have to 'linked' your java ListView attribute to the listview declared in your template.

Android listview: Resource Not Found Exception

I've got problem with Listview in Android.
Then I try to set adapter to a listview I got Resource Not Found Exception.
My code:
public class MyActivity extends Activity {
ArrayList<String> list;
public void onCreate(Bundle savedInstanceState) {
list = new ArrayList<String>();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list.add("first");
listView = (ListView)findViewById(R.id.CheckpointList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.id.CheckpointList, list);
listView.setAdapter(adapter);
}
}
And in my main.xml I've got:
<ListView android:id="#+id/CheckpointList" android:layout_height="wrap_content" android:layout_width="fill_parent"></ListView>
I've tried to clean and refesh project - no effects...
How to solve this problem?
Cheers.
The resource you hand over to the ArrayAdapter should not be the id of the ListView. It should be the textViewResourceId - which is basically which TextView-layout-id you want your items, in the list, to be rendered as.
One of the standards is e.g. android.R.layout.simple_list_item_1.
Here's an example of a simple ListView:
public class ListviewExample extends Activity
{
private ListView listView;
private String listView_data[] = {"Android","iPhone","BlackBerry","AndroidPeople"};
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.ListView1);
// By using setAdapter method in listview we add the string array to the ListView.
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , listView_data));
}
}
The ressource you hand over to the ArrayAdapter should not be the id of the listview. It should be the layout ressource of the textview in the listview. Look at the documentation: ArrayAdapter(Context context, int textViewResourceId, List objects)

Implementing ListView with icons on different items in android application

I am developing an android application in which i have to place 5 items with 5 icons in a list.Has anyone implemented it before?
If Yes,Can he help me how to implement it?
Thanks
tushar
you can try from this
Dynamic ListView in Android app
public class ListViewDemo extends ListActivity {
//LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems=new ArrayList<String>();
//DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> adapter;
//RECORDING HOW MUCH TIMES BUTTON WAS CLICKED
int clickCounter=0;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {
listItems.add("Clicked : "+clickCounter++);
adapter.notifyDataSetChanged();
}
}
android.R.layout.simple_list_item_1 is default list items layout supplied by android and >you can use this stock layout for non complex things.
listItems is an array list which holds the data shown in the ListView and all the >insertion and removal should be done on listItems the changes in list should reflect in >the view and thats handled by ArrayAdapter adapter which should be notified using
adapter.notifyDataSetChanged();
Adapter is instantiated with 3 paramters the context which could be your >activity/listactivity the layout of you individual list item and lastly the list which is >the actual data to be displayed in the list.

Android ListView understanding

I have a question about the ListView and how to use it. My Prolem is that my listView is only a part of the view and I am not sure how to do this.
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView myListView = (ListView) findViewById(R.id.ListView01);
String[] strings = new String[]{"Test1","Test2"};
ArrayAdapter<String> myArrayAdapter= new ArrayAdapter<String>(this, R.id.ListView01,strings);
myListView.setAdapter(myArrayAdapter);
I think the problem is the "this" in myArrayAdapter!?
The layout resource id you're supposed to pass to ArrayAdapter is a layout that's used to render each item in the list, not the layout for the list itself. Android provides some layout resources for the common cases. Try using:
ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);

Categories

Resources