ArrayAdapterを使用してListViewを作成するのは初めてです。私のArrayAdapterはListViewに別の文字列を表示しません
しかし、2番目の文字列(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;
}
読みやすいように改訂されました。改善されたコードインデント。 – GhostCat