自定义我的自动完成下拉列表的格式

时间:2013-06-03 21:40:46

标签: 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的新手,所以我到目前为止所获得的内容主要来自教程和一些自学。我非常感谢您的帮助

Search_Page:

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" />

1 个答案:

答案 0 :(得分:1)

答案是自定义列表视图而不是设置长单字符串,而是使用具有不同属性的模型,在这里查看一个很好的示例how to customize listview row android

查看模板膨胀的adpapter,它代表每一行

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{
        ....
        }