I made an Activity for searching people that also shows history of recent research.
If I long click on an item of the history it asks me if I want to delete it. If I press "Yes" it deletes the list item.
So, I write something and click to "Search" button. This brings me in another Activity with results. Here I click on result so it stores the person info and brings me in the person page.
When I come back I don't see the new person in the history.
So I overwritten onResume() but it still not work and now I cannot delete items from the history list.
Here the code:
package com.lpsmt.proffinder;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.lpsmt.R;
public class HomeActivity extends Activity
{
protected Db db = null;
protected List<ProfBean> historyProfs = null;
protected ProfListItemAdapter listAdapter = null;
protected ListView listView = null;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.db = new Db(this);
this.setContentView(R.layout.prof_finder_home);
this.historyProfs = this.db.getHistory(-1); // -1 means with no limits
this.listAdapter = new ProfListItemAdapter(HomeActivity.this, R.id.prof_finder_history_list_view, this.historyProfs);
this.listView = (ListView) this.findViewById(R.id.prof_finder_history_list_view);
listView.setAdapter(this.listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(HomeActivity.this, ProfPageActivity.class);
Bundle bundle = new Bundle();
bundle.putString("profId", HomeActivity.this.historyProfs.get(position).getProfId());
intent.putExtras(bundle);
HomeActivity.this.startActivity(intent);
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id)
{
Resources resources = HomeActivity.this.getResources();
String title = resources.getString(R.string.prof_finder_history_delete_title);
String message = resources.getString(R.string.prof_finder_history_delete_message);
AlertDialog.Builder adb=new AlertDialog.Builder(HomeActivity.this);
adb.setTitle(title);
adb.setMessage(message);
final int positionToRemove = position;
String positive = resources.getString(R.string.prof_finder_history_delete_positive);
String negative = resources.getString(R.string.prof_finder_history_delete_negative);
adb.setNegativeButton(negative, null);
adb.setPositiveButton(positive, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ProfBean prof = HomeActivity.this.historyProfs.get(positionToRemove);
HomeActivity.this.db.deleteProf(prof.getProfId());
HomeActivity.this.historyProfs.remove(positionToRemove);
HomeActivity.this.runOnUiThread(new Runnable() {
public void run() {
HomeActivity.this.listAdapter.notifyDataSetChanged();
}
});
}});
adb.show();
return true;
}
});
}
public void searchProf(View view) throws Exception
{
EditText queryEditText = (EditText) this.findViewById(R.id.prof_finder_search_query);
String query = queryEditText.getText().toString().trim();
queryEditText.setText(query);
if (query.length() < 3) {
String message = this.getResources().getString(R.string.prof_finder_query_too_short);
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(HomeActivity.this, SearchResultActivity.class);
Bundle bundle = new Bundle();
bundle.putString("query", query);
intent.putExtras(bundle);
this.startActivity(intent);
}
public void onResume()
{
super.onResume();
this.historyProfs = this.db.getHistory(-1);
this.listAdapter.notifyDataSetChanged();
}
}
You haven't set any new data to list view. Thats why your new contact isn't added to the list after notifyDataSetChanged(). You need to add some method into adapter like
setData(List<ProfBean> data)
{
this.currentAdaptersList= data;
}
and then call notifyDataSetChanged(). So the final onResume will be :
public void onResume()
{
super.onResume();
this.historyProfs = this.db.getHistory(-1);
this.listAdapter.setData(this.historyProfs);
this.listAdapter.notifyDataSetChanged();
}
Enjoy.
And using onResume() for this task is bad idea. Is better to use onActivityResult.
notifyDataSetChanged() didn't work for me either. I was able to solve this a little bit differently:
I use OnStart() (in a derived class from Fragment)
I use setNotifyOnChange() of the ArrayAdapter:
ListView listView = (ListView) findViewById(R.id.logListView);
listView.setAdapter(logAdapter);
logAdapter.setNotifyOnChange(true);
I create the adapter once:
logAdapter = new ArrayAdapter(activity, android.R.layout.simple_list_item_1, activity.logMessages);
in onViewCreated().
Related
I have an android app, which shows a list of items. By pressing an item it should perform an action like delete item. The problem is that, when I press the item, nothing happens, it is like the screen does not recognize my finger press. But, down on page, I have a button, that works fine, when I press it.
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private ListView listView;
public static FirebaseUtil firebaseUtil;
public static CostumeAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
firebaseUtil = new FirebaseUtil();
setContentView(R.layout.activity_main);
final Context context = this;
if (firebaseUtil.getmFirebaseUser() == null) {
loadLogInView();
} else {
firebaseUtil.setmUserId(firebaseUtil.getmFirebaseUser().getUid());
listView = (ListView) findViewById(R.id.listView);
adapter = new CostumeAdapter(this);
listView.setAdapter(adapter);
fillData();
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
final Costume car = adapter.getCostumes().get(position);
final String uuid = car.getUuid();
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Delete")
.setMessage("This car will be deleted.")
.setPositiveButton("Delete", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
firebaseUtil.remove(uuid, car);
}
})
.setNegativeButton("Cancel", null)
.show();
return true;
}
});
The xml files are ok, I guaranteed.
The OnItemLongClickListener that you have implemented would work only if the else part is executed. Is firebaseUtil.getmFirebaseUser() == null false?
I used this, and it worked, You can try out with some Log.d() to see if the clickListener is executed.
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onViewCreated: on View Created");
getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> adapterView,View view,final int position, long id) {
new AlertDialog.Builder(getContext())
.setTitle(R.string.attention)
.setMessage(R.string.confirm_to_delete)
.setNegativeButton(R.string.cancel,null)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Cursor c= adapter.getCursor();
c.moveToPosition(position);
int itemID = c.getInt(c.getColumnIndex("_id"));
dbWrite.delete("diary","_id=?",new String[]{""+itemID});
refreshList();
}
}).show();
return true;
}
});
}
My problem is that my code does not react accordingly whenever an user selects an item from an AutoCompleteTextView.
flag is a variable which is set to a value whenever one item from each AutoCompleteTextView has been selected. If it's set to 1, then it means it's right and it should proceed to main activity. Otherwise, a toast is displayed on click of button whose onClick calls the method callMainActivity.
There are no errors. Gradle build is successful, but clicking on that button (mentioned above) does nothing at all.
Code:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.Arrays;
import java.util.List;
public class Location extends AppCompatActivity {
private static int flag=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
int city = android.R.layout.simple_dropdown_item_1line;
int area = android.R.layout.simple_dropdown_item_1line;
int store = android.R.layout.simple_dropdown_item_1line;
String []city_array = getResources().getStringArray(R.array.City);
String []area_array= getResources().getStringArray(R.array.Area);
String []store_array= getResources().getStringArray(R.array.Store);
List<String> city_list= Arrays.asList(city_array);
List<String> area_list= Arrays.asList(area_array);
List<String> store_list= Arrays.asList(store_array);
ArrayAdapter<String> adapter_city = new ArrayAdapter(this,city, city_list);
ArrayAdapter<String> adapter_area = new ArrayAdapter(this, area, area_list);
ArrayAdapter<String> adapter_store = new ArrayAdapter(this, store, store_list);
final AutoCompleteTextView autocompleteView_city =
(AutoCompleteTextView) findViewById(R.id.City);
final AutoCompleteTextView autocompleteView_area =
(AutoCompleteTextView) findViewById(R.id.Area);
final AutoCompleteTextView autocompleteView_store =
(AutoCompleteTextView) findViewById(R.id.Store);
autocompleteView_area.setAdapter(adapter_area);
autocompleteView_city.setAdapter(adapter_city);
autocompleteView_store.setAdapter(adapter_store);
autocompleteView_area.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View arg0) {
autocompleteView_area.showDropDown();
if(autocompleteView_area.getListSelection()!= ListView.INVALID_POSITION)
flag=1;
else
flag=0;
}
});
autocompleteView_city.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View arg0) {
autocompleteView_city.showDropDown();
if(autocompleteView_area.getListSelection()!= ListView.INVALID_POSITION)
flag=1;
else
flag=0;
}
});
autocompleteView_store.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View arg0) {
autocompleteView_store.showDropDown();
if(autocompleteView_area.getListSelection()!= ListView.INVALID_POSITION)
flag=1;
else
flag=0;
}
});
//This is the newly updated part
autocompleteView_area.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
//... your stuff
if(autocompleteView_area.getListSelection()>0) {
flag = 1;
System.out.println(flag + "flag at area");
}else
flag=0;
}
});
}
public void callMainActivity(View view){
if(flag==1) {
Intent in = new Intent(getBaseContext(), MainActivity.class);
startActivity(in);
}
else
Toast.makeText(getBaseContext(),"Please select all fields properly",Toast.LENGTH_LONG);
}
}
The reason you are not seeing the Toast or changing activities, is because you are never calling callMainActivity(View view) in your code. Add this line to the end of all your OnClickListeners: callMainActivity(arg0) -- if this does not work, put some log statements in your OnClickListeners to check if they are triggering or not.
Also, if you want to trigger the call when an item from your AutoCompleteTextView result list is selected, you should use an AdapterView.OnItemClickedListener instead. This will notify you when an item is selected from the AutoCompleteTextView list, or when nothing is selected and then you can react accordingly.
I have a ListView which has more rows in it. Inside of rows I have two LinearLayouts (deleteItem and editItem) which has setOnClickListener on them. When I'm trying to click on deleteItem or editItem it works only at the second touch, i don't know why.. I've read some answers on stackoverflow but I couldn't find one answer to fix my problem..
This is the code:
import org.json.JSONArray;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.currencymeeting.adapters.GetAllMyCurrencyListViewAdapter;
import com.currencymeeting.beans.User;
import com.currencymeeting.connectors.DeleteCurrencyConnector;
import com.currencymeeting.connectors.MyCurrencyConnector;
import com.currencymeeting.controllers.MessageDialogController;
import com.currencymeeting.controllers.VariableController;
public class MyCurrencyActivity extends FragmentActivity {
private ListView getAllMyCurrencyListView;
private ProgressDialog dialog;
private View view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_currency);
this.dialog = ProgressDialog.show(MyCurrencyActivity.this, MessageDialogController.PROGRESS_DIALOG_TITLE, MessageDialogController.PROGRESS_DIALOG_MESSAGE);
this.getAllMyCurrencyListView = (ListView) findViewById(R.id.getAllMyCurrencyListView);
new GetMyCurrencyResults().execute(new MyCurrencyConnector());
getAllMyCurrencyListView.setOnItemClickListener(
new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
MyCurrencyActivity.this.view = view;
final TextView itemID = (TextView) view.findViewById(R.id.itemID);
final LinearLayout deleteItem = (LinearLayout) view.findViewById(R.id.deleteItem);
final LinearLayout editItem = (LinearLayout) view.findViewById(R.id.editItem);
deleteItem.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
new AlertDialog.Builder(MyCurrencyActivity.this)
.setMessage("Are you sure do you want to delete it?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String vid = itemID.getText().toString();
String userId = ""+VariableController.getInstance().getUser().getId();
new DeleteCurrencyTask().execute(userId, vid);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}
);
editItem.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),EditCurrencyActivity.class);
startActivity(intent);
}
}
);
}
}
);
final LinearLayout menu = (LinearLayout) findViewById(R.id.menu);
menu.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MenuLoggedActivity.class);
startActivity(intent);
}
}
);
}
public void setListAdapter(JSONArray jsonArray){
this.getAllMyCurrencyListView.setAdapter(new GetAllMyCurrencyListViewAdapter(jsonArray,this));
this.dialog.dismiss();
};
private class GetMyCurrencyResults extends AsyncTask<MyCurrencyConnector,Void,JSONArray>{
#Override
protected JSONArray doInBackground(MyCurrencyConnector... params) {
User user = VariableController.getInstance().getUser();
return params[0].getAllResults(user);
}
#Override
protected void onPostExecute(JSONArray jsonArray) {
setListAdapter(jsonArray);
}
}
private class DeleteCurrencyTask extends AsyncTask<String,Void,String[]>{
#Override
protected String[] doInBackground(String... params) {
return params;
}
#Override
protected void onPostExecute(String[] result) {
boolean status = new DeleteCurrencyConnector().deleteTransaction(result[0],result[1]);
LinearLayout itemViewId = (LinearLayout) MyCurrencyActivity.this.view.findViewById(R.id.itemViewID);
if(status){
new GetMyCurrencyResults().execute(new MyCurrencyConnector());
} else {
Toast.makeText(MyCurrencyActivity.this, "Error", Toast.LENGTH_LONG).show();
}
}
}
}
Your problem might be caused by the way your program executes. You first thread your adapter initialization, and then your main thread starts attaching onclick listeners before your adapter initialization finishes. This means your listview will have a click listener but not the items in the adapter, and when you click the listview once
new GetMyCurrencyResults().execute(new MyCurrencyConnector());
this fires off in your DeleteCurrencyTask and sets the adapter again. To fix this try putting setOnClick initializaion in the post execute of your
private class GetMyCurrencyResults extends AsyncTask<MyCurrencyConnector,Void,JSONArray>{
I believe I realized why the event of deleteItem and editItem is called only on second click, because deleteItem and editItem clickListeners are atached only when getAllMyCurrencyListView.setOnItemClickListener (when I click on row) is called not when the code is loaded. So I guess I have to find another way to make these events
I want to update listview in fragment from main activity after selecting item from dialog.
I am using FragmentPagerAdapter in this Activity
My Dialog method
private void showDialog(String routeName) {
MyTTCDB db = new MyTTCDB(this);
List<String> directions = db.getDirectionByRoute(routeName);
AlertDialog.Builder builderSingle = new AlertDialog.Builder(this);
builderSingle.setTitle("Direction");
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.select_dialog_singlechoice);
for (String string : directions) {
arrayAdapter.add(string);
}
builderSingle.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builderSingle.setAdapter(arrayAdapter,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String side = arrayAdapter.getItem(which);
FragmentStops fragment = (FragmentStops) mSectionsPagerAdapter
.getItem(1);
fragment.refresh(side);
}
});
builderSingle.show();
}
My Fragment
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.anilvasani.myttc.StopsActivity.Interface_FragmentStops;
import com.anilvasani.myttc.adapter.AdapterStops;
import com.anilvasani.myttc.data.MyTTCDB;
import com.anilvasani.myttc.models.Stop;
public class FragmentStops extends Fragment {
View view;
AdapterStops myAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragement_stops, container, false);
return view;
}
public void refresh(String side) {
// TODO Auto-generated method stub
String routeName = getActivity().getIntent()
.getStringExtra("routeName").toString();
ListView listStops = (ListView) view.findViewById(R.id.dialog_listView);
// EditText txtSearch=(EditText)
// view.findViewById(R.id.dialog_txtSearch);
MyTTCDB db = new MyTTCDB(getActivity());
List<Stop> allStops = db.getAllStopsByRouteAndSide(routeName, side);
myAdapter = new AdapterStops(getActivity(), allStops);
listStops.setAdapter(myAdapter);
}
}
I am getting errors like...
12-14 23:43:02.696: E/MessageQueue-JNI(7577): java.lang.NullPointerException
12-14 23:43:02.696: E/MessageQueue-JNI(7577): at com.anilvasani.myttc.FragmentStops.refresh(FragmentStops.java:34)
12-14 23:43:02.696: E/MessageQueue-JNI(7577): at com.anilvasani.myttc.StopsActivity$3.onClick(StopsActivity.java:120)
12-14 23:43:02.696: E/MessageQueue-JNI(7577): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:941)
So please help me, How to call method of Fragment from Activity based on user's input.
It seems your error is coming from the line:
String routeName = getActivity().getIntent()
.getStringExtra("routeName").toString();
Make sure of the following things:
That when you started the activity that you calling refresh method (the activity that the dialog is attached to) that you start the activity in an intent since getIntent() is trying to return that intent.
Next make sure that there is a String named "routeName" and that that String is not null.
I hope this helps
In my Android app I have a list of countries that I can add new entries to at any time when the app is running. This list is held in a database. I have tried several different ways of trying to implement an OnListItemClick but I cannot get it to work. Here is my class containing my list:
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity
{
private DBManager db;
Cursor cursor;
Button goEdit;
ListView listContent;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.initial_activity);
listContent = (ListView)findViewById(R.id.list);
goEdit = (Button)findViewById(R.id.goedit);
//Open database
db = new DBManager(this);
db.openToRead();
cursor = db.queueAll();
String[] from = new String[]{DBManager.KEY_ID, DBManager.KEY_YEAR, DBManager.KEY_CONTENT, DBManager.KEY_DESC};
int[] to = new int[]{R.id.editcountry, R.id.yeartext, R.id.countrytext};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
//go to add/delete screen
goEdit = (Button)findViewById(R.id.goedit);
goEdit.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Log.i("Test", "Now moving to the edit activity");
Intent intent = new Intent(MainActivity.this, EditList.class);
startActivity(intent);
}
});
}
//life cycles
protected void onPause()
{
super.onPause();
db.close();
}
#Override
protected void onDestroy()
{
super.onDestroy();
db.close();
finish();
}
}
Here is my class where I can choose to enter new countries into the list:
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class EditList extends Activity
{
private DBManager db;
Cursor cursor;
EditText editCountry, editYear, editDesc;
Button add, delete, back;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editCountry = (EditText)findViewById(R.id.editcountry);
editYear = (EditText)findViewById(R.id.edityear);
editDesc = (EditText)findViewById(R.id.editdesc);
add = (Button)findViewById(R.id.add);
delete = (Button)findViewById(R.id.delete);
back = (Button)findViewById(R.id.backmain);
//Open database and fill it with content, then close it
db = new DBManager(this);
db.openToWrite();
cursor = db.queueAll();
add.setOnClickListener(buttonAddOnClickListener);
delete.setOnClickListener(buttonDeleteAllOnClickListener);
//handle switching back to main screen
Log.i("Test", "back to main");
back = (Button)findViewById(R.id.backmain);
back.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
db.close();
//can't use "finish()" because then the list won't refresh with the new data
Log.i("Test", "Going back to the main screen");
Intent intent = new Intent(EditList.this, MainActivity.class);
startActivity(intent);
}
});
}
//insert new country button
Button.OnClickListener buttonAddOnClickListener = new Button.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Toast.makeText(getApplicationContext(), "Added!", Toast.LENGTH_LONG).show();
int year = Integer.parseInt(editYear.getText().toString());
String country = editCountry.getText().toString();
String desc = editDesc.getText().toString();
db.insert(year, country, desc);
updateList();
//clear text fields after use
editYear.setText(null);
editCountry.setText(null);
editDesc.setText(null);
}
};
//delete all button
Button.OnClickListener buttonDeleteAllOnClickListener = new Button.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Toast.makeText(getApplicationContext(), "Your list has been deleted!", Toast.LENGTH_LONG).show();
db.deleteAll();
updateList();
}
};
private void updateList()
{
cursor.requery();
}
#Override
protected void onDestroy()
{
super.onDestroy();
db.close();
finish();
}
}
I have previously implemented an OnListItemClick in a class where the data was statically held in an array. That class also extended ListActivity, which this one doesn't.
The difference between ListActivity and a standard Activity is that the ListActivity handled the OnItemClickListener interface mapping for you, and just provided an extra callback method. Without ListActivity, you'll need to add that plumbing yourself; i.e.
public class MainActivity extends Activity implements AdapterView.OnItemClickListener
{
...
ListView listContent;
#Override
public void onCreate(Bundle savedInstanceState)
{
...
listContent.setOnItemClickListener(this);
...
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//Callback logic here for clicked items
}
...
}
In your MainActivity add
#Override
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
// a list item is click do whatever you want
}