my first time doing an app on android. i have 4 strings and a lisview.i want to display the strings whenever user press the items on the listview on a toast.
can any one help me on this ?
For eg: there are Event 1 ,event 2 and event 3 on my list view.
and when user press event 1. it will display the date, time , venue for that event.
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Context context = getApplicationContext();
Toast.makeText(context,
"Date: "+date +" " +"Tme: "+ time, Toast.LENGTH_LONG).show();
Toast.makeText(context,"hi",Toast.LENGTH_LONG).show();
}
});
This is the codes for extracting it out from googleCalendar
date = contentResult[0].substring(10, 21);
time = contentResult[0].substring(21, 36);
location = contentResult[2].substring(6);
description = contentResult[4].substring(18);
eventTitle[i] = name;
eventDate[i] = date;
eventTime[i] = time;
eventVenue[i] = location;
eventContent[i] = description;
event[i] = name + " " + "Date: " +date + " " + "Time: " +time + " " +" Location:"+ location+ "Event Description:"+ description ;
You would need to set your lists onItemClickListener like so:
private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id)
{
// Display a messagebox.
Toast.makeText(this,"You clicked something!",Toast.LENGTH_SHORT).show();
}
};
myList.setOnItemClickListener(mMessageClickedHandler);
Of course you would have to input your own data (I'm guessing from a database or web service) to the Toast message indicating the date, time, and location of the event.
EDIT: In response to your comment, it sounds like you are new to Android development. You might want to check out this great tutorial to learn how to use SQLite, and the Android SDK in general.
Related
I have a ListView that I update from my SQLite database and I have set an onItemLongClickListener to help me delete from my database through a dialog interface. However, I can't seem to have access to notifyDataSetChanges not even if I make my adapter global.
This is my list population function
private void populateListView() {
Cursor data = mDatabaseHelper.getData();
ArrayList<String> listdata = new ArrayList<>();
while (data.moveToNext()) {
last_date = expire_Date(data.getString(4));
Calendar c = Calendar.getInstance();
c.setTime(last_date);
c.add(Calendar.DATE, 30);
Date currDate = new Date();
SimpleDateFormat dates = new SimpleDateFormat("M/d/yyyy");
currDate.setTime(System.currentTimeMillis());
int differenceDates;
differenceDates = difOfDay(c, currDate);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
month = month+1;
String date = month + "/" + day + "/" + year;
String first_row =data.getString(3) + " (" + data.getString(1) + ")\n";
String second_row = getResources().getString(R.string.red_info);
listdata.add(first_row + " " + second_row + differenceDates + " (" + date + ")");
}
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,listdata);
mListView.setAdapter(adapter);
}
And this is my delete function:
public AdapterView.OnItemLongClickListener myClickListener = new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,final int position, final long id) {
AlertDialog alert = new AlertDialog.Builder(view.getContext())
.setTitle("Delete ")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mDatabaseHelper.deletechute(position);
Toast.makeText(getApplicationContext(), "Parachute deleted", Toast.LENGTH_LONG).show();
adapter.notifyDataSetChanged(); ////Here my code can't resolve my adapter///
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).show();
return false;
}
};
This just looks firstly like a plain Java Variable scope problem but there are other problems as well but it is very hard to tell without more code.
So.
Problem 1
The adapter is defined and initialized in the scope of populateListView.
When this method ends, the reference is removed and the variables memory area can be deleted by garbage collection.
I suggest you read https://www.java-made-easy.com/variable-scope.html
You could fix this by defining the adapter as a private variable in the scope of your Activity Class and then it will be available to all methods in the class.
Problem 2
If you have fixed Problem 1 then adapter.notifyDataSetChanged(); still won't do anything.
This is because you are notifying it that the ArrayList<String> listdata has changed when you have done nothing to update the List and you cannot update it because again there is not available reference to listdata because it was also defined in local scope.
This again could be fixed by making listdata a private variable in the scope of your Activity but that leads to:-
Problem 3
This is a very convoluted way of doing this having to keep the database and list in sync manually.
You would be better off with a Cursor Adapter e.g. https://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
A guide on Cursor Adapters with Listviews at https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter
Then in the OnClick method you would update your database, and get a new Cursor of all the results again and swap the new Cursor for the old and then adapter.notifyDataSetChanged();
If I populate my listview with the primary key of my table I can get this primary key using getItemAtPosition and then it works fine.
The problem is that I don't want to use the primarykey to populate de listview, instead I want to use other fields of my table. Doing that, when I use the getItemAtPosition comand, because itsn't unic I can't use this to select my register.
I thought about using getItemIdAtPosition but I didn't reached any solution.
public void populateListView() {
//get the data and append to the list
Cursor data = db.getAllDataFillup(selectedID);
ArrayList<String> listData2 = new ArrayList<>();
while (data.moveToNext()) {
//listData2.add("FILLUP_ID: " + data.getString(0) + " FILLUP_VEHICLE_ID: " + data.getString(1));
//listData2.add(data.getString(7) + " " + data.getString(8) + " " + data.getString(2));
listData2.add(data.getString(3));
//listData2.add(data.getString(2));
}
//create the list adapter and set the adapter
ListAdapter adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listData2);
list_fillup.setAdapter(adapter2);
//set onItemClickListener to the listView
list_fillup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
int fillupID = Integer.parseInt(adapterView.getItemAtPosition(i).toString());
long position = adapterView.getSelectedItemId();
//long a = list_fillup.get(codigoDoObjeto).getCodigoIdOuPKQualquer();
toastMessage("position: " + position);
//toastMessage("fillupPosition: " + fillupPosition);
//long fillupPosition = adapterView.getItemIdAtPosition(i);
Log.d(TAG, "onItemClick: You Clicked on " + fillupID);
Cursor data = db.getDataTableFillup(fillupID);//get the data associated with that fillupID
fillupID = -1;
while (data.moveToNext()) {
fillupID = data.getInt(0);
vehicleID = data.getInt(1);
fillupDate = data.getString(2);
odometer = data.getLong(3);
kmDriven = data.getLong(4);
liters = data.getLong(5);
consumption = data.getLong(6);
label = data.getString(7);
sequence = data.getInt(8);
}
if (fillupID > -1) {
Log.d(TAG, "onItemClick: The ID is: " + fillupID);
Intent screenVehicle = new Intent(Vehicle_painel.this, Fillup_edit.class);
screenVehicle.putExtra("fillupID", fillupID);
screenVehicle.putExtra("vehicleID", vehicleID);
screenVehicle.putExtra("vehicleName", selectedName);
screenVehicle.putExtra("date", fillupDate);
screenVehicle.putExtra("odometer", odometer);
screenVehicle.putExtra("kmDriven", kmDriven);
screenVehicle.putExtra("liters", liters);
screenVehicle.putExtra("consumption", consumption);
screenVehicle.putExtra("label", label);
screenVehicle.putExtra("sequence", sequence);
//toastMessage("fillupPosition: " + fillupPosition);
startActivity(screenVehicle);
} else {
toastMessage("fillupID = " + fillupID);
//db.deleteAllFillup(selectedID);
//toastMessage("No ID associated with that name hahaha");
}
The best thing to do would be to create a custom class to hold your data. That way you no longer just get a simple String value back from your adapter. Your ArrayList would be something like:
ArrayList<YourCustomClass> listData2 ...
Create a custom class "YourCustomClass" (Call it what ever you like). It could look like:
public class YourCustomClass {
private long itemId = 0;
private String itemName;
private String itemDescription;
public YourCustomClass(){
}
public void setItemId(long id){ this.itemId = id; }
public void setItemName(String itemName){ this.itemName = itemName; }
public void setItemDescription(String itemDescription){ this.itemDescription = itemDescription; }
public long getItemId() { return this.itemId; }
public String getItemName(){ return this.itemName; }
public String getItemDescription(){ return this.itemDescription; }
}
Now in your onItemClick method get the Id and the other data like this:
YourCustomClass data = (YourCustomClass) adapterView.getItemAtPosition(i);
long orderId = data.getItemId();
String name = data.getItemName();
You will need a custom adapter to populate your ListView with data.
You can also take a look at this answer. It shows how to change the background color of a ListView item, but also shows more detail of how to implement a custom adapter for your ListView.
How to set background color for each item listview depanding on variable value
I am simply displaying a list from sqlite table. I have not used any BDHelper class. With this code only how can i get id.
When i click on 1st Item, it shows 0 where as in table it's id is 1. Below is my code.
SQLiteDatabase myDB;
try {
myDB = openOrCreateDatabase(DB_NAME,
SQLiteDatabase.CREATE_IF_NECESSARY, null);
myDB.execSQL("create table if not exists " + COUNTRY_TABLE
+ "(country_id INTEGER PRIMARY KEY,"
+ "country_title text,"
+ "country_status int(11));");
/*myDB.execSQL("insert into " + COUNTRY_TABLE +" values "+
"(null,'India',1)," +
"(null,'China',1)," +
"(null,'Australia',1)," +
"(null,'Japan',1)," +
"(null,'Germany',1)");*/
Cursor c = myDB.rawQuery("select country_id,country_title from "
+ COUNTRY_TABLE + " where country_status=1", null);
if (c != null) {
if (c.moveToFirst()) {
do {
int id = c.getInt(c.getColumnIndex("country_id"));
String name = c.getString(c.getColumnIndex("country_title"));
clist.add(id + ") " + name);
} while (c.moveToNext());
//int itemcount = clist.size();
//Toast.makeText(MainActivity.this, itemcount, Toast.LENGTH_LONG).show();
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, clist));
}
}
} catch (SQLiteException se) {
Toast.makeText(MainActivity.this, se.getMessage().toString(),Toast.LENGTH_LONG).show();
}
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(MainActivity.this,String.valueOf(id),Toast.LENGTH_LONG).show();
}
Please suggest what should i do to get the id fron table and not the position.
try below code:
Put your id and name data in clist ArrayList, generate getter, setter method first, set your id and name in that method when myou get that data, each time add that data in ArrayList and then use it like below.
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(MainActivity.this,clist.get(position).getId(),Toast.LENGTH_LONG).show();
}
public List<Emp> AllRecords()
{
List<Emp> dataset = new ArrayList<Emp>();
Cursor cursor = database.query(DatabaseHelper.TABLE_EMPLOYEE,allColumns, null, null,null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Emp obj = cursorToContact(cursor);
dataset.add(obj);
cursor.moveToNext();
}
cursor.close();
return dataset;
}
private Emp cursorToContact(Cursor cursor)
{
Emp obj = new Emp();
obj.setId(cursor.getInt(0));
obj.setName(cursor.getString(1));
obj.setDesig(cursor.getString(2));
return obj;
}
//--------------
list_data = (ArrayList<Emp>) qh.AllRecords();
CustomListAdapter adapter = new CustomListAdapter(MainActivity.this,list_data);
//---------
int id=list_data.get(arg2); //Type mismatch: cannot convert from Emp to int
Plz tell what should i write to get country id.
Seeing as how there is no accepted answer...
I did this and it seemed to work.
I'm using a ListView Activity, SQLiteDatabase and an ArrayAdapter
In my onListItemClick method, I instantiate a new object and make it equal to whatever item is at that current position in my adapter.
Task task = adapter.getItem(arg3);
In my task class, I have a private variable of type long called id, as well as a setter and getter for that variable.
Hence, I can then do this:
long taskId = task.getId();
Checking this result with Toast yielded the id generated by SQLite NOT the position in the ListView, which is what I needed.
Hope this helps someone out!
you can set tag on list item with the id after which you can call getTag() on onItemClick.
((TextView)v).getText(); by using this we will get the text of view, then we can query to the table again to get id of the corresponding text and toast it.
String country_title=((TextView)v).getText();
Cursor c = myDB.rawQuery("select country_id from "+ COUNTRY_TABLE + " where country_title='"+country_title+"'", null);
Toast.makeText(MainActivity.this,c.getString(c.getColumnIndexorThrow("country_title")),Toast.LENGTH_LONG).show();
This was not possible the way i wanted it but got its solution some other way. While in cursor loop, adding data to list, take another ArrayList and add only id to it.
clist1.add(id);
Now, on onListItemClick, you can get the position of the item. So, use this position and get the id from the other ArrayList clist1
int country_id=clist1[position];
Hope this helps.
I'm a couple years late, but it might help someone else: basically set the tag wherever you're setting the individual views.
// Find fields to populate in inflated template
TextView productName = (TextView) view.findViewById(R.id.product_name);
TextView productPrice = (TextView) view.findViewById(R.id.product_price);
TextView productQuantity = (TextView) view.findViewById(R.id.product_quantity);
// Extract properties from cursor
String name = cursor.getString(cursor.getColumnIndexOrThrow(ProductDbContract.ProductEntry.COLUMN_NAME));
int price = cursor.getInt(cursor.getColumnIndexOrThrow(ProductDbContract.ProductEntry.COLUMN_PRICE));
int quantity = cursor.getInt(cursor.getColumnIndexOrThrow(ProductDbContract.ProductEntry.COLUMN_QUANTITY));
int id = cursor.getInt(cursor.getColumnIndexOrThrow(ProductDbContract.ProductEntry.ID));
// Set Tag
productName.setTag(id);
and then later you can access it by calling on getTag():
int id = (Integer)((TextView)findViewById(R.id.product_name)).getTag();
Log.d("LETSEE",String.valueOf(id));
I have a ArticlesAdapter which I am using to read remote JSON data. The adapter is working but I am struggling to get my ListView setonitemclicklister working.
What I am trying to accomplish is the ability to click on a ListItem and to get the Article data for the option clicked on.
I have functions like getName() in my Article class which I need to call in response to a ListItem click but I can't seem to find a way to accomplish it.
I am trying to do this in my AsyncTask class.
Using the following code:
protected void onPostExecute(JSONArray result) {
// TODO Auto-generated method stub
Log.i("CHECK", "RESULTS: " + result);
List<Article> articles = new ArrayList<Article>();
String title = null;
String nid = null;
try{
for(int i=0; i < data.length(); i++){
JSONObject dataObj = (JSONObject)data.get(i);
JSONObject record = dataObj.getJSONObject("node");
title = (record.getString("title"));
nid = (record.getString("nid"));
Log.i("FOUND", "title: " + title);
Log.i("FOUND", "nid: " + nid);
articles.add( new Article(title, "", Integer.parseInt(nid)) );
}
}catch(JSONException j){
Log.e("CHECK", "Attempting to read data returned from JSONReader: " + j.toString());
}
ListView articlesList = (ListView)findViewById(R.id.articlesList);
ArticleAdapter adapter = new ArticleAdapter(ArticlesActivity.this, R.layout.article_item, articles);
articlesList.setAdapter(adapter);
articlesList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "testing : ", Toast.LENGTH_LONG);
Log.i("CHECK", "AdapterView: " + parent);
Log.i("CHECK", "VIEW : " + view);
Log.i("CHECK", "POSITION : " + position);
Log.i("CHECK", "ID : " + id);
}
});
if(dialog.isShowing())
dialog.dismiss();
}
My problem is I am not sure how to get the setOnItemClickListener working.
I am able to print the arguments via the Log function:
Log.i("CHECK", "AdapterView: " + parent);
Log.i("CHECK", "VIEW : " + view);
Log.i("CHECK", "POSITION : " + position);
Log.i("CHECK", "ID : " + id);
...but I get errors when I try casting any of the arguments into an Article object so I can call it's getName() function etc.
Your AsyncTask appears to be nested in your Activity, if it isn't simply pass articles to your Activity, then use:
articlesList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Article article = articles.get(position);
// Do something with article
}
});
An architectural change you would want to do is pull your ListView, Adapter, and OnItemClickListener initialization out of the AsyncTask and only update articles as well as notify the Adapter in the onPostExecute callback.
I have a simple ListActivity that uses the SimpleCursorAdapter. I allow users to change one of the values using an EditText. I perform simple validation to make sure that the number entered is less than 100. If the user entered value fails validation, I want to put the old value back.
I've tried a few different ways. My current approach is to requery it out of the database, but this isn't working. I'm always getting the value associated with the last entry in the ListActivity, regardless of which one was actually changed. I noticed in LogCat that onTextChanged and afterTextChanged are firing multiple times for each row in the ListActivity and not just the one that changed.
Here's the code:
public class MySimpleCursorAdapter extends SimpleCursorAdapter {
Context lcontext;
boolean changed;
String lastval;
private PortfolioData pfdata;
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
lcontext = context;
}
#Override
public View getView(final int pos, View v, ViewGroup parent) {
v = super.getView(pos, v, parent);
final EditText et = (EditText) v.findViewById(R.id.classpercentage);
final TextView tv = (TextView) v.findViewById(R.id._id);
et.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
Log.d("TEST", "In afterTextChanged s=" + s.toString() + " "
+ tv.getText() + " POS = " + Integer.toString(pos));
lastval = tv.getText().toString();
if (changed == true) {
String enteredValue = s.toString();
if (checkNullValues(enteredValue)) {
if (Float.parseFloat(enteredValue.trim()) > 100.0f) {
AlertDialog.Builder builder = new AlertDialog.Builder(
lcontext);
builder.setMessage("Percentage Value should be Less than 100");
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface arg0, int arg1) {
String sql = "select c.percentage as PERCENTAGE " +
"from asset_classes c WHERE c._id = " + lastval + ";";
pfdata = new PortfolioData(lcontext);
SQLiteDatabase db = pfdata.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, null);
if (cursor != null)
{
cursor.moveToFirst();
et.setText(cursor.getString(0));
}
cursor.close();
pfdata.close();
}
});
// End of the Alert
if (changed == true)
{
builder.show();
}
}
}
changed = false;
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// Log.d("TEST", "In beforeTextChanged start=" +
// Integer.toString(start) +" count="+ Integer.toString(count) +
// " after=" + Integer.toString(after) + " s=" + s + " " + tv);
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
Log.d("TEST", "In onTextChanged start=" +
Integer.toString(start) + " count=" + Integer.toString(count)
+ " before=" + Integer.toString(before) + " s=" + s + " " +
tv);
changed = true;
}
});
return v;
}
}
I would really appreciate a fresh perspective on this. As always, thanks in advance.
Try to use an onFocusChangeListener. When it gets focus, save the current text into field of the class.
Something like:
String oldText - Would be your old text field.
Then you do:
et.setOnFocusChangeListener(new OnFocusChangeListener()) {
#Override
public void onFocusChange(View whatever, boolean hasFocus) {
if (hasFocus) {
//code
} else {
//code or maybe empty
}
}
}
Then if the number is > 100 you just get the oldText value and put in the EditText.
I think you're not aware of recycling in ListView. Having 1000 rows in Cursor there is only 10-20 (depends on screen size) row views created.
Don't store data in view!
At beggining I recommend you to read http://commonsware.com/Android/excerpt.pdf