2016-10-12 8 views
0

私は、次のコードを持っている:私はarrayAdapterを使用してリストビュー内で表示するように配列 "から" を取得しようとしているリストビューの2D <String>配列の内容を同じ水平線の下に表示するにはどうすればよいですか?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="zathstudio.projetliste.MainActivity"> 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"></ListView> 

import android.app.ListActivity; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
public class MainActivity extends ListActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     String[][] from ={{"jonathan","guillaume","gerard"},{"fraise","patate","chocolat"}}; 

     ArrayAdapter<String[][]> adapter = new ArrayAdapter<String[][]>(getListView().getContext(),android.R.layout.simple_list_item_1, from); 

     getListView().setAdapter(adapter); 
    } 
} 

XMLは次のとおりです。配列が単次元である場合には動作しますが、2D配列でどのように行うことができますか?

答えて

0

ArrayAdapterクラスから継承したカスタムArrayAdapterを作成する必要があります。これは、配列ではなく自分のクラスのオブジェクトのArrayListを使って行うのが最も簡単だとわかりますが、両方が可能であると仮定しています。私はこれをArrayList構造体で実装しただけです。

public class CustomArrayAdapter extends ArrayAdapter<String> { 

    private Context context; 
    private ArrayList<YourClass> objects; 
    //or with 2d array this might work: 
    private String [][] strings2d; 

    public CustomArrayAdapter (Context context, ArrayList<YourClass> objects) { 
     super(context, -1, objects); 
     this.context = context; 
     this.objects = objects; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     //Get individual object to be displayed 
     YourClass object = this.objects.get(position); 
     //or with 2d array 
     String[] strings1d = this.strings2d[position]; 

     String string1 = object.strings[1]; //Your class has array of strings as class var 
     String string2 = object.strings[2]; 
     String string3 = object.strings[3]; 

     //Inflate view and get (custom) layout for each item in list 
     LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View customView = layoutInflater.inflate(R.layout.your_custom_list_item_layout, parent, false); 

     //Get textView to display each String 
     TextView textView1 = (TextView) customView.findViewById(R.id.text_field_1); 
     TextView textView2 = (TextView) customView.findViewById(R.id.text_field_2); 
     TextView textView3 = (TextView) customView.findViewById(R.id.text_field_2); 

     //Set text of each textView 
     textView1.setText(string1); 
     textView1.setText(string2); 
     textView1.setText(string3); 

     //Or with 2d arrays 
     textView1.setText(strings1d[0]); 
     textView1.setText(strings1d[1]); 
     textView1.setText(strings1d[2]); 

     return customView; 
    } 

} 

私はそれが実行されるかどうかを確認するために、これをテストしていないと明らかにあなたは、文字列とリストビューの各項目を表示するカスタムレイアウトを保持するカスタムクラスを作成する必要があると思います。

関連する問題