setAdapter中的getView不起作用

时间:2015-06-08 09:29:54

标签: android android-layout listview android-linearlayout

我在询问之前搜索网,但所有线程都不适合我。 我有一个自定义线性布局,其中有一个列表视图。我希望​​通过自定义适配器填充该列表视图,但getView方法不起作用。

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

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/dropup" />

    <com.samanpr.samanak.ui.widgets.PersianTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/please_select"
        android:textColor="#c6c6c6" >
    </com.samanpr.samanak.ui.widgets.PersianTextView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#c6c6c6" 
        android:layout_marginTop="5dp"
        android:layout_alignParentBottom="true">
    </LinearLayout>
</RelativeLayout>

<ListView
    android:id="@+id/favorites_list_box"
    android:layout_width="fill_parent"
    android:layout_height="0.0dip"
    android:layout_weight="1"
    android:layout_marginTop="2.0dip"
    android:cacheColorHint="@color/transparent"
    android:divider="@null"
    android:dividerHeight="0.0dip"
    android:drawSelectorOnTop="false" />

<TextView
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

和custom linearlayout类中的自定义适配器:

public class FavoritesListView extends LinearLayout implements OnClickListener {

List<FavoriteAccountsDTO> dtoList = new ArrayList<FavoriteAccountsDTO>();
private String currentAccount;
private int type;
public ListView lv;
public View parentView;

public FavoritesListView(Context context) {
    super(context);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    parentView = inflater.inflate(R.layout.favorites_list, this);
    init();
}

public FavoritesListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    parentView = inflater.inflate(R.layout.favorites_list, this);
    init();
}

public FavoritesListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    parentView = inflater.inflate(R.layout.favorites_list, this);
    init();
}

private void init() {
    lv = (ListView) parentView.findViewById(R.id.favorites_list_box);
}

public void displayContents(Context context) {
    try {
        dtoList = new DatabaseHelper(SamanakApplication.getInstance().getApplicationContext()).getFavoritesDao().queryBuilder().where().eq("type", getType()).and()
                .ne("account", getCurrentAccount()).query();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    ListFavoritesAdapter adapter = new ListFavoritesAdapter(context, 0, dtoList);
    lv.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}

public class ListFavoritesAdapter extends ArrayAdapter<FavoriteAccountsDTO> {
    List<FavoriteAccountsDTO> accounts;
    Context context;
    public ListFavoritesAdapter(Context context, int resource, List<FavoriteAccountsDTO> array) {
        super(context, resource, array);
        this.accounts = array;
        this.context = context;
    }

    @Override
    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View localView = inflater.inflate(R.layout.favorites_list_item, null);
        TextView itemAcc = (TextView) localView.findViewById(R.id.favorites_list_item_account);
        itemAcc.setText(this.accounts.get(paramInt).getAccount());
        TextView itemName = (TextView) localView.findViewById(R.id.favorites_list_item_name);
        itemName.setText(this.accounts.get(paramInt).getName());
        localView.setTag(this.accounts.get(paramInt).getAccount());
        localView.setOnClickListener(FavoritesListView.this);
        return localView;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return dtoList.size();
    }

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

public int getType() {
    return type;
}

public void setType(int type) {
    this.type = type;
}

public String getCurrentAccount() {
    return currentAccount;
}

public void setCurrentAccount(String currentAccount) {
    this.currentAccount = currentAccount;
}

@Override
public void onClick(View view) {
    String index = String.valueOf(view.getTag()).toString();
}

问题是什么?

0 个答案:

没有答案