dialog with list containing autocomplete text view and a button - Android Software Development

I need to create a dialog with a list containing an autocomplete text view and a button for each line. So everything is working except for the autocomplete text view. My program crashes when i tried to call the setAdapter. that's my code:
Code:
private Dialog createSosteDialog() {
final ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
Button conferma_modifiche_soste;
ListView lv;
final Dialog sosteDialog = new Dialog(this);
sosteDialog.setTitle("Lista soste");
sosteDialog.setContentView(R.layout.modifica_soste_dialog_layout);
lv=(ListView)sosteDialog.findViewById(R.id.lista_write);
conferma_modifiche_soste=(Button)sosteDialog.findViewById(R.id.Conferma_modifiche_soste);
AutoCompleteTextView autocomplete=(AutoCompleteTextView)sosteDialog.findViewById(R.id.autocomplete_soste);
String[] fermate_dispositivo = getResources().getStringArray(R.array.Lista_fermate_dispositivo);
ArrayAdapter<String> adapter_autocomplete = new ArrayAdapter<String>(this, R.layout.auto_complete_item, fermate_dispositivo);
autocomplete.setAdapter(adapter_autocomplete); //ERROR WITH THIS LINE!!!!
HashMap<String, String> map;// = new HashMap<String, String>();
int i;
for(i=0;i<soste.length;i++){
map = new HashMap<String, String>();
map.put("sosta", soste[i].getNome());
map.put("orario", soste[i].getStringOrario());
mylist.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, mylist, R.layout.lista_soste_item_modifica,
new String[] {"sosta", "orario"},
new int[] {R.id.autocomplete_soste, R.id.lista_soste_cambia_orario});
//new String[] {"from", "to"} imposta le colonne "sosta" e "orario" della map come valori
lv.setAdapter(adapter);
return sosteDialog;
}
I don't know if this is the best way to do that, by the way i've to make a dialog with 1 autocomplete and a button for each line of the list inside that dialog. how could i do that?
thx everyone for your help!

any ideas?

i really need your help, maybe it's a problem with the SimpleAdapter?!?

Related

widget SMS for android

Hello,
I'm trying to write code of a widget sms for android. But I have a problem of cursor, after lot of test on compiling I dircoverd that
Code:
Cursor c = context.getContentResolver().query(Uri.parse("content://sms/"), null, null ,null,null);
make an error and I don't no why. If somebody knows how use a cursor or have a better idea to view sms without cursor, I woold like share it with him!
thank's
try something like this
Code:
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uriSms, null,null,null,null);
Thank's to you Draffodx, I such begin my widget, now it can put on screen the sms I want... but I can't change of SMS with th button I've created. I don't understand how make a button with the widget because it need to be an Activity for a button and I've made an AppWidget...
I trying to do like this:
Code:
public class MySMSwidget extends AppWidgetProvider implements View.OnClickListener {
private Button Bnext;
private int sms_id=0;
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.widget_layout);
final Button button = (Button) findViewById(R.id.next);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (v==Bnext){sms_id=sms_id+1;}
}
});
}
}.... and the rest of the code
But when I click the button, nothing happend.
hey, my idea seems to be a bad idea so I try this way:
Code:
public class MySMSwidget extends AppWidgetProvider {
private int sms_id=0;
public void onReceive (Context context, Intent intent){
if (Intent.ACTION_ATTACH_DATA.equals(intent.getAction()))
{
Bundle extra = intent.getExtras();
sms_id = extra.getInt("Data");
}
}
public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds) {
Cursor c = context.getContentResolver().query(Uri.parse("content://
sms/inbox"), null, null ,null,null);
String body = null;
String number = null;
String date = null;
c.moveToPosition(sms_id);
body = c.getString(c.getColumnIndexOrThrow("body")).toString();
number =
c.getString(c.getColumnIndexOrThrow("address")).toString();
date = c.getString(c.getColumnIndexOrThrow("date")).toString();
c.close();
RemoteViews updateViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
updateViews.setTextColor(R.id.text, 0xFF000000);
updateViews.setTextViewText(R.id.text,date+'\n'+number+'\n'+body);
ComponentName thisWidget = new ComponentName(context,
MySMSwidget.class);
appWidgetManager.updateAppWidget(thisWidget, updateViews);
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_ATTACH_DATA);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.next, changeData(context));
}
private PendingIntent changeData(Context context) {
Intent Next = new Intent();
Next.putExtra("Data", sms_id+1);
Next.setAction(Intent.ACTION_ATTACH_DATA);
return(PendingIntent.getBroadcast(context,
0, Next, PendingIntent.FLAG_UPDATE_CURRENT));
}
}
my code isn't terminated.
I hope there will be someone to help to correct it.
Just want to display next SMS.
Please help.

[SOLVED] Notification not showing in menubar

Hi,
the following code shows a notification in the menu bar. It works if I launch it from an activity, but it doesn't work if I launch it from a service (which runs when I receive BOOT_COMPLETED).
Code:
private void showNotification(String msg) {
String menuText = msg;
String contentTitle = getString(R.string.notify_title_safety);
String contentText = msg;
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(R.drawable.ico_notification, menuText, System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification);
}
I can't find any informations about why it works from the activity and not from a service. In fact, in the very first lines of the official docs it is suggested that Services use Notifications instead of Activities (which already have the attention of the user).
Any help/previous experience?
Dumb mistake: the code was not being called because the service name was missing the leading "." in AndroidManifest.xml

How to prevent laggy scrolling ListView w ImageView-Items filled with cached images?

Hello community,
I've got a ListActivity in my app, which shows Views including TextViews, for showing artist, title and album information of some mp3-Files (queried by my cursor), and an ImageView, for showing an albumart => Similar to the Tracklist in the music app.
Here's the code for my custom SimpleCursorAdapter and it's binding to the ListView:
Code:
void setListAdapterOverride() {
String[] fromColumns = new String[] {
AudioColumns.ARTIST,
MediaColumns.TITLE,
AudioColumns.ALBUM,
AudioColumns.ALBUM_ID
};
int[] toColumns = new int[] {
R.id.tv_artist,
R.id.tv_title,
R.id.tv_album,
R.id.iv_albumArt
};
cursorAdapter = new customAdapter(getBaseContext(), R.layout.listviewitem, cursor, fromColumns, toColumns);
setListAdapter(cursorAdapter);
if (MyDebug.Log)
Log.d("Activity", "ListAdapter gesetzt");
}
class customAdapter extends SimpleCursorAdapter {
int layout;
Cursor cursor;
String[] from;
int[] to;
public customAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.cursor = c;
this.from = from;
this.to = to;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.listviewitem, parent, false);
if (MyDebug.Log)
Log.d("Activity", "Inflate");
}
cursor.moveToPosition(position);
TextView artist = (TextView) row.findViewById(to[0]);
TextView title = (TextView) row.findViewById(to[1]);
TextView album = (TextView) row.findViewById(to[2]);
ImageView albumArt = (ImageView) row.findViewById(to[3]);
artist.setText(cursor.getString(cursor.getColumnIndex(from[0])));
title.setText(cursor.getString(cursor.getColumnIndex(from[1])));
album.setText(cursor.getString(cursor.getColumnIndex(from[2])));
Cursor albumArtCursor = contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, new String[] { MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART }, MediaStore.Audio.Albums._ID + "='" + cursor.getInt(cursor.getColumnIndex(from[3])) + "'", null, null);
albumArtCursor.moveToFirst();
String albumArtUri = albumArtCursor.getString(albumArtCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
if (albumArtUri == null) albumArtUri = "default";
if (!imageCache.containsKey(albumArtUri)) {
Bitmap albumArtBitmap;
if (!albumArtUri.equals("default")) {
Options opts = new Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(albumArtUri, opts);
Integer[] bitmapSize = new Integer[] { opts.outWidth, opts.outHeight };
Integer scaleFactor = 1;
while ((bitmapSize[0]/2 > 50) && (bitmapSize[1]/2 > 50)) {
scaleFactor++;
bitmapSize[0] /= 2;
bitmapSize[1] /= 2;
}
opts = new Options();
opts.inSampleSize = scaleFactor;
albumArtBitmap = BitmapFactory.decodeFile(albumArtUri, opts);
} else {
albumArtBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_mp_song_list);
}
imageCache.put(albumArtUri, albumArtBitmap);
}
albumArt.setImageBitmap(imageCache.get(albumArtUri));
return row;
}
}
imageCache is a Hashmap<String, Bitmap> for caching the Bitmaps, so they don't have to be decoded from file again when they had been cached before.
In a former version of the code, I didn't have the imageCache, so the albumarts were decoded from file everytime a new row has been added. That slowed down ListView-scrolling. By adding the imageCache scrolling in ListView doesn't lag that much as before, but it's lagging a bit sometimes.
Can anyone help me to further optimize my code to completely prevent lagging when I scroll the ListView?
Thanks regards ChemDroid

ListView with tabhost with listviews

I want to create a listview. On the last row of the listview, I want to show a tabhost with two tabs and on each tab I would like to load another listview. For this, I've created three XML files.
The main.xml is where I'm loading the initial list. The mainlistdata.xml is how each row of the list is presented. And the last xml is tabslayout.xml. This layout is used for the last row on the main listview. It will load the tabhost. Here is the code with all the xml
My main class is this:
Code:
public class MainActivity extends Activity {
ArrayList<testData> myData = new ArrayList<testData>();
ListView listContent;
TestAdapter dataAdapter;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] aString = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
for (int i = 0; i < aString.length; i++) {
myData.add(new testData(i, aString[i]));
}
listContent = (ListView) findViewById(R.id.mainlist);
dataAdapter = new TestAdapter(this, myData,1);
listContent.setAdapter(dataAdapter);
}
}
And this is the class that loads the content on the listviews and where I set the tabhost:
Code:
public class TestAdapter extends ArrayAdapter<testData> {
private Context context;
private ArrayList<testData> data;
private ListView listContent,listContent2;
public TabHost tabs;
TestAdapter dataAdapter;
private int choice;
public TestAdapter(Context context, ArrayList<testData> data, int choice) {
super(context, R.layout.mainlistdata, data);
this.context = context;
this.data = data;
this.choice = choice;
}
[user=439709]@override[/user]
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
if (choice == 1) {
if (position == data.size()-1) {
rowView = inflater.inflate(R.layout.tabslayout, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
tabs = (TabHost) rowView.findViewById(android.R.id.tabhost);
tabs.setup();
TabSpec tab1 = tabs.newTabSpec("Tab 1");
TabSpec tab2 = tabs.newTabSpec("Tab 2");
tab1.setContent(R.id.tabOne);
tab2.setContent(R.id.tabTwo);
tab1.setIndicator("One");
tab2.setIndicator("two");
tabs.addTab(tab1);
tabs.addTab(tab2);
tabs.setup();
listContent = (ListView) rowView.findViewById(R.id.listOne);
dataAdapter = new TestAdapter(context, data,2);
listContent.setAdapter(dataAdapter);
listContent2 = (ListView) rowView.findViewById(R.id.listTwo);
dataAdapter = new TestAdapter(context, data,3);
listContent2.setAdapter(dataAdapter);
} else {
rowView = inflater.inflate(R.layout.mainlistdata, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
}
} else if (choice == 2) {
rowView = inflater.inflate(R.layout.mainlistdata, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item tab1 " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
} else {
rowView = inflater.inflate(R.layout.mainlistdata, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item tab2 " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
}
return rowView;
}
}
As you can see on the next image, I'm loading the data and then I'm loading the tabs. The problem is that each tab is only loading the first row of the data. Any idea How can I solve this?
favolas said:
I want to create a listview. On the last row of the listview, I want to show a tabhost with two tabs and on each tab I would like to load another listview. For this, I've created three XML files.
As you can see on the next image, I'm loading the data and then I'm loading the tabs. The problem is that each tab is only loading the first row of the data. Any idea How can I solve this?
Click to expand...
Click to collapse
I don't quite understand what you are trying to do but why do you want the tabs in the last item of the list? Could you just put the tabs in the main activity and add a scrollview to bring the tabs up or set the weight of the layout so the tabs take up the bottom of the screen and the top list would scroll if to long?
Your problem may be fixed if you set the tabs and the lists inside to be a certain size (200dp or something). I know sometimes having it set to wrap_content will only show one list item
Acela1230 said:
I don't quite understand what you are trying to do but why do you want the tabs in the last item of the list? Could you just put the tabs in the main activity and add a scrollview to bring the tabs up or set the weight of the layout so the tabs take up the bottom of the screen and the top list would scroll if to long?
Your problem may be fixed if you set the tabs and the lists inside to be a certain size (200dp or something). I know sometimes having it set to wrap_content will only show one list item
Click to expand...
Click to collapse
Hello. Thanks for your answer.
My initial idea was to create a scrollview. In this scrollview, create the main listview, load the tabhost and them, in each tab, load another listview. After seeing this question, I saw that we shouldn't place a listview inside one scrollview. That's why I came up with this solution.
I've tried to do your solution but changing the height did not solve the problem.
After some debugging I belive that this is not working because the first list loads the last row (the one with the tabs) and then returns, not giving time to load the other lists. Can this be the problem?
Regards,
favolas
favolas said:
Hello. Thanks for your answer.
My initial idea was to create a scrollview. In this scrollview, create the main listview, load the tabhost and them, in each tab, load another listview. After seeing this question, I saw that we shouldn't place a listview inside one scrollview. That's why I came up with this solution.
I've tried to do your solution but changing the height did not solve the problem.
After some debugging I belive that this is not working because the first list loads the last row (the one with the tabs) and then returns, not giving time to load the other lists. Can this be the problem?
Regards,
favolas
Click to expand...
Click to collapse
But why? Why not just ActionBar Tabs with ListViews in them? At any rate, you can only have one View in a ScrollView, which is why people always put i.e. a RelativeLayout in a ScrollView, with the other Views in there.
Hi bassie1995.
Sorry for the late feedback.
bassie1995 said:
But why? Why not just ActionBar Tabs with ListViews in them?
Click to expand...
Click to collapse
This is just because my data is this way. Will then try to follow your suggestion and do actionbar tabs and put listview in them.
Regards,
Favolas

Display default contact photo in listView when no photo assigned?

I have a listview which displays the Starred Contacts with photo and name. I've managed so far to display the photos of the contacts which have one. The problem is that when a contact hasn't a photo only the name displayed and not the default contact(from drawables).
Here is the related code:
Code:
Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED};
String selection =ContactsContract.Contacts.STARRED + "='1'";
Cursor cursor = managedQuery(queryUri, projection, selection,null,null);
long id= cursor.getColumnIndex(ContactsContract.Contacts._ID);
Bitmap bitmap = loadContactPhoto(getContentResolver(), id);
if(bitmap!=null){
favIcon.setImageBitmap(bitmap);
}
else{
}
String[] from = {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts.DISPLAY_NAME};
int to[] = new int[]{
R.id.ivDefContact,
R.id.tvContactName
};
ListAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.favs_list_item,
cursor,
from,
to,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
final ListView listStarred = (ListView) findViewById(R.id.lvFavs);
listStarred.setAdapter(adapter);
}
public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
if (input == null) {
return null;
}
return BitmapFactory.decodeStream(input);
}
How can I achive this?
EDIT:
The app run fine, but I also get these errors on logcat:
I/System.out: resolveUri failed on bad bitmap uri:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /: open failed: EISDIR (Is a directory)
Click to expand...
Click to collapse
Did you managed to fix this? I have the same problem.

Categories

Resources