Deleted item still showing on the Result page. - java

I'm able to edit or delete single item without any problem but when I click delete button after edit an item, the item is still showing on the ListView even though it has been deleted from the database.
ListViewActivity.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_requests);
requestsList = new ArrayList<HashMap<String, String>>();
new LoadAllRequests().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String request_id = ((TextView) view.findViewById(R.id.request_id)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
ViewRequestActivity.class);
// sending rid to next activity
in.putExtra(TAG_ID, request_id);
// starting new activity and expecting some response back
startActivityForResult(in, 55);
}
});
}

Instead of closing and reopening the activity, you should use invalidateViews
yourListView.invalidateViews();

You have to sign the adapter to the listview again

You can also try refreshing the activity. I believe that it is ListViewActivity.restart();

Related

Check if setOnItemClickListener is selected

I wrote a simple program that will display an ArrayList and the user can select an item and delete it with a button.
If the user does not select an item but continues to hit the delete button, they will remove the first item on the ArrayList. How do I prevent this from happening? The delete button should only run when an item is selected, so I need to find a way to check for that
int positionID;
ListView listview;
ArrayList<String> valuesArray = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
valuesArray.add("a");
valuesArray.add("b");
valuesArray.add("c");
valuesArray.add("d");
valuesArray.add("e");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, valuesArray);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
positionID = (int) id;
}
});
}
public void onClickDelete(View v) {
//need to check if an item is selected. if so, run code below
valuesArray.remove(positionID);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, valuesArray);
listview.setAdapter(adapter);
}
Based on the code you've provided, this is likely happening because the default initialized value for positionID is set to 0. To fix this, you could initialize int positionID = -1 and then in your onClickDelete method, do a check to make sure the positionID is not invalid. Like this:
public void onClickDelete(View v) {
if (positionID < 0) return;
// continue your code here
}
By the way, a few other things you should fix. You should actually interact with adapter directly, so instead of valuesArray.remove(positionID) you should do adapter.remove(positionID). This will automatically update the adapter and refresh the ListView for you so you can get rid of the last two lines of your onClickDelete method.
You can just make valuesArray a local variable in your onCreate method, unless you plan on manipulating it directly elsewhere in your code. If you choose to do that, you can call adapter.notifyDataSetChanged() to make the adapter refresh your ListView.

Creating different activities for each item in a ListActivity

How can I create a new activity when an item in my ListView is clicked? It would be best if I can create this in a single java file. Also, it would be great if someone explained how I can display text in the new activity, I may be wrong, but I assume I can somehow use TextView. Any thought would be helpful, thanks in advance!
Capture the item click event and create an intent that contains the data that you need to send the new activity. Then start the activity with your intent as normal.
YourActivity.java
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(YourActivity.this, YourNextActivity.class);
// Insert your logic to send data based on which item was clicked here
intent.putExtra("MY_EXTRA_DATA", "Some data!");
startActivity(intent);
}
});
In the activity you start, you can use getIntent() to retrieve the data you pass in.
YourNextActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
Intent intent = getIntent();
String data = intent.getStringExtra("MY_EXTRA_DATA");
TextView textView = (TextView) findViewById(R.id.yourId);
textView.setText(data);
}

how to use Android - notifyDataSetChanged() after delete an item?

I search a lot of similar question...all of them say I must use something like this code:
arrayAdapter.notifyDataSetChanged();
after this code: bookmarks.remove(pos);
but you can see...in BookMarkActivity class do not use arrayAdapter
public class BookMarkActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.message);
// Reading all contacts
final ArrayList<BookMark> bookmarks = (ArrayList<BookMark>) db.getAllBookMarks();
BuildingAdapter adapter = new BuildingAdapter(context);
// ADDED
final ListView lv = (ListView) findViewById(R.id.list_view);
lv.setAdapter(new BookmarkAdapter(this, bookmarks));
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
bookmarks.remove(pos);
return true;
}
});
}
}
I am new in android...but I try change code with this:
private ArrayAdapter<BookMark> arrayAdapter;
arrayAdapter = new BookmarkAdapter(this, bookmarks);
but no success! the listview do not be refresh after delete one item!
ListView displays some data stored in an ArrayList.
When element remove or add in ArrayList, you have to tell the list that the source of the data had changed to show the new data.
So, that is where notifyDatasetChanged() comes in. It tells the ListView that the data has been modified so please reflect the changed data.
final BookmarkAdapter adapter = new BookmarkAdapter(this, bookmarks);
lv.setAdapter(adapter);
and after remove element use below line.
adapter.notifyDataSetChanged();
hope it helps.
Just add two lines on the button/imageView click listeners.
delete.setOnClickListener {
list.removeAt(pos)
notifyItemRemoved(pos)
}

Set onClickListener for every single listview element

Here's how my app works. User records a sound and then gives it a certain name. In this example I'll be recording two sound files. Test1.mp3 and Test2.mp3. After I'm done recording for the first time, a dialog appears and I type in 'Test1', same goes for the second recording. Test1.mp3 and Test2.mp3 were now added to the listview. This is the code:
//filename is a variable for the name of the file, these lines execute everytime I record a new file
ArrayList<String> fileNames = new ArrayList<String>();
fileNames.add(filename.toString());
listView = (ListView) findViewById (R.id.mainListView);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, fileNames);
listView.setAdapter(listAdapter);
So, after I've recorded both files and they're added to listview, I want to set onClickListeners to both listview elements. But how do I do it? How do I make it so that everytime a new recorded file has been added to the listview, it also automatically generates the onclick method. This wouldn't be as complicated but every recorded file, ofcourse, has a different path.
The code now:
//LISTVIEW
fileNames.add(filename.toString()); //adding each filename to ArrayList<String>
listView = (ListView) findViewById (R.id.mainListView);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, fileNames);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast toast = Toast.makeText(getApplicationContext(), filename, Toast.LENGTH_SHORT);
toast.show();
}
});
This is the code that executes each time new file is recorded
You don't have to add a new listener every time you add an element to the list.
You can use a OnItemClickListener set once and for all, and you will be able to find the item that has been clicked by the index in the callback function
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
which is, parameter int position.
Inside onItemClick implementation, you can then retrieve the element which has been clicked by
arg0.getItemAtPosition(position)
Also, you don't have to add the onItemClickListener every time, you can just prepare it once in the onCreate method of your Activity and never change it.
What you will need to do instead, is making a new adapter (or adding a new item to the adapter) when your new file recording has terminated.
I prepared an almost full sample to show how to use the ListView:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> fileNames = new ArrayList<String>();
fileNames.add("Test1.mp3");
fileNames.add("Test2.mp3");
final ListView listView = (ListView) findViewById (R.id.mainListView);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileNames);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast toast = Toast.makeText(getApplicationContext(),
(String)arg0.getItemAtPosition(position), Toast.LENGTH_SHORT);
toast.show();
}
});
}
This is enough if the items in the list do not change.
When a recording of your next mp3 file terminates, and you want to add an item to the list, you may just do:
ListView listView = (ListView) findViewById (R.id.mainListView);
fileNames.add("Test3.mp3");
((ArrayAdapter<String>)listView.getAdapter()).notifyDataSetChanged();
Replace "Test3" with your new recorded mp3's filename, and it will show up in the list.
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
switch(position){
case 0:
blablbla
break;
case1 1:
blabla
break;
/....
}
}):

Listview with detailview in android

Help!!
I have a listadapter with a detail view, the problem that I am having is trying to pass values from an xml file to the second view. I can currently do this but only if I show those values in the first view. What I am trying to acheive is on the firstview have just a title and when you click on that title it takes you to a detail view with more information about it.
here is the code that I currently have.
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_USERADDRESS, KEY_DATEDUE}, new int[] {
R.id.name, R.id.cost });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
//#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
// String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView)view.findViewById(R.id.cost)).getText().toString();
// String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
//in.putExtra(KEY_USERADDRESS, name);
in.putExtra(KEY_DATEDUE, cost);
//in.putExtra(KEY_DESC, description);
startActivity(in);
}
});
}
As you can see I have a list adapter,I only want to show the contents of the KEY_USERADDRESS on the first view and when they click it then the rest will be shown. If I add the rest of the Textviews to the listadapter it makes the textviews to big.
Sorry if I am a little confusing, I'm still a rookie when it comes to android development.
Any help will be appreciated!!
Assuming your data is stored in the menuItems Map object, how about something like this in your onItemClick():
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Your code here..
Map<String, String> myValueMap = menuItems.get(position);
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_USERADDRESS, myValueMap.get(KEY_USERADDRESS));
in.putExtra(KEY_DATEDUE, myValueMap.get(KEY_DATEDUE));
in.putExtra(KEY_DESC, myValueMap.get(KEY_DESC));
startActivity(in);
}
the position parameter in the onItemClick can be used to get the selected Map from your menuItems which you used the create your SimpleAdapter to populate the ListView

Categories