2017-08-01 7 views
-2

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

読みやすいように改訂されました。改善されたコードインデント。 – GhostCat

答えて

0

あなたはあなただけのように、その下に示すようにコードを変更1.必要な2つのアダプターは必要ありません -

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

これにより、アダプタがアレイに設定されます。理想的には、1つのアダプターに2つの配列を配置することはお勧めしません。だからあなたはカスタムオブジェクトの配列を持っている必要があります。しかし、このシナリオのために私はあなたがそれをやる方法を説明します。 私が先に言ったように、理想的にあなたは、このようにそれを行うべきではありません。

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

将来のためのより良い方法 - 以下のようなあなたのgetView方法を変更します。 -

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で多くのチュートリアルを手に入れることができます。

+0

申し訳ありませんが、私はROMを取得するために何を変更する必要がありますか? –

+0

私のコードで行の文字列を取得する方法の最初の配列から取得していますString tool = tools_names [position]; –

+0

soo muuchありがとう、本当に助かりました –

関連する問題