这是我第一次使用ArrayAdapter创建ListView。
但我不知道如何显示第二根弦(A,B,C ......)?
我认为我必须在主java文件中添加一些东西,但是我已经'不知道要添加什么?
这是我目前的代码:
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
public class equipements extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_equipements);
String[] tools_names = {"1" , "2" , "3" , "4" , "5" };
String[] tools_def = {"A" , "B" , "C" , "D" , "E" };
ListAdapter testAdapter = new eq_custom_adapter(this , tools_names);
ListAdapter testAdapter1 = new eq_custom_adapter(this , tools_def);
ListView test = (ListView)findViewById(R.id.eq_listView);
test.setAdapter(testAdapter);
test.setAdapter(testAdapter1);
}}
这是我的适配器
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.ImageView;
class eq_custom_adapter extends ArrayAdapter<String>{
eq_custom_adapter(Context context,String [] tools) {
super(context,R.layout.equipments_view ,tools);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater1 = LayoutInflater.from(getContext());
View customView = inflater1.inflate(R.layout.equipments_view , parent , false);
String tool = getItem(position);
String tool_defs = getItem(position);
TextView tool_name = (TextView) customView.findViewById(R.id.tool_name);
TextView tool_def = (TextView) customView.findViewById(R.id.tool_def);
ImageView tool_image = (ImageView)
customView.findViewById(R.id.tool_image);
tool_name.setText(tool);
tool_def.setText(tool_defs);
switch (position) {
case 0:
tool_image.setImageResource(R.drawable.a);
break;
case 1:
tool_image.setImageResource(R.drawable.b);
break;
case 2:
tool_image.setImageResource(R.drawable.c);
break;
case 3:
tool_image.setImageResource(R.drawable.d);
case 4:
tool_image.setImageResource(R.drawable.e);
break;
}
return customView;
}
答案 0 :(得分:0)
你不需要2个你需要的适配器1.所以改变代码如下所示 -
String[] tools_names = {"1" , "2" , "3" , "4" , "5" };
String[] tools_def = {"A" , "B" , "C" , "D" , "E" };
ListAdapter testAdapter = new eq_custom_adapter(this , tools_names);
ListView test = (ListView)findViewById(R.id.eq_listView);
test.setAdapter(testAdapter);
这会将您的适配器设置为数组。理想情况下,不建议使用2个数组来填充一个适配器。所以你应该有自定义对象的数组。但是对于这种情况,我将解释你如何做到这一点。更改您的getView
方法,如下所示 -
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater1 = LayoutInflater.from(getContext());
View customView = inflater1.inflate(R.layout.equipments_view , parent , false);
String tool = tools_names[position]; // change here to get from 1st array
String tool_defs = tools_def[position]; // change here to get from 2nd array
TextView tool_name = (TextView) customView.findViewById(R.id.tool_name);
TextView tool_def = (TextView) customView.findViewById(R.id.tool_def);
ImageView tool_image = (ImageView)
customView.findViewById(R.id.tool_image);
tool_name.setText(tool);
tool_def.setText(tool_defs);
switch (position) {
case 0:
tool_image.setImageResource(R.drawable.a);
break;
case 1:
tool_image.setImageResource(R.drawable.b);
break;
case 2:
tool_image.setImageResource(R.drawable.c);
break;
case 3:
tool_image.setImageResource(R.drawable.d);
case 4:
tool_image.setImageResource(R.drawable.e);
break;
}
return customView;
}
未来更好的方式: 正如我之前所说,理想情况下你不应该这样做。如果您要显示多个数据,请创建一个类 -
class Tool{
String toolName;
String toolDef;
int toolImageId;
public String getToolName() {
return toolName;
}
public void setToolName(String toolName) {
this.toolName = toolName;
}
public String getToolDef() {
return toolDef;
}
public void setToolDef(String toolDef) {
this.toolDef = toolDef;
}
public int getToolImageId() {
return toolImageId;
}
public void setToolImageId(int toolImageId) {
this.toolImageId = toolImageId;
}
}
然后创建此类的对象数组,然后从中填充列表视图。你可以在Google上获得很多有关此内容的教程。