I want to get id of particular name that i selected in spinner.i did like this but it will print "_id" how can i retrieve id of particular name.
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String sel_name,numbers;
//String[] name=new String[]{arg0.getItemAtPosition(arg2).toString()};
sel_name=arg0.getItemAtPosition(arg2).toString();
String[] p=new String[]{People._ID};
Cursor cur=getContentResolver().query(People.CONTENT_URI, p,People.NAME+"= '"+sel_name+"'" ,null , null);
String r=cur.getString(cur.getColumnIndex(People._ID));
Toast.makeText(arg0.getContext(),r, Toast.LENGTH_LONG).show();
}
You need to add a cursor.moveToFirst(); call before you use the cursor.
If you are refering to sel_name, then this is what you should do to get the view's id:
sel_name = String.valueOf(arg0.getChildAt(arg2).getId());
add following after cursor
while(cur.moveToNext)
{
String r=cur.getString(cur.getColumnIndex(People._ID));
}
by id of perticular name if you mean a string den avoid to do so . alw
ays use id as int only ,because R file have int entries only , so will be easy to handle .
for this i guess in onItemSelected
arg1.getId() will work ........
or try setTag(id) in get view of customAdapter of spinner , then in onItemSelected int id = arg1.getTag();
Related
I have a array of strings which is a resultant of options selected from a MultiAutoCompleteTextView using the comma tokenizer. The string appears as below:
"India, China, Japan, America, Australia"
I need to seperate this values based on the coma positiona and set those values to different textview's. Also I need to restrict the users to select only 5 values and those values should not be repeated.
You can use String.split() like this:
String[] array = yourString.split(",");
And iterate through that array.
EDIT:
To check if the user already selected the item, you can add an OnItemClickListener to your multiAutoCompleteTextView and have a HashSet or a Set where your items clicked are stored and check if this item already exists in the set
Example:
Initialize your HashSet first: HashSet<String> hashset = new HashSet<String>();
And later:
youMultiAutoCompleteTv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemClicked = parent.getAdapter().getItem(position).toString();
if(hashset.contains(itemClicked)){
Toast.makeText(getApplicationContext(), "Item exists", Toast.LENGTH_SHORT).show();
}
else {
hashset.add(itemClicked);
}
}
});
Use something like this:
String myListAsString = "India, China, Japan, America, Australia";
String[] countries = myListAsString.split(",");
TextView t1 = (TextView)findViewById(R.id.t1);
TextView t2 = (TextView)findViewById(R.id.t2);
TextView t3 = (TextView)findViewById(R.id.t3);
TextView t4 = (TextView)findViewById(R.id.t4);
TextView t5 = (TextView)findViewById(R.id.t5);
t1.setText(countries[0]);
t2.setText(countries[1]);
t3.setText(countries[2]);
t4.setText(countries[3]);
t5.setText(countries[4]);
You can use this :
String input = editText.getText().toString().trim(); //Getting String from view.
String[] singleInputs = input.split("\\s*,\\s*"); //Splitting in proper way.
In my application I have a spinner that displays a number of spending periods (weekly, bi-weekly, monthly, etc) and I filled it using a SimpleCursorAdapter because those periods are kept in the database. Here is how I implemented it:
Cursor spendingPeriodCursor = dataSource.getSpendingPeriods();
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, spendingPeriodCursor, fromColumns, toColumns);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpendingSpinner.setAdapter(mAdapter);
The spinner populates, and at the end of the activity I try to get the selected item like this:
// Get spending period description:
String spendingDesc = mSpendingSpinner.getSelectedItem().toString();
But it doesn't return the actual string value. It looks like a get a string representation of part of the cursor:
android.database.sqlite.SQLiteCursor#410dfae8
What do I have to do to get the actual text of the selected item?
Cursor cursor = (Cursor) mSpendingSpinner.getSelectedItem();
String text = cursor.getString(cursor.getColumnIndex("my_column_name"));
1) Well, with will be enough with:
String spendingDesc = mSpendingSpinner.getSelectedItem().toString();
2) but you can try too:
mSpendingSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View arg1,
int pos, long arg3) {
String spendingDesc= parent.getSelectedItem().toString();
}
}
3) or
Cursor cursor = (Cursor) mSpendingSpinner.getSelectedItem();
String text = cursor.getString(cursor.getColumnIndex("COLUN_NAME"));
Im using a Filter for an ArrayAdapter. Now I would like to have following Feature:
For Example my Listview contains the word käuflich and the search Input is kauf, käuflich should be displayed as result. So ö,ä,ü should be replaced additionally by o,a,u.
Is this possible?
This is my simple filter
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Activity.this.adapter.getFilter().filter(cs);
}
Thanks
You can use the java.text.Normalizer to remove the accents. When iterating over list items in Filter.performFiltering(CharSequence), do the following:
for(int i = 0; i<Original_Names.size(); i++){
filterableString = Original_Names.get(i);
String s = Normalizer.normalize(filterableString, Form.NFD);
// Remove Accents
String withoutAccents =
s.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
if(withoutAccents.toLowerCase().contains(filterString)){
Filtered_Names.add(filterableString);
}
}
A word of caution here: Link
I really need your help.
I have a database with latitude and longitude data, here the example:
id_____name_____lat_________long
1_____hotel one___6.1234______-106.12345
2____hotel two____6.54321_____-107.23456
3___hotel three___7.12345_____-106.98765
The data display in ListView on Android. And i use onItemClick, so if i click one of the item in ListView it will go to
Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?f=d&saddr=&daddr="+lat_var+","+long_var))
But the problem is if i click one of the item in ListView the data in lat_var and long_var on intent above won't give me the lat and long data from my database...
Here my code, can you edit it, so if i click one of the item in ListView it will give me the lat and long data from my database. For example if i click hotel two the lat_var will change to 6.54321 and the long_var will change to -107.23456 on that intent.
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
SQLiteDatabase dbs = dbHotelHelper.getReadableDatabase();
String get_lat = "SELECT lat FROM hoteltbl";
Cursor result_lat = dbs.rawQuery(get_lat, null);
double lat_var = result_lat.getColumnIndex("lat");
String get_long = "SELECT lat FROM hoteltbl";
Cursor result_long = dbs.rawQuery(get_long, null);
double long_var = result_long.getColumnIndex("long");
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?f=d&saddr=&daddr="+lat_var+","+long_var));
startActivity(intent);
}
I don't know if the problem is my syntax or what. Please fix my code. I really thanks for anyone who answer this...
First, only one query is necessary. No need to query your table twice.
Second, you need to adapt your query with parameters from your selection:
#Override
protected void onListItemClick(ListView listView, View view, int position, long id) {id) {
String selectedItem = (String) getListAdapter().getItem(position);
String query = "SELECT lat, long FROM hoteltbl WHERE name = '" + selectedItem + "'";
SQLiteDatabase dbs = dbHotelHelper.getReadableDatabase();
Cursor result = dbs.rawQuery(query, null);
result.moveToFirst();
double lat = result.getDouble(result.getColumnIndex("lat"));
double long = result.getDouble(result.getColumnIndex("long"));
....
This code was written in the StackOverflow editor, it might not work as such, but will give you an idea.
(Also, don't add suffixes "_var", or "_long" to your variables, it's not necessary in a strongly-typed language like Java)
This is my problem.
I have a ListView, each row is a CheckedTextView.
The list view items are "1", "2" and "3".
When a ListItem is clicked, I want to read the number and assign it to an int variable.
I did the following to read the Text of the clicked item:
onItemClick(AdapterView<?> parent, View v, int position, long id) {
int num = 0; //initialise to 0
CharSequence s = ((TextView)v).getText();
// s contains the number, how to get it into num?
}
Basically, I want the number read in s to be converted and given to num.
I know it maybe simple, but please help if you have an answer..
Regards,
Kiki
String aString = "78";
int aInt = Integer.parseInt(aString);