10

TextViewにリストアイテムごとに複数のsがありListViewます。私は自分が信じる適切なメソッドを書くことを学びましたが、そのメソッドを呼び出すためにgetViewどのように使用するのかわかりません。setAdapter

private static String[] project = {"proj1","proj2"};
private static String[] workRequests = {"requirement gathering", "design"};
private static String[] startDate = {"02/21/2012","07/15/2011"};
private static String[] status = {"WIP","DONE"};

ListView mListView;

public class MyDashboardActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mydashboard);

        final LayoutInflater mInflater = LayoutInflater.from(this);
        mListView = (ListView)findViewById(R.id.dashboardList);
        mListView.setAdapter(
                // How do I set the adapter?
                );
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        System.out.println("enters");
        if(convertView == null){
            convertView = LayoutInflater.from(this).inflate(R.layout.mydashboard,null);
        }

        ((TextView) convertView.findViewById(R.id.project)).setText(project[position]);
        ((TextView) convertView.findViewById(R.id.work_request)).setText(workRequests[position]);
        ((TextView) convertView.findViewById(R.id.start_date)).setText(startDate[position]);
        ((TextView) convertView.findViewById(R.id.status)).setText(status[position]);

        return convertView;
    }

これはxmlレイアウトです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- Include Action Bar -->
    <include layout="@layout/actionbar_layout" />

    <ListView
        android:id="@+id/dashboardList"
        style="@style/LeftHeaderText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:background="@drawable/innerdashboard_bg"
        android:textColor="@color/textColor" >

        <TextView android:id="@+id/project" />

        <TextView android:id="@+id/work_request" />

        <TextView android:id="@+id/start_date" />

        <TextView android:id="@+id/status" />

    </ListView>

</LinearLayout>

私はいくつかの方法を試しましたが、どれもうまくいきませんでした。この場合のアダプターの設定方法を教えてください。ありがとう!

4

3 に答える 3

19

独自のアダプタを実装する必要があります。私の方法は、ビューを「表す」オブジェクトも定義することです。

以下に、ニーズに合わせて2つの非常に単純な例を示しTextViewsます。

ビューを表すオブジェクト(ListViewの行):

public class CustomObject {

    private String prop1; 
    private String prop2;

    public CustomObject(String prop1, String prop2) {
        this.prop1 = prop1;
        this.prop2 = prop2;
    }

    public String getProp1() {
        return prop1;
    }

    public String getProp2() {
       return prop2;
    }
}

次に、カスタムアダプタ:

public class CustomAdapter extends BaseAdapter {

   private LayoutInflater inflater;
  private ArrayList<CustomObject> objects;

   private class ViewHolder {
      TextView textView1;
      TextView textView2;
   }

   public CustomAdapter(Context context, ArrayList<CustomObject> objects) {
      inflater = LayoutInflater.from(context);
      this.objects = objects;
   }

   public int getCount() {
      return objects.size();
   }

   public CustomObject getItem(int position) {
      return objects.get(position);
   }

   public long getItemId(int position) {
      return position;
   }

   public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder = null;
      if(convertView == null) {
         holder = new ViewHolder();
         convertView = inflater.inflate(R.layout.your_view_layout, null);
         holder.textView1 = (TextView) convertView.findViewById(R.id.id_textView1);
        holder.textView2 = (TextView) convertView.findViewById(R.id.list_id_textView2);
         convertView.setTag(holder);
      } else {
         holder = (ViewHolder) convertView.getTag();
      }
      holder.textView1.setText(objects.get(position).getprop1());
      holder.textView2.setText(objects.get(position).getprop2());
      return convertView;
   }
}

これで、アクティビティでアダプタを定義および設定できます。

ArrayList<CustomObject> objects = new ArrayList<CustomObject>();
CustomAdapter customAdapter = new CustomAdapter(this, objects);
listView.setAdapter(customAdapter);

これで、オブジェクトリストでCustomObjectを管理するだけで済みます。customAdapter.notifyDataSetChanged()ListViewで変更を再実行する場合は、必ず呼び出すようにしてください。

于 2012-06-19T19:02:47.260 に答える
4

getView() コードは、BaseAdapter またはそのサブクラスの 1 つを拡張するクラスに入る必要があります。

これを行う 1 つの方法は、MyDashboardActivity 内にプライベート クラスを作成することです。以下に簡単な例を示します (追加のコードが必要になります)。表示したいすべてのものを 1 つのリスト項目に関連付けるカスタム オブジェクトも必要になるでしょう。複数の配列の代わりに、追跡している各値のプロパティを持つカスタム型の配列を 1 つ用意します。

もう 1 つ: 4 つの TextView をそれぞれのレイアウト ファイルに入れる必要があります (こちらの list_item.xml を参照してください)。そのアイテム レイアウト ファイルは、カスタム アダプターのコンストラクターを介して接続されます (これを強調するために、以下のコードにコメントを追加しました)。

protected CustomAdapter mAdapter;

public class MyDashboardActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mydashboard);

        final LayoutInflater mInflater = LayoutInflater.from(this);
        mListView = (ListView)findViewById(R.id.dashboardList);

        mAdapter = new CustomAdapter(this, <array to be adapted>);
        mListView.setAdapter(mAdapter);
    }

    private class CustomAdapter extends ArrayAdapter<String> {

        protected Context mContext;
        protected ArrayList<String> mItems;

        public CustomAdapter(Context context, ArrayList<String> items) {
            super(context, R.layout.custom_list_item, items); // Use a custom layout file
            mContext = context;
            mItems = items;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            System.out.println("enters");
            if(convertView == null){
                convertView = LayoutInflater.from(this).inflate(R.layout.mydashboard,null);
            }

            // You'll need to use the mItems array to populate these...
            ((TextView) convertView.findViewById(R.id.project)).setText(project[position]);
            ((TextView) convertView.findViewById(R.id.work_request)).setText(workRequests[position]);
            ((TextView) convertView.findViewById(R.id.start_date)).setText(startDate[position]);
            ((TextView) convertView.findViewById(R.id.status)).setText(status[position]);

            return convertView;
        }
    }
}
于 2012-06-19T18:26:12.270 に答える
2

これを行うにはいくつかの方法があります。2 つのレイアウトを含む私の方法を紹介します。1 つは ListView そのもので、もう 1 つはリスト項目ごとのテキストの表示方法です。

リストセット.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/shipMenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

listitems.xml (ここに画像を入れることもできます。ここでのアイデアはコントロールです)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/makerID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="25dp"/>

</LinearLayout>

上記ではまだアートワークはありませんが、アイコン用の ImageView を追加する予定です (さらに TextView を追加することもできます)。これが私のカスタムアダプタークラスです。

ListAdapter.java

class ListAdapter extends ArrayAdapter <String>
{

    public ListAdapter(Context context, String[] values) {

        super(context, R.layout.listitems, values); //set the layout that contains your views (not the one with the ListView)
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.listitems, parent, false); //same here.

        String text = getItem(position);

        TextView makerID = (TextView) view.findViewById(R.id.makerID);
        makerID.setText(text);

        return view;
    }

}

メイン アクティビティ ファイル セット内

setContentView (R.layout.listset);

これを setContentView() と同じブラケットの下に追加します

ListAdapter adapter = new ListAdapter(this, MyString[]); //place your String array in place of MyString

        ListView lv = (ListView) findViewById(R.id.ListViewID); //the ID you set your ListView to.
        lv.setAdapter(adapter);

編集

私はそれが見えるパーティーに少し遅れていますが、多分これは誰かを助けるでしょう.

于 2015-10-28T23:47:30.640 に答える