ListView中的ListView只显示一个项目

时间:2016-08-25 11:55:11

标签: android listview

我正在开发一个债务管理器应用程序,您可以在其中添加您欠他们或他们欠您的人和金额。人员应显示在ListView中,并在人员条目中的ListView中显示债务。目前我正在添加两个人,每两个债务条目作为虚拟数据。两个人都显示在外部ListView中,但每个只显示一个债务项目。

有什么想法吗?

我已经阅读了一些与此主题相关的问题,但我找不到解决方案。

这些是相关文件:

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main"
    tools:context="de.cheereeo.imbroke_debtmanager.MainActivity">

    <ListView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/personsView"
            android:layout_alignParentTop="true" android:layout_alignParentStart="true"/>
</RelativeLayout>

person_item.xml:

<?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="wrap_content">
        <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginLeft="5dp"
                android:id="@+id/relativeLayout2">
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0.00"
                    android:id="@+id/amountView" android:layout_gravity="right"
                    android:layout_marginRight="5dp" android:layout_alignParentTop="true"
                    android:textStyle="bold" android:textColor="#000000"/>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="from"
                    android:id="@+id/forView" android:layout_marginRight="5dp"
                    android:layout_alignParentTop="true" android:layout_toEndOf="@+id/amountView" android:textStyle="bold"
                    android:textColor="#000000"/>
            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Jon Doe"
                    android:id="@+id/nameView"
                    android:layout_marginRight="5dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_alignBottom="@+id/forView" android:layout_toEndOf="@+id/forView"
                    android:textStyle="bold" android:textColor="#000000"/>
        </RelativeLayout>
    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentRight="true"
            android:id="@+id/relativeLayout">
        <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/addButton" android:layout_alignParentRight="false"
                android:src="@android:drawable/ic_input_add" android:background="#ffff"
        />
        <ImageButton
                android:layout_width="24dp"
                android:layout_height="wrap_content"
                android:id="@+id/withdrawButton" android:layout_alignParentRight="false"
                android:src="@android:drawable/button_onoff_indicator_on"
                android:layout_toEndOf="@+id/addButton" android:layout_alignParentEnd="false"/>
    </RelativeLayout>
    <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/debtsView" android:layout_below="@+id/relativeLayout"
            android:layout_alignStart="@+id/relativeLayout2" android:stackFromBottom="false"/>

</RelativeLayout>

debt_item.xml:

<?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">
    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginLeft="5dp"
            android:id="@+id/relativeLayout2">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0.00"
                android:id="@+id/amountView" android:layout_gravity="right"
                android:layout_marginRight="5dp"
                android:textStyle="bold" android:textColor="#000000"
        />
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="for"
                android:id="@+id/forView" android:layout_marginRight="5dp"
                android:layout_alignParentTop="true" android:textStyle="bold"
                android:textColor="#000000" android:layout_toEndOf="@+id/amountView"/>
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Food"
                  android:id="@+id/subjectView" android:layout_marginRight="5dp" android:layout_alignParentTop="true"
                  android:textStyle="bold" android:textColor="#000000" android:layout_toEndOf="@+id/forView"/>
    </RelativeLayout>
    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentRight="true"
            android:id="@+id/relativeLayout">
        <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/addButton" android:layout_alignParentRight="false"
                android:src="@android:drawable/ic_menu_edit" android:background="#fff"/>
    </RelativeLayout>
</RelativeLayout>

PersonAdapter.java:

@EBean
public class PersonAdapter extends BaseAdapter {

    List<Person> persons;

    @RootContext
    Context context;

    @AfterInject
    void initAdapter() {
        persons = new ArrayList<>();
        persons.add(new Person("Jon Doe"));
        persons.add(new Person("Peter Pan"));
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PersonItemView personItemView;
        if (convertView == null) {
            personItemView = PersonItemView_.build(context);
        } else {
            personItemView = (PersonItemView) convertView;
        }

        personItemView.bind(getItem(position));

        return personItemView;
    }

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

    @Override
    public Person getItem(int position) {
        return persons.get(position);
    }

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

DebtAdapter.java:

@EBean
public class DebtAdapter extends BaseAdapter {

    List<Debt> debts;

    Person other;

    @RootContext
    Context context;

    @AfterInject
    void initAdapter() {
        debts = new ArrayList<>();

        debts.add(new Debt("Pizza", 5.9, this.other, Debt.Direction.BORROWED_FROM));
        debts.add(new Debt("Bus ticket", 2.3, this.other, Debt.Direction.LENT_TO));
    }

    public void setOther(Person other) {
        this.other = other;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DebtItemView debtItemView;
        if (convertView == null) {
            debtItemView = DebtItemView_.build(context);
        } else {
            debtItemView = (DebtItemView) convertView;
        }

        debtItemView.bind(getItem(position));

        return debtItemView;
    }

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

    @Override
    public Debt getItem(int position) {
        return debts.get(position);
    }

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

PersonItemView.java:

@EViewGroup(R.layout.person_item)
public class PersonItemView extends LinearLayout {
    @ViewById
    TextView nameView;

    @ViewById
    ListView debtsView;

    @Bean
    DebtAdapter adapter;

    public PersonItemView(Context context) {
        super(context);
    }

    public void bind(Person person) {
        adapter.setOther(person);
        nameView.setText(person.name);
    }

    @AfterViews
    public void afterViews(){
        debtsView.setAdapter(adapter);
    }
}

DebtItemView:

@EViewGroup(R.layout.debt_item)
public class DebtItemView extends LinearLayout {
    @ViewById
    TextView amountView;
    @ViewById
    TextView subjectView;

    public DebtItemView(Context context) {
        super(context);
    }

    public void bind(Debt debt) {
        amountView.setText(String.valueOf(debt.amount));
        subjectView.setText(debt.subject);
    }
}

1 个答案:

答案 0 :(得分:0)

哟应该尝试可扩展ListView here是可扩展列表视图的示例。

在群组中添加人名及其在该群组的子女中的债务。