Android Application gets crashed - android

The app crash because of the following code lines.
I have two string-array in a XML file named as Telephones in a folder named values and two TextView in text.xml.
I can not figure out what's going wrong here.
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] name= getResources().getStringArray(R.array.names);
String[] number= getResources().getStringArray(R.array.numbers);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.text, name);
listView.setAdapter(arrayAdapter);
ArrayAdapter<String> arrayAdapter1= new ArrayAdapter<String>(this, R.layout.text, number);
listView.setAdapter(arrayAdapter1);
}
}
Your help would be appreciated.
Thank You!

I forgot to add TextView id in MainActivity java but after adding the names_txt won't show up the only one show up is numbers_txt, how fix it please?
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] name= getResources().getStringArray(R.array.names);
String[] number= getResources().getStringArray(R.array.numbers);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.text, R.id.names_txt, name);
listView.setAdapter(arrayAdapter);
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(this, R.layout.text, R.id.numbers_txt, number);
listView.setAdapter(arrayAdapter1);
}
}

Related

Android listview unfortunately, app has stopped

I have an issue about listview filled with string item althouh I look up a search about it. I don't undertand where the problems comes from though I define layout.xml correctly. How can I solve it out?
public class A extends AppCompatActivity {
ArrayList<String> items= null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
items = new ArrayList<String>();
items.add("AAAA");
items.add("BBB");
items.add("CCC");
items.add("DDD");
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
ListView listView = (ListView) findViewById(R.id.listviewAttactivePlaces);
listView.setAdapter(itemsAdapter);
}
a.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView 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/listviewAttactivePlaces"
tools:context="com.example.android.turkeytourguide.AttactivePlacesActivity">
</ListView>
you have to declare your arrayList as follows
List<String> items = new ArrayList<>();
please add this..
items=new ArrayList<String>();
items.add("AAAA");
items.add("BBB");
items.add("CCC");
items.add("DDD");
Try changing the type in declaration
public class A extends AppCompatActivity {
List<String> items= null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
items = new ArrayList<String>();
items.add("AAAA");
items.add("BBB");
items.add("CCC");
items.add("DDD");
ArrayAdapter<String> itemsAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
ListView listView = (ListView) findViewById(R.id.listviewAttactivePlaces);
listView.setAdapter(itemsAdapter);
}

add item in to Listview Android?

I am trying to add an item to ListView:
correct?
public class Main extends Activity {
ArrayAdapter <String> listAdapter ;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView)findViewById(R.id.listView);
String[] S= new String[]{"YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA-Ali"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(S) );
listAdapter = new ArrayAdapter<String>(this,R.layout.main,planetList);
list.setAdapter(listAdapter);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Main layout
String[] s = {"YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA- Ali","YA ali","YA-Ali"};
list = (ListView)findViewById(R.id.listView); // Your activity should have listview with id listView
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s); //android.R.layout.simple_list_item_1 is built-in xml file
list.setAdapter(listAdapter);
}

Simple List View in Android crashes if ArrayList is not empty

I have a Problem with a ListView.
Here is my code:
private void setViewProtocol() {
setContentView(R.layout.protocol);
List<String> logs = new ArrayList<String>();
logs.add("TEST 1");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.protocol, logs);
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);
}
It crashes - but when I remove the line logs.add("TEST 1") it works.
Can anyone help me? Why does it work when I call it with an empty list?
Thanks
Use the following:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, logs);
If you give a custom layout you have to have an TextView with the ID android.R.id.text1 or you have to override the getView()
Try this way,hope this will help you to solve your problem.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.protocol);
List<String> logs = new ArrayList<String>();
logs.add("TEST 1");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.simple_list_item_1, logs);
ListView lv = (ListView) findViewById(android.R.id.listView1);
lv.setAdapter(adapter);
}

Android: create CheckBox in activity.java

Hi i want to create dynamical number of checkboxes (it depends on the length of array "genre") in an Android app then i did:
CheckBox [] checkbox;
String [] genre={"party","sport","music","café","education"};
#Override
protected void onCreate(Bundle savedInstanceState) {
checkbox=new CheckBox[genre.length];
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
for(int i=0;i<genre.length;i++){
CheckBox c=new CheckBox(this);
c.setText(genre[i]);
checkbox[i]=c;
}
ArrayAdapter<CheckBox>adapter= new ArrayAdapter<CheckBox>(this, android.R.layout.simple_list_item_1,checkbox);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setAdapter(adapter);
}
but in the listview it shows numbers of the checkboxes in memory(I guess), why?
like : Android.wiget.CheckBox#44f03160
Android.wiget.CheckBox#44f03d60
...
You'd rather use the android.R.simple_list_item_checked item layout and enable multiple-choice on the list:
String [] genre={"party","sport","music","café","education"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, genre);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
This way you don't have to create the checkboxes manually by yourself
Because of this line
c.setText(genre[i]);
String [] genre={"party","sport","music","café","education"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, genre);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
This looks like checkboxes.

How to add an Array of TextViews into a ListView?

I got this piece of code that creates new TextView, then adds it to ArrayList<View> and when it is finished adding TextViews to Array it sets adds that Array into ListView. But somehow my ListView is appearing empty. Any idea what am I doing wrong?
Here is the code:
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayList<View> textvs = new ArrayList<View>();
for (int i=0; i<10;i++) {
TextView tv = new TextView(MainActivity.this);
tv.setText(""+i);
textvs.add(tv);
}
lv.addTouchables(portit); // lv is my listview
You should use ArrayAdapter. You did this the wrong way. Here is a sample:
public class ArrayAdapterDemo extends ListActivity {
String[] items = { "this", "is", "a", "really", "silly", "list" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1,
items));
}

Categories

Resources