Array adapter and ListView Showing wrong data - java

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);
}
}

Related

Android studio , List View, 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);
}

open activity when select item from spinner and press the button

I don't know where is the problem.When I run the app, the spinner doesn't exist.
I am new in adroid studio and i'll glad to see where the mistake.
Spinner spin1;
Button but11;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_program_autobuz);
spin1 = (Spinner) findViewById(R.id.spin1);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(ProgramAutobuz.this,
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.LiniiAutobuz));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(myAdapter);
but11 = (Button) findViewById(R.id.but11);
but11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = spin1.getSelectedItem().toString();
if (text.equals("101")) {
Intent intent = new Intent(ProgramAutobuz.this, Orar101.class);
startActivity(intent);
}
if (text.equals("102")) {
Intent intent = new Intent(ProgramAutobuz.this,Statii102.class);
startActivity(intent);
}
}
});`

Item in the ArrayList gets replaced instead of appending

I am trying to bring all the items from the ArrayList getting saved in EditActivity to the MainActivity using SharedPreferences. But only the last entered data gets displayed in the ListView on the first MainActivity. How can I make all the text entered into the EditText get displayed as a different item in the ListView and not replace the earlier item.
Here is my Code:
MainActivity
public class MainActivity extends AppCompatActivity {
private ListView list;
private Button nextButton;
ArrayList<String> arrayList = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
nextButton = (Button) findViewById(R.id.nextButton);
loadData();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
list.setAdapter(adapter);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
nextActivity();
}
});
}
public void nextActivity(){
Intent intent = new Intent(this, EditActivity.class);
startActivity(intent);
}
public void loadData(){
SharedPreferences sp = getSharedPreferences("Data", Context.MODE_PRIVATE);
Gson gson = new Gson();
String json = sp.getString("list", null);
Type type = new TypeToken<ArrayList<String>>() {}.getType();
arrayList = gson.fromJson(json, type);
if(arrayList == null){
arrayList = new ArrayList<String>();
}
}}
EditActivity
public class EditActivity extends AppCompatActivity {
private EditText input;
private Button addButton;
public ArrayList<String> list = new ArrayList<String>();
public ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
input = (EditText) findViewById(R.id.input);
addButton = (Button) findViewById(R.id.addButton);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getInput = input.getText().toString();
adapter.addAll(getInput);
saveData();
input.setText(""); //clear the value in the edit text
}
});
}
public void saveData(){
SharedPreferences sp = getSharedPreferences("Data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
Gson gson = new Gson();
String json = gson.toJson(list);
editor.putString("list", json);
editor.apply();
}}
I think you forget to add item to list before save and thus the result is you are saving only the last item.
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getInput = input.getText().toString();
list.add(getInput); //add this line
adapter.addAll(getInput);
saveData();
input.setText(""); //clear the value in the edit text
}
});

cannot infer type arguments for ArrayAdapter<> when retrieving data from a database

My code is here please tell me what is the problem.
when i want to retrieve data from SQlite database the it give me the following error.
Please someone tell me what is the problem in my code
public class ViewData extends AppCompatActivity {
DatabaseHelper mDb;
TextView EList;
Button mBtn;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_data);
listView = (ListView) findViewById(R.id.listView);
mBtn = (Button) findViewById(R.id.buttonList);
mDb=new DatabaseHelper(this);
view1All();
}
public void view1All() {
mBtn.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<String> theList=new ArrayList<>();
Cursor res = mDb.getAllData();
if(res.getCount() == 0) {
// show message
// showMessage("Error","Nothing found");
// return;
}
while (res.moveToNext()) {
theList.add(res.getString(1));
ListAdapter listAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
listView.setAdapter(listAdapter);
}
}
}
);
}
}

Getting the inputs from arrayadapter and showing them one by one in the other activity

Lets say I have the ability to add player names in my adapter as inputs and then show them in the listview. Now i want to show these names that the user just added in my second activity, but not as list view. But single items. How can I don that?
Here i create the adapter and add player names:
public class ZaidejaiActivity extends ActionBarActivity implements View.OnClickListener{
public Button mBtnIstrinti;
public Button mBtnPrideti;
public Button mBtnPradeti;
public EditText mYrasytiVarda;
public ListView mZaidejai;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zaidejai);
mBtnPradeti = (Button)findViewById(R.id.žaistiBtn);
mBtnPradeti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pradetiZaidima = new Intent(v.getContext(), ZaidimasActivity.class);
startActivity(pradetiZaidima);
}
});
mBtnPrideti = (Button)findViewById(R.id.pridėtiBtn);
mBtnPrideti.setOnClickListener(this);
mYrasytiVarda = (EditText)findViewById(R.id.VardoYrasymoBtn);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
// set the mZaidejai variable to your list in the xml
mZaidejai=(ListView)findViewById(R.id.sarašas);
mZaidejai.setAdapter(adapter);
#Override
public void onClick(View v) {
String input = mYrasytiVarda.getText().toString();
if(input.length() > 0)
{
// add string to the adapter, not the listview
adapter.add(input);
// no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
}else{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Klaida:");
alertDialog.setMessage("Blogai yrašytas vardas");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.show();
}
}
Here i want to show the added inputs, as single values.
public class ZaidimasActivity extends ZaidejaiActivity {
ArrayList<String> listas = getIntent().getStringArrayListExtra("list");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zaidimas);
}
How to get the context from the adapter in my last activity and how to show them?
what you want to do is pass the list as a bundle to the new activity. Make a getter method for your adapter class to get the list.
mBtnPradeti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pradetiZaidima = new Intent(v.getContext(),
ZaidimasActivity.class);
pradetiZaidima.putExtra("playerList", adapter.getList());
startActivity(pradetiZaidima);
}
});
Then in your launched activity
public class ZaidimasActivity extends ZaidejaiActivity {
ArrayList<String> listas;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zaidimas);
listas.getIntent().getExtras().getStringArrayList("playerList");
}

Categories