Android Custom AutoComplete textview Symbol und text
Ich eine AutoCompleteTextView
angezeigt werden können text und ein Symbol. Bekomme ich Hilfe aus diesem Beispiel: http://wptrafficanalyzer.in/blog/customizing-autocompletetextview-to-display-images-and-text-in-the-suggestion-list-using-simpleadapter-in-android/
In diesem Beispiel werden Abbildungen verwendet, in drawable-Ordner des Projekts. Aber, ich möchte, um Bilder aus der Datenbank. Ich weiß nicht, wie ändern Sie den zweiten parameter code unten, um das Bild aus der Datenbank, anstatt drawable Ordner das Bild.
Update:
Es stellt sich heraus, dass ich brauche, um eine benutzerdefinierte adopter. Ich habe versucht den code unten, aber getView wird nicht genannt. Warum?
SearchItemArrayAdapter
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class SearchItemArrayAdapter extends ArrayAdapter<CountryEntry>
{
private static final String tag = "SearchItemArrayAdapter";
private CountryEntry listEntry;
private TextView autoItem;
private ImageView categoryIcon;
private List<CountryEntry> countryEntryList = new ArrayList<CountryEntry>();
/**
*
* @param context
* @param textViewResourceId
* @param objects
*/
public SearchItemArrayAdapter(Context context, int textViewResourceId, ArrayList<CountryEntry> objects)
{
super(context, textViewResourceId, objects);
countryEntryList = objects;
Log.d(tag, "Search List -> journalEntryList := " + countryEntryList.toString());
}
@Override
public int getCount()
{
Log.w(tag, "Size:= " + this.countryEntryList.size());
return this.countryEntryList.size();
}
@Override
public CountryEntry getItem(int position)
{
CountryEntry journalEntry = this.countryEntryList.get(position);
Log.d(tag, "*-> Retrieving JournalEntry @ position: " + String.valueOf(position) + " : " + journalEntry.toString());
//return journalEntry;
return countryEntryList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Log.w(tag, "GetView");
View row = convertView;
LayoutInflater inflater = LayoutInflater.from(getContext());
if (row == null)
{
row = inflater.inflate(R.layout.autocomplete_layout, parent, false);
}
listEntry = this.countryEntryList.get(position);
String searchItem = listEntry.title;
autoItem = (TextView) row.findViewById(R.id.txt);
autoItem.setText(searchItem);
//Get a reference to ImageView holder
categoryIcon = (ImageView) row.findViewById(R.id.flag);
categoryIcon.setImageBitmap(listEntry.image);
return row;
}
}
MainActivity
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AutoCompleteTextView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
AutoCompleteTextView mAutoCompleteTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap theImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//Add the country details
AutoCompleteTextView autoComplete = (AutoCompleteTextView) findViewById(R.id.autocomplete);
ArrayList<CountryEntry> list = new ArrayList<CountryEntry>();
//Add it to array
list.add(new CountryEntry("india", theImage));
list.add(new CountryEntry("usa", theImage));
SearchItemArrayAdapter adapter = new SearchItemArrayAdapter(this, R.layout.autocomplete_layout, list);
autoComplete.setAdapter(adapter);
}
}
CountryEntry
import android.graphics.Bitmap;
public class CountryEntry {
public String title;
public Bitmap image;
public CountryEntry(String title, Bitmap image) {
this.title = title;
this.image = image;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="@android:color/black"
android:hint="autocomplete"
android:completionThreshold="1"
/>
<TextView
android:id="@+id/tv_currency"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/autocomplete"
/>
</RelativeLayout>
autocomplete_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/hello_world"
android:padding="10dp"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:padding="10dp"
/>
</LinearLayout>
- Haben Sie geprüft ??
- Ich überprüfte es. Aber, nicht sicher über diese Linie iconRetriever = new CategoryIconRetriever(context); keine Ahnung?
- Sie können diese
iconRetriever.getJournalEntryTypeIcon(journalEntry.typeId))
und fügen Sie Ihren code von der Einstellung Bild in der Bildansicht
Du musst angemeldet sein, um einen Kommentar abzugeben.
Dieser link ist toll, aber es fehlt eine kritische Sache - um die für den adapter, um erfolgreich zu zeigen, das richtige auto-Vervollständigungen für Ihre eingetippte text,
Ihre "JournalEntry" (wie im Beispiel-link) implementieren muss, um seine eigene toString() für die Filterung:
Können Sie dieser link zu erfüllen Ihre Anforderung :
Klasse :
Den Adapter :
Füllen Sie das Array wie:
Dies ist der UI-thread-code
Adapter-code: