Android studio , List View, java - java

The app is keeps crashing , what can I do?
this is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
ListView TestListView = findViewById(R.id.ListView1);
final ArrayList<String> TestObjects = new ArrayList<String>(asList("PC" , "Phone"));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this , android.R.layout.simple_list_item_1,TestObjects);
TestListView.setAdapter(arrayAdapter);
}
}

this is my code,it can work.
String[] Balls= new String[] {"ball","pen","toy","other"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ex_list_view01);
lstPrefer=(ListView) findViewById(R.id.lstPrefer);
ArrayAdapter<String> adapterBalls=new ArrayAdapter<String>(
this,android.R.layout.simple_list_item_1,Balls);
lstPrefer.setAdapter(adapterBalls);
lstPrefer.setOnItemClickListener(lstPreferListener);
}

Related

How to set an ArrayAdapter to a String Array using the resource id (R.string.)

I tried to cast the returned value from the resource into a string but it doesn't work.
this is my code:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
String[] storiesTitles = {(String) getText(R.string.first_story_title), (String) getText(R.string.second_story_title), (String) getText(R.string.third_story_title)};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_expandable_list_item_1, storiesTitles);
ListView list = findViewById(R.id.lv_stories);
list.setAdapter(adapter);
}
}
You can refer to this link:
Android documentation: string resources
And this is the solution you are looking for:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
The 'planets' array will contains all the string you defined for it in the strings.xml
Call the string array inside onCreate function
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] storiesTitles = {(String) getText(R.string.first_story_title), (String) getText(R.string.second_story_title), (String) getText(R.string.third_story_title)};
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_expandable_list_item_1, storiesTitles);
ListView list = findViewById(R.id.lv_stories);
list.setAdapter(adapter);
}
}
You probably get this:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.myniprojects.testsave/com.myniprojects.testsave.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
You have to init array in onCreate like this:
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] storiesTitles = {(String) getText(R.string.first_story_title), (String) getText(R.string.second_story_title), (String) getText(
R.string.third_story_title)};
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,
android.R.layout.simple_expandable_list_item_1,
storiesTitles
);
ListView list = findViewById(R.id.lv_stories);
list.setAdapter(adapter);
}
}
Now it should work. But easier way is to make this like that:
in res/values/strings.xml add:
<string-array name="titles_array">
<item>First title</item>
<item>Second title</item>
<item>Third title</item>
</string-array>
And MainActivity:
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] storiesTitles = getResources().getStringArray(R.array.titles_array);
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_expandable_list_item_1,
storiesTitles
);
ListView list = findViewById(R.id.lv_stories);
list.setAdapter(adapter);
}
}

How to add in this case elements to the ArrayList

i am runing the Code below but not happens. How can i doit right?
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;
List<String> memorablePlaces;
ListView listView;
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.memorablePlaces);
memorablePlaces = new ArrayList<>();
memorablePlaces.add("Add a place");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1);
listView.setAdapter(arrayAdapter);
}
Pass your list to adapter:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, memorablePlaces);

Array adapter and ListView Showing wrong data

I am trying multiple activities that obtain data from ArrayLists. I have written the layout files and the java files but now one of the activities is showing the ArrayList from another activity.
There is no error so I am not quite sure what I am doing wrong.
The first array and adapter:
public class AlbumsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_albums);
ArrayList<String> albums = new ArrayList<>();
albums.add("Umqele");
albums.add("We are friends");
albums.add("Isiphithiphiti");
albums.add("Scorpion kings");
albums.add("Red");
albums.add("Different world");
albums.add("Irue");
albums.add("Dirty Computer");
albums.add("Anti");
albums.add("Love Girls");
ArrayAdapter<String> albumsAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, albums);
ListView albumView = findViewById(R.id.albums);
albumView.setAdapter(albumsAdapter);
}
The second:
public class PlaylistActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playlists);
ArrayList<String> lists = new ArrayList<>();
lists.add("House");
lists.add("MorningJams");
lists.add("SundayTunes");
ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, lists);
ListView listView = findViewById(R.id.playlist);
listView.setAdapter(listAdapter);
}
Here is the output of your code. It runs as expected. Meaning different data in each Activity.
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String a[] = new String[4];
playlist = findViewById(R.id.open_playlist_bt);
album = findViewById(R.id.open_albumlist_bt);
playlist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, PlaylistActivity.class));
}
});
album.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, AlbumsActivity.class));
}
});
}
AlbumsActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String a[] = new String[4];
ArrayList<String> albums = new ArrayList<>();
albums.add("Umqele");
albums.add("We are friends");
albums.add("Isiphithiphiti");
albums.add("Scorpion kings");
albums.add("Red");
albums.add("Different world");
albums.add("Irue");
albums.add("Dirty Computer");
albums.add("Anti");
albums.add("Love Girls");
ArrayAdapter<String> albumsAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, albums);
ListView albumView = findViewById(R.id.album);
albumView.setAdapter(albumsAdapter);
}
public void openPlasylistActivity(View view) {
startActivity(new Intent(this, PlaylistActivity.class));
}
Playlist Activity
public class PlaylistActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ArrayList<String> lists = new ArrayList<>();
lists.add("House");
lists.add("MorningJams");
lists.add("SundayTunes");
ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, lists);
ListView listView = findViewById(R.id.play_list);
listView.setAdapter(listAdapter);
}
}

Show the text from EditText on ListView

I have a button, an editText, and a listView on my project. The function I want to realize is that as I enter some words in editText and click the button, the words I typed need to be in the listView.
Here is my code. After debugging, it shows error: incompatible types: String cannot be converted to String[]
public class MainActivity extends AppCompatActivity {
private String[] str = new String[10];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view); //build listview
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, str);
listView.setAdapter(adapter);
}
public void sendMessage(View view){
EditText editText = (EditText) findViewById(R.id.editText);
str = editText.getText().toString();
}
}
Below is working code for what you want.
step 1 -> Add data in edit text
step 2 -> click on submit button
step 3 -> Added data will display in listview
public class MainActivity extends AppCompatActivity {
private List<String> str = new ArrayList<>();
private ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.list_view); //build listview
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, str);
listView.setAdapter(adapter);
}
public void sendMessage(View view) {
EditText editText = findViewById(R.id.editText);
str.add(editText.getText().toString());
adapter.notifyDataSetChanged();
editText.setText("");
}
}
Try it this way
public class MainActivity extends AppCompatActivity {
private List<String> str = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view); //build listview
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, str);
listView.setAdapter(adapter);
}
public void sendMessage(View view){
EditText editText = (EditText) findViewById(R.id.editText);
String str1 = editText.getText().toString();
str.add(str1);
}
}
error error: incompatible types: String cannot be converted to String[]
is because of str = editText.getText().toString();
here you are trying to assign string to array of string incorrectly
what you have to do is use ArrayList insted of String[] as it is dynamic and efficient than String[]
and
insted of str = editText.getText().toString(); do
arrayList.add(ditText.getText().toString());
In your case you are using String[] array with size 10.
now if you want to add string from EditText in ListView then you have to add like below.
public class MainActivity extends AppCompatActivity {
private String[] str = new String[10];
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view); //build listview
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, str);
listView.setAdapter(adapter);
}
public void sendMessage(View view){
EditText editText = (EditText) findViewById(R.id.editText);
if(count < str.length){
str[count] = editText.getText().toString();
count++;
}
}
}
Why you geeting error?
You get the error because you set the String array as a string. But if you want to add string in string array then you have to specify a position for array.

How to Implement KMP string seaching algorithm on my listview ...?

I have a listview and I want to add a search in listview using the algorithm KMP
How to Implement on my listView.
#Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView =(ListView) findViewById(R.id.list_istilah);
autoTv = (AutoCompleteTextView) findViewById(R.id.AutoTV);
db = DatabaseHelper.getInstance(this);
setData();
autoTv.addTextChangedListener(this);
listView.setOnItemClickListener(this);
}
private void setData()
{
listKamus = db.getAllKamus();
adapter = new ArrayAdapter<Kamus>(this,
android.R.layout.simple_expandable_list_item_1, listKamus);
listView.setAdapter(adapter);
}

Categories