1

I am new to Android. I have some Products like a0, a1, a2. I want to add their definitions in the second line as 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


a1


a2


How can I find them in this way:


a0

x0


a1

x1


a2

x2


Thanks in advance.

4

2 回答 2

0

-默认 只能ArrayAdapter处理一个TextView

-所以如果你想做一些类似显示 2 的事情TextView,那么最好使用Custom ArrayAdapter.

请参阅此链接以获取自定义 ArrayAdapter 示例:

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

于 2012-11-29T09:20:36.097 回答
0

您需要将适配器更改为以下内容:

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;
    }
};
于 2012-11-29T09:20:41.640 回答