简体   繁体   English

如何在Android App中添加简单的商品查询

[英]How to add simple product query in android App

I am new to Android. 我是Android新手。 I have some Products like a0, a1, a2. 我有一些产品,例如a0,a1,a2。 I want to add their definitions in the second line as x0, x1, x2. 我想在第二行中将它们的定义添加为x0,x1,x2。

Here is my main code: 这是我的主要代码:

private ListView lv;
ArrayAdapter<String> adapter;
ArrayList<HashMap<String, String>> productList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_eagle);

 String products[] = {"a0", "a1","a2", };
 String definitions[] = {"x0", "x1","x2",};

lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
    lv.setAdapter(adapter);

What I find: 我发现:

a0 00


a1 a1


a2 a2


How can I find them in this way: 如何以这种方式找到它们:


a0 00

x0 00


a1 a1

x1 x1


a2 a2

x2 2倍


Thanks in advance. 提前致谢。

- The default ArrayAdapter can handle only one TextView . - 默认的 ArrayAdapter只能处理一个 TextView

- So if you want to do something like displaying 2 TextView , then its better to go with Custom ArrayAdapter . -因此,如果您想要执行类似显示2 TextView ,则最好使用Custom ArrayAdapter

See this link for Custom ArrayAdapter example: 有关自定义ArrayAdapter示例的信息,请参见以下链接:

http://www.ezzylearning.com/tutorial.aspx?tid=1763429 http://www.ezzylearning.com/tutorial.aspx?tid=1763429

You need to change your adapter to something like this: 您需要将适配器更改为如下所示:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, products){
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null)
             convertView = View.inflate(getBaseContext(), android.R.layout.simple_list_item_2, null);

        ((TextView) convertView.findViewById(android.R.id.text1)).setText(products[position]);
        ((TextView) convertView.findViewById(android.R.id.text2)).setText(definitions[position]);

        return convertView;
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM