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);
a0
a1
a2
How can I find them in this way:
a0
x0
a1
x1
a2
x2
Thanks in advance.
- The default ArrayAdapter
can handle only one TextView
.
- So if you want to do something like displaying 2 TextView
, then its better to go with Custom ArrayAdapter
.
See this link for Custom ArrayAdapter example:
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;
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.