我最近在youtube上关于如何创建自定义数组适配器的Derek Banas的教程。在适配器代码中,他只为一个文本视图输入了一个字符串数组。我有两个文本视图和两个字符串数组,我已经输入了第一个文本视图的第一个字符串数组,就像在视频中一样,但我该怎么做呢?
以下是Derek Banas的视频教程:https://www.youtube.com/watch?v=rhj4_KBD6BQ&list=UUwRXb5dUK4cvsHbx-rGzSgw
这是我的适配器代码:
class HangarAdapter extends ArrayAdapter<String> {
public HangarAdapter(Context context,String[] values) {
super(context, R.layout.hangar_layout, values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.hangar_layout, parent, false);
TextView TextView1 = (TextView) theView.findViewById(R.id.textView1);
//TextView2 Is the text view i want the second string array to go into
TextView TextView2 = (TextView) theView.findViewById(R.id.textView2);
TextView1.setText(getItem(position));
ImageView theImageView = (ImageView) theView.findViewById(R.id.imageView);
return theView;
}
以下是我在活动中设置适配器的方法:
String[] ship = {"Scout Ship", "Ranger Ship" , "Gun Ship MK.1",
"Conquerer Ship", "Gun Ship MK.2", "Tank Ship",
"Battle Ship MK.1", "Titan Ship", "Battle Ship MK.2",
"Colossal Titan Ship"};
//shipDesc below is the second string array.
String[] shipDesc = {"10 Planets Every 5 Secs \n 100$",
"50 Planets Every 5 Secs \n 500$",
"100 Planets Every 5 Secs \n 1500$",
"500 Planets Every 4 Secs \n 3000$",
"1000 Planets Every 4 Secs \n 7500$",
"5000 Planets Every 4 Secs \n 15000$",
"10000 Planets Every 3 Secs \n 50000$",
"30000 Planets Every 3 Secs \n 100000$",
"60000 Planets Every 3 Secs \n 500000$",
"100000 Planets Every 1 Secs \n 1000000$"};
ListView hangarList = (ListView) findViewById(R.id.hangarList);
ListAdapter adapter = new HangarAdapter(this, ship);
hangarList.setAdapter(adapter);
答案 0 :(得分:0)
不是使用String-array,而是创建一个带有title和description字段的ship类,并制作一个ship-array。
Ship ship = (Ship) getItem(position);
textview1.setText(ship.getTitle());
textView2.setText(ship.getDesc());
答案 1 :(得分:0)
由于两个数组都有相同的长度所以这个
class HangarAdapter extends ArrayAdapter<String> {
//shipDesc below is the second string array.
String[] shipDesc = {"10 Planets Every 5 Secs \n 100$",
"50 Planets Every 5 Secs \n 500$",
"100 Planets Every 5 Secs \n 1500$",
"500 Planets Every 4 Secs \n 3000$",
"1000 Planets Every 4 Secs \n 7500$",
"5000 Planets Every 4 Secs \n 15000$",
"10000 Planets Every 3 Secs \n 50000$",
"30000 Planets Every 3 Secs \n 100000$",
"60000 Planets Every 3 Secs \n 500000$",
"100000 Planets Every 1 Secs \n 1000000$"};
public HangarAdapter(Context context,String[] values) {
super(context, R.layout.hangar_layout, values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.hangar_layout, parent, false);
TextView TextView1 = (TextView) theView.findViewById(R.id.textView1);
//TextView2 Is the text view i want the second string array to go into
TextView TextView2 = (TextView) theView.findViewById(R.id.textView2);
TextView1.setText(getItem(position));
TextView2.setText(shipDesc[position])
ImageView theImageView = (ImageView) theView.findViewById(R.id.imageView);
return theView;
}