gpt4 book ai didi

java - 如何在教程中的数组适配器中设置第二个 TextView 的文本

转载 作者:行者123 更新时间:2023-12-03 06:26:21 26 4
gpt4 key购买 nike

我最近关注了Derek Banas在youtube上的教程,该教程介绍了如何创建自定义数组适配器。在适配器代码中,他只为一个文本 View 输入了一个字符串数组。我有两个文本 View 和两个字符串数组,我已经为第一个文本 View 输入了第一个字符串数组,就像在视频中一样,但是我该怎么做第二个?

这是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;
}

这是我在 Activity 中设置适配器的方式:
    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);

最佳答案

由于两个数组的长度相同,因此

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;
}

关于java - 如何在教程中的数组适配器中设置第二个 TextView 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26044993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com