I want to delete an element from the list when a button x is clicked on the layout of a list element.
Here is my java code, onNext() is working for adding an element to the list . but the onDelete isnt working.The textView4 element has numbers starting from 1 to n when new elements are added
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list2);
arrayList = new ArrayList<String>();
arrayList.add("1");
adapter2 = new ArrayAdapter<String>(this, R.layout.list2_layout, R.id.textView4, arrayList);
ListView myList2 = (ListView)
findViewById(R.id.listView2);
myList2.setAdapter(adapter2);
}
public void onNextItem(View v)
{
counter++;
String str=Integer.toString(counter);
arrayList.add(str);
adapter2.notifyDataSetChanged();
}
public void onDeleteItem(View v2)
{
arrayList.remove(R.id.textView4);
adapter2.notifyDataSetChanged();
}
you should not be passing the R.id.textView4 to remove the 4th textView
arrayList.remove(R.id.textView4);
You will need to do something like arrayList.remove(4); to delete the forth element from the list
on onNextItem(View v):
you are adding an integer(counter) converted to string to the list(arrayList.add(str);)
but on onDeleteItem(View v2):
you are trying to remove a view from the list (arrayList.remove(R.id.textView4);)
Your trying to remove Textview
arrayList.remove(R.id.textView4);
do it like this
arrayList.remove(clickeditempostion);
adapter2.notifyDataSetChanged();
You can do something like this.
arrayList.remove(CURRENT_INDEX_OF_ARRAYLIST);
adapter2.notifyDataSetChanged();
Related
I have created Activity with a listView which uses setOnItemClickListener to determine which index has been pressed. Then it should pass the data into the next activity based on the selected index.
Passing of the values works, but it only works for the first listView index pressed after the app is reloaded.
To make it more clear. When the app is loaded I can press on any index of the ListView which will pass the data to the next Activity and display it correctly, but then when I go back to the ListView and select different index it still displays the same data from the previous index without updating it. Therefore, I have to reload the app to get new index results displayed.
Here is my ViewListActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_artist);
artistList = (ListView) findViewById(R.id.artistList);
databasehandler = new SqliteHandler(ViewArtistActivity.this);
allArtists = databasehandler.getAllArtists();
artistName = new ArrayList<>();
singleArtistDetails = new ArrayList<>();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (allArtists.size() > 0) {
for (int i=0; i < allArtists.size(); i++) {
ArtistModel artistModel = allArtists.get(i);
artistName.add(artistModel.getArtistName());
}
}
adapter = new ArrayAdapter(ViewArtistActivity.this, android.R.layout.simple_list_item_1, artistName);
artistList.setAdapter(adapter);
artistList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ArtistModel artistModel = allArtists.get(position);
singleArtistDetails.add(artistModel.getArtistName());
singleArtistDetails.add(artistModel.getArtistDOB());
singleArtistDetails.add(artistModel.getArtistBiography());
Intent intent = new Intent(ViewArtistActivity.this, SavedArtistDetailsActivity.class);
intent.putExtra("NAME", singleArtistDetails.get(0));
intent.putExtra("DOB", singleArtistDetails.get(1));
intent.putExtra("BIOGRAPHY", singleArtistDetails.get(2));
startActivity(intent);
}
});
}
Here is my code for displaying selected index results Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saved_artist_details);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
artistSavedNameLbl2 = (TextView) findViewById(R.id.artistSavedNameLbl2);
artistSavedDOBLbl2 = (TextView) findViewById(R.id.artistSavedDOBLbl2);
artistSavedBiographyLbl2 = (TextView) findViewById(R.id.artistSavedBiographyLbl2);
btnDeleteDetails = (Button) findViewById(R.id.btnDeleteDetails);
updateUI();
}
private void updateUI() {
artistSavedNameLbl2.setText(getIntent().getStringExtra("NAME"));
artistSavedDOBLbl2.setText(getIntent().getStringExtra("DOB"));
artistSavedBiographyLbl2.setText(getIntent().getStringExtra("BIOGRAPHY"));
}
Do one thing , initialize [not saying declare] your array list singleArtistDetails inside the onClick method, before adding any values to it.
like
singleArtistDetails = new ArrayList();
Hope this will help.
i don't know why but when i add items to listview i get only the last item. E.G. if i write apple and than pear i get only pear and not apple and pear.
Why?
CODE:
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("IDDD:"+iddd);
System.out.println("IDDD:"+video2.getPic());
//holder.lw.setA
String commento= (holder.tw.getText().toString());
// holder.lw.setAdapter(adapter);
ArrayList<String> arrayList= new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, arrayList);
arrayList.add(commento);
// next thing you have to do is check if your adapter has changed
holder.lw.setAdapter(adapter);
adapter.notifyDataSetChanged();
System.out.println("LISTVIEW:"+arrayList);
}
});
in every click you decalre new list ArrayList<String> arrayList= new ArrayList<String>(); and it's false
you need to declate it out of the listener and in your listener just add the new items and use adapter.notifyDataSetChanged();
You are creating new list and adapter during every click so move the declaration and initialization of both, outside that function
Declare them outside getView(if Base or ArrayAdaoter) or createViewHolder (RecyclerAdapter)
initialize them inside constructor
add data to list and notify adapter
Better use view holder pattern in here where you use Existing instance of listview rather than recreating the new one all the time.
You don't need to do anything special, inside of onClick you need to get a reference to your ArrayAdapter and call the method add(T...) on it.
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("IDDD:"+iddd);
System.out.println("IDDD:"+video2.getPic());
String commento= (holder.tw.getText().toString());
ArrayAdapter adapter = (ArrayAdapter) holder.lw.getAdapter();
adapter.add(commento);
}
});
No need to even call notifyDataSetChanged because add already does that internally.
if you use this you can solve your problem
yourAdapter.(getCount()-1-position);
and this if you want to see the last items in front
public yourPoJo getItem(int position) {
return super.getItem(getCount()-1-position);
}
I have an arraylist for my spinner, 1 add button to add element into the list and 1 delete button to delete the element inside the list. The elements that I added into the list will show in the spinner. Initially the arraylist is empty with nothing inside. When it is empty and I press the delete button, means that I am trying to delete elements in a arraylist with no element inside and this makes my app crashes.
So, I wanted to add a toast to replace the delete function when the list is empty. When the list is not empty, then the delete function will come back.
Any solution for this?
spinner = (Spinner) findViewById(R.id.spinner1);
adp = new ArrayAdapter<String>(CarSelection.this,android.R.layout.simple_spinner_item, list);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
spinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, final int position, long id) {
Button delete = (Button) findViewById(R.id.delete);
View.OnClickListener del = new View.OnClickListener() {
#Override
public void onClick(View view) {
list.remove(position);
}
Firstly,IMHO for better UX, you should not display the spinner if the list is empty.You can show toast message to the user saying that you cant perform this operation.
Anyways here is the code snippet you can use to do the check.You can put this check in whichever place you want
if(!list.isEmpty())
//list is empty
else
list is not empty
Lets say, your ArrayList is called mList, your delete function should look something like -
public void deleteElement(int pos) {
if(mList.isEmpty()) {
//Toast
Toast.makeText(yourContext, "Ooi, list is empty", Toast.LENGTH_SHORT).show();
} else {
mList.remove(pos);
}
}
UPDATE
#Override
public void onClick(View view) {
if(list.isEmpty()) {
//Your Toast
Toast.makeText(yourContext, "Ooi, list is empty", Toast.LENGTH_SHORT).show();
} else {
list.remove(position);
}
}
You should test for the "empty" case. Supposing an array named "elements":
if elements.isEmpty() {
deleteButton.disable();
}
The best is to disable the delete button when there is no elements in the array.
I have a listview. In it, each row had a text saying 0:00. But now, I added a button on my actionbar but then I got stuck. I don't know how to make the button create a new row, displaying 0:00
This is my code for the data in a row.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RowData rowdata_data[] = new RowData[]
{
new RowData("0:00")
};
RowdataAdapter adapter = new RowdataAdapter(this,
R.layout.listview_item_row, rowdata_data);
listView1.setAdapter(adapter);
listView1 = (ListView)findViewById(R.id.listView1);
}
this is my RowData class:
public class RowData {
String title;
public RowData(String title) {
this.title = title;
}
}
So how should I implement a button click to add another row?
Under addtionbutton: should be the method.
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle presses on the action bar items
switch (item.getItemId())
{
case R.id.additionbutton:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I don't know how to make the button create a new row
With your current solution it's not possible (in efficient way do it) because you're using static array of objects - it means array has fixed size (it means that you're assuming that size won't be changed at runtime).
But your goal is "slightly" different. You want after each click on Button increase number of objects by one. It means you don't know exactly how many rows you can have (or can be changed at runtime).
Due to things mentioned above you should (have to) use dynamic array with changeable size. For this reason you need to use as source of data List that is representation of dynamic array.
Basic algorithm:
Create new List with zero (or you can have by default one row at
start).
Then create public method in your adapter that will add new item to
collection and send request that Adapter should be refreshed (after you added new row into collection).
Assign OnClickListener to your Button and in onClick() method you'll use created method for adding new row into ListView.
Solution:
How to initialise ListAdapter:
// Activity-level variable scope
private List<RowData> items = new ArrayList<RowData>();
private RowdataAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView1 = (ListView)findViewById(R.id.listView1);
// adding first item to List, it's optional step
items.add(new RowData("0:00"));
adapter = new RowdataAdapter(this, R.layout.listview_item_row, items);
listView1.setAdapter(adapter);
}
Method for adding new row into ListAdapter:
public void addRow(RowData newRow) {
// items represents List<RowData> in your Adapter class
this.items.add(newRow);
// sends request to update ListAdapter
notifyDataSetChanged();
}
How to update Adapter after click on Button:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// add new row to Adapter
adapter.addRow(new RowData("0:00"));
}
});
Hope i helped you to solve your problem.
you need to create a dataset that you can change so you need to make your array a class wide variable so you can add to it when you need to
when you add the new row you need to notify the adapter that something changed so you do this
adapter.notifyDatasetChanged();
and that should update your list
this code is working fine but its creating only one spinner dynamically i want to get value from database and want to run the loop till database values and generate dynamic Spinner inside the loop overhere i have mentioned the code of FOR LOOP but its not working and as well as i want to load different item in different spinner please give me idea how to do this?
public class DisciplineActivity extends Activity
{ ArrayList<String> selectDisciplineArrayList,disciplineInfoArrayList;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.discipline_view);
for(int i=0;i<20;i++)
{
disciplineInfoArrayList.add("select location item:"+i);
}
// for(int i=0;i<5;i++)
//{
Spinner disciplineInfoSpinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,disciplineInfoArrayList);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
disciplineInfoSpinner = (Spinner) findViewById(R.id.disciplineinfo_spinner);
disciplineInfoSpinner.setAdapter(spinnerArrayAdapter);
}//
}
You are creating a new Spinner and doing nothing with it.
You need to create an empty LinearLayout on your "discipline_view" layout, and then add your created Spinners on that LinearLayout, like this:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);
for(int i=0;i<5;i++) {
Spinner disciplineInfoSpinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,disciplineInfoArrayList);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
disciplineInfoSpinner.setAdapter(spinnerArrayAdapter);
linearLayout.addView(disciplineInfoSpinner);
}