android - 自定义我的自动完成下拉菜单的格式

标签 android autocomplete textview format

我正在开发一个应用程序的 Android 版本,我正在尝试复制这张图片中显示的设计。 http://s1298.photobucket.com/user/Mitch_Thornton/media/Screenshot2013-06-03at50428PM_zps3d9b6acc.png.html?sort=3&o=0

我存储的数据是从网站输入的。到目前为止,我已经成功地做到了,所以自动完成功能可以正确地搜索数据。 我的问题是格式。我希望能够控制和风格化文本的某些部分。具体百分比。如果百分比高于 75%,则应为绿色,如果低于,则应为红色或黄色。

问题是,我递的是一根长绳子,所以我无法单独设置某些部分的样式。

通过添加空格和线条,我能够得到一个穷人版本的样式,但没有什么特别的...... 这是我到目前为止所拥有的 http://s1298.photobucket.com/user/Mitch_Thornton/media/Screenshot2013-06-03at51100PM_zps6859e634.png.html?sort=3&o=1

我是 Android 的新手,所以到目前为止我所学到的实际上是从教程和一些自学中获得的。非常感谢您的帮助

搜索页面:

public class Search_Page extends Activity implements TextWatcher {

AutoCompleteTextView myAutoComplete;

// PROBLEM: This initializes and defines the string array...It also says how
// big the string array can be
// So this limits how many items I can grab from Parse and fill in the
// array....
// I can only fill/replace as many items/elements that are defined right
// here
String item[] = { "Jack Daniels", "Captain Morgan", "Corona", "Dubra",
        "KeyStone Light", "Burnettes", "Budweiser", "Bud Light",
        "Smirnoff", "Grey Goose", "Smirnoff", "Mike's Hard Lemonade",
        "Poland Springs", "Yukon Jack", "Magic Hat",
        "Captain Morgan Black", "Absolut", "Absolut Raspberry",
        "Absolut Mandarin", "Absolut Peppar", "Absolut Citron",
        "Absolut Vanilla", "Wild Turkey" };
public String selection = "";
ParseObject dealsObject;
int n = 0;
int p = 0;
String mydate;
String objectId;

private String[] obj = { "Jack Daniels", "Captain Morgan", "Corona",
        "Dubra", "KeyStone Light", "Burnettes", "Budweiser", "Bud Light",
        "Smirnoff", "Grey Goose", "Smirnoff", "Mike's Hard Lemonade",
        "Poland Springs", "Yukon Jack", "Magic Hat",
        "Captain Morgan Black", "Absolut", "Absolut Raspberry",
        "Absolut Mandarin", "Absolut Peppar", "Absolut Citron",
        "Absolut Vanilla", "Wild Turkey" };

private int[] percent = { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4,
        5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2,
        3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_page);

    Parse.initialize(this, "vUz23Z6zdIL1jbxbVWeLpsSdu1ClTu3YiG30zTWY",
            "4BTyoq1QQKows8qVJV7lvU3ZokSRrLFyOCPzffwJ");

    myAutoComplete = (AutoCompleteTextView) findViewById(R.id.myautocomplete1);

    // Using this Parsequery...I can grab/locate the objects from the Parse
    // list...Here I grabbed the objects that have Burnettes and the price
    // of it
    // Eventually I may need to set a limit on how many results I will get
    // from the for loop....So far I think it is limited to 100 by default
    ParseQuery query = new ParseQuery("Deals");

    // query.whereEqualTo("Brand", "Burnettes");
    query.findInBackground(new FindCallback() {

        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                Log.d("Brand", "Retrieved " + objects.size() + " Brands");
                for (ParseObject dealsObject : objects) {
                    // use dealsObject.get('columnName') to access the
                    // properties of the Deals object

                    SimpleDateFormat format = new SimpleDateFormat(
                            "MMM dd, yyyy");
                    String date = format.format((dealsObject.getCreatedAt()));
                    objectId = dealsObject.getObjectId();
                    // Grabs the position of the object in Parse and spits
                    // out the Brand, price.
                    // This is then put into the autocomplete dropdown
                    percent[n] = dealsObject.getInt("Percentage");

                    item[n] = dealsObject.getString("Brand") + " "
                            + dealsObject.getString("Size") + " $"
                            + dealsObject.getString("Price")
                            + "                "
                            + dealsObject.getInt("Percentage") + "%" + "\n"
                            + date;
                    obj[n] = objectId;

                    n++;
                }

            } else {
                Log.d("Brand", "Error: " + e.getMessage());
            }
        }

    });
    myAutoComplete.addTextChangedListener(this);
    myAutoComplete.setAdapter(new ArrayAdapter<String>(this,
            R.layout.my_custom_dropdown, item));

    // When the user selects an element from the dropdown, it will
    // automatically
    // go to the DealPage
    myAutoComplete.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

            while (true) {
                if (arg0.getItemAtPosition(arg2).equals(item[p])) {
                    break;
                }
                p++;
            }

            selection = (String) arg0.getItemAtPosition(arg2);
            Log.d("Before", selection);

            Intent myIntent = new Intent("com.alpha.dealtap.DEALPAGE");
            myIntent.putExtra("Stuff", selection);
            myIntent.putExtra("ObjectId", obj[p]);
            myIntent.putExtra("Percent", percent[p]);

            startActivity(myIntent);
        }
    });
    Log.d("After", selection);

    Button deal = (Button) findViewById(R.id.b2);
    Button map = (Button) findViewById(R.id.map);

    deal.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            startActivity(new Intent("com.alpha.dealtap.DEALPAGE"));

        }

    });

    Intent myIntent = new Intent(Search_Page.this, DealPage.class);

    map.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent("com.alpha.dealtap.MAP"));
        }
    });

}

@Override
protected void onPause() {

    super.onPause();
}

@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

}

}

Search_Page.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#FFFFFF" >

<Button
    android:id="@+id/map"
    style="@style/ButtonText"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/b2"
    android:background="@drawable/yellow_button"
    android:text="Map"
    android:textSize="20sp" />

<Button
    android:id="@+id/b2"
    style="@style/ButtonText"
    android:layout_width="160dp"
    android:layout_height="50dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/yellow_button"
    android:text="Deal"
    android:textSize="20sp" />

<AutoCompleteTextView
    android:id="@+id/myautocomplete1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/map"
    android:completionThreshold="1"
    android:ems="10"
    android:hint="Search for deals" >

    <requestFocus />
</AutoCompleteTextView>

</RelativeLayout>

my_custom_dropdown.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:textColor="@color/black"
android:textSize="17sp" />

最佳答案

好吧,答案是自定义您的 ListView ,而不是设置一个长字符串,而是使用具有不同属性的模型,在这里看一个很好的例子 how to customize listview row android

查看其中扩展了代表每一行的模板的适配器

public View getView(int position, View convertView, ViewGroup parent){
        String myText = getItem(position);           

        if(convertView == null){ // If the View is not cached
            // Inflates the Common View from XML file
            convertView = this.inflater.inflate(R.id.my_row_layout, null);
        }

        // Select your color and apply it to your textview
        int myColor;
        if(myText.substring(0, 1) == "a"){
            myColor = Color.BLACK;
        }else{
        ....
        }

关于android - 自定义我的自动完成下拉菜单的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906302/

相关文章:

java - 从另一个类获取按钮调用

java - 如何将 ListView 项添加到整个其他 ListView

c# - 自动完成中的Visual Studio代码顺序

android - 按钮中的自定义 TextView

javascript - jquery.autocomplete.js 触发 radio 选择和取消触发

javascript - 如何使用 angucomplete-alt 中的值数组作为本地数据?

android - 如何计算 EditText 框的总和?

java - 单击时更改通知按钮图标

android - 增量 OTA zip 未生成

android - Intel XDK Crosswalk 构建中的 cordova.file