【问题标题】:Customizing the format of my AutoComplete Drop Down自定义我的自动完成下拉菜单的格式
【发布时间】:2013-05-30 04:02:28
【问题描述】:

我正在开发一个应用程序的 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" />

【问题讨论】:

    标签: android autocomplete textview format


    【解决方案1】:

    答案是自定义您的列表视图,而不是设置一个长的单个字符串,而是使用具有不同属性的模型,在此处查看一个很好的示例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{
            ....
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2011-01-12
      相关资源
      最近更新 更多