2017-02-03 11 views
0

私はAndroidアプリケーションを開発しています。私はカスタムListViewを実装する必要があります。カスタムListViewの項目にアクセスする方法

Iは、リストビューの宣言を含むメインレイアウトを有する:

<ListView 
    android:id="@+id/imageList" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.5"> 
</ListView> 

カスタム列のレイアウトを:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/list_row_selector" 
    android:padding="8dp"> 

    <LinearLayout 
     android:id="@+id/titleLL" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/title" 
      android:textColor="#FF0000" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/photoList" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/titleLL" 
     android:orientation="horizontal" 
     android:layout_alignParentStart="true"> 

     <ImageView 
      android:id="@+id/takePhoto" 
      android:layout_width="wrap_content" 
      android:layout_height="60dp" 
      android:scaleType="center" 
      android:src="@drawable/ico_camera" 
      android:layout_marginRight="40dp" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentStart="true" /> 

     <ImageView 
      android:id="@+id/thumbnail0" 
      android:layout_width="wrap_content" 
      android:layout_height="60dp" 
      android:scaleType="center" 
      android:layout_alignParentLeft="true" 
      android:layout_marginRight="40dp" /> 

     <ImageView 
      android:id="@+id/thumbnail1" 
      android:layout_width="wrap_content" 
      android:layout_height="60dp" 
      android:scaleType="center" 
      android:layout_alignParentLeft="true" 
      android:layout_marginRight="40dp" /> 
    </LinearLayout> 

    <ImageView 
     android:id="@+id/deleteall" 
     android:layout_width="wrap_content" 
     android:layout_height="60dp" 
     android:scaleType="centerCrop" 
     android:src="@drawable/ico_menu_delete_photo" 
     android:layout_below="@+id/titleLL" 
     android:layout_alignParentEnd="true" /> 
    </RelativeLayout> 

これは、カスタム行のクラスである。

public class CustomRow { 
    private Integer id; 
    private String title; 

    public CustomRow() {} 

    public CustomRow(String ti, Integer id) { 
     this.id = id; 
     this.title = ti; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String ti) { 
     this.title = ti; 
    } 
} 

and finally the code of the CustomList: 


public class CustomList extends BaseAdapter { 
    private Activity activity; 
    private LayoutInflater inflater; 
    private List<CustomRow> ListItem; 

    public CustomList(Activity activity, List<CustomRow> listPhotoItems) { 
     this.activity = activity; 
     this.ListItem = listPhotoItems; 
    } 

    @Override 
    public int getCount() { 
     return ListItem.size(); 
    } 

    @Override 
    public Object getItem(int location) { 
     return ListItem.get(location); 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (inflater == null) 
      inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) 
      convertView = inflater.inflate(R.layout.custom_list, null); 

     ImageView takePhoto = (ImageView) convertView.findViewById(R.id.takePhoto); 

     TextView title = (TextView) convertView.findViewById(R.id.title); 
     CustomRow m = ListItem.get(position); 
     takePhoto.setTag(m.getId()); 

     title.setText(m.getTitle()); 
     return convertView; 
    } 
} 

質問は簡単ですが、作成前にリストビューのアイテムにアクセスする方法は簡単ですリストビューのリスナーではなくomRow?

例えば、私はImageViewのをthumbail0の画像を変更したい...

私はdiffrerent方法何も作業を試してみました。例えば

lv.findViewById(R.id.thumbnail0)。

それとも

ビューのvi = inflater.inflate(R.layout.custom_list、NULL); vi.findViewById(R.id.thumbnail0);

私はエラーoccoursこのコードを実装しようとすると、私はthisソリューションを使用することはできません。

のjava.lang.NullPointerException:仮想メソッド 「android.view.View android.viewを起動しようとします。 View.findViewById(int型)」ヌル オブジェクト参照

+0

アダプタの 'getView()'メソッドで行います。 –

答えて

1

にあなたがadaptergetView()方法で作成する前に、任意の項目にアクセスすることができます。このメソッドは各項目をバインドします。

+0

アダプターgetViewでこれを行うことはできません。 – Max

+1

@Maxなぜ、あなたはそれを正確に行うことができませんか? 'ListView'の行はそこに展開される前には存在しないので、それより早くアクセスすることはできません。 –

+0

'行リストのいくつかの要素を修正する必要がある(ImageViewの画像を変更するなど) – Max

関連する問題