按下它时更改ListView项的布局

时间:2015-09-08 10:30:00

标签: android listview

在为我和我的室友写一个应用来计算我们的食物付款时,我遇到了这样的问题:

添加新付款时,您: 1)从RadioGroup中选择名称 2)给予价值 3)给出描述(或保留默认' opis')

然后按下按钮" DODAJ"它在ListView中显示为具有特定格式的新项目。现在问题开始了:

我想这样做,当我点击此列表中的一个项目时,我会略微改变这个特定项目的格式(在NowaWplata类中,我有额外的时间显示),这是我在另一个.xml布局文件中得到的,当再次按下它时,它应该回到正常的状态。布局。 我尝试了两种不同的方法,一种是在我的代码底部注释(我从这个主题Change layout of selected list item in Android尝试过),另一种是覆盖onItemClick方法 - 它们都没有工作

编译器说错误是:

java.lang.NullPointerException
            at com.example.lukasz.dom.MainActivity.onItemClick

在这行代码中:

        relativeLayoutInflate.addView(child);

在主要活动中(onClick仅适用于' lukasz',同样适用于' marcelina'' karolina'):

    @Override
    public void onClick(View v) {

        if (v == addNewPaymentButton) {
            //checking which button from RadioGroup is checked and adding new payment to certain person
            int checkedRadioButtonId = members.getCheckedRadioButtonId();
            switch (checkedRadioButtonId) {

                //if Lukasz button is pressed, do:
                case R.id.lukaszRadioButton:
                    if (lukaszRadioButton.isChecked()) {

                        //add value from addNewPaymentButton to tempLukasz
                        try {
                            tempLukasz += valueOf(newPaymentValue.getText().toString());
                        } catch (NumberFormatException e) {
                        }

                        //seting new value to money spent by lukasz
                        String sumaLukasza = getString(R.string.money_spent_by_lukasz);
                        sumaLukasza = String.format(sumaLukasza, tempLukasz);
                        moneySpentByLukasz.setText(sumaLukasza);

                        //adding new payment to list of all payments with flag 'int == 1' to set color to RED
                        NowaWplata newPayment = new NowaWplata(lukaszRadioButton.getText().toString(), newPaymentValue.getText().toString(), descriptionOfNewPayment.getText().toString(), 1);
                        NowaWplata.setWplaty(newPayment);
                        listView.setAdapter(new NewPaymentAdapter(this, R.layout.new_list_item_layout, NowaWplata.getWplaty()));

                    }
                    break;
}


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        TextView hourDate = (TextView) view.findViewById(R.id.hoursDateFieldInSelectedListItem);
        TextView dayDate = (TextView) view.findViewById(R.id.daysDateFieldInSelectedListItem);
        TextView name = (TextView) view.findViewById(R.id.nameFieldInSelectedListItem);
        TextView value = (TextView) view.findViewById(R.id.valueFieldInSelectedListItem);
        TextView description = (TextView) view.findViewById(R.id.descriptionFieldInSelectedListItem);

        RelativeLayout relativeLayoutInflate = (RelativeLayout) view.findViewById(R.id.layoutOfSelectedListItem);
        NowaWplata newPayment = (NowaWplata) listView.getItemAtPosition(position);
        View child = getLayoutInflater().inflate(R.layout.selected_list_item, null);
        relativeLayoutInflate.addView(child);


        hourDate.setText("[" + newPayment.getHourDate() + "]");
        dayDate.setText(newPayment.getDayDate());
        name.setText(newPayment.getOsoba());
        value.setText(newPayment.getWplata() + "zł");
        description.setText(newPayment.getOpis());


    }
}

//custom adapter class
 class NewPaymentAdapter extends ArrayAdapter<NowaWplata> {

    public LayoutInflater layoutInflater;

    //custom adapter's constructor with values
    public NewPaymentAdapter(Context context, int textViewResourceId, List<NowaWplata> wplaty) {
        super(context, textViewResourceId, wplaty);
        layoutInflater = LayoutInflater.from(context);
    }

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

        View view = convertView;
        NowaWplata newPayment = getItem(position);
        Holder holder = null;

//            private int position;
//            public void selectedItem(int position)
//            {
//                this.position = position;
//            }

        //if there are no items in list - add 1st item
        if (view == null) {
            view = layoutInflater.inflate(R.layout.new_list_item_layout, null);

            TextView name = (TextView) view.findViewById(R.id.osobaWplata);
            TextView value = (TextView) view.findViewById(R.id.kwotaWplata);
            TextView description = (TextView) view.findViewById(R.id.opisWplata);

            holder = new Holder(name, value, description);
            view.setTag(holder);
        }
        // if there are items in list get holder tag
        else {
            holder = (Holder) view.getTag();
        }

        //setting text to a new list item
        holder.osoba.setText("[" + newPayment.getDate() + "]  " + newPayment.getOsoba());
        holder.kwota.setText(newPayment.getWplata() + "zł");
        holder.opis.setText(newPayment.getOpis());

        //setting different color to Lukasz/Marcelina/Karolina text
        if (newPayment.getFlag() == 1) {
            holder.osoba.setTextColor(Color.RED);
        } else if (newPayment.getFlag() == 2) {
            holder.osoba.setTextColor(Color.BLUE);
        } else if (newPayment.getFlag() == 3) {
            holder.osoba.setTextColor(Color.GREEN);
        }
        //setting color to payment's value
        holder.kwota.setTextColor(Color.BLACK);

//            if (this.position == position) {
//                View view2;
//                view2 = layoutInflater.inflate(R.layout.selected_list_item, null);
//
//                TextView hourDate = (TextView) view2.findViewById(R.id.hoursDateFieldInSelectedListItem);
//                TextView dayDate = (TextView) view2.findViewById(R.id.daysDateFieldInSelectedListItem);
//                TextView name = (TextView) view2.findViewById(R.id.nameFieldInSelectedListItem);
//                TextView value = (TextView) view2.findViewById(R.id.valueFieldInSelectedListItem);
//                TextView description = (TextView) view2.findViewById(R.id.descriptionFieldInSelectedListItem);
//
//                hourDate.setText("[" + newPayment.getHourDate() + "]");
//                dayDate.setText(newPayment.getDayDate());
//                name.setText(newPayment.getOsoba());
//                value.setText(newPayment.getWplata() + "zł");
//                description.setText(newPayment.getOpis());
//
//                return view2;
//            }

        return view;
    }
}

这是ListView的主要XML部分

   <ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@android:id/list"
    android:layout_below="@+id/tableLayout"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_above="@+id/newPaymentValue"
    android:layout_alignRight="@+id/tableLayout"
    android:layout_alignEnd="@+id/tableLayout"
    android:choiceMode="none"
    android:clickable="false"
    android:padding="20dp"
    android:transcriptMode="disabled"/>

这是列表

中新项目的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layoutOfSelectedListItem">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/hoursDateFieldInSelectedListItem"
    android:layout_gravity="center_horizontal"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="18dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/daysDateFieldInSelectedListItem"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textSize="18dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/nameFieldInSelectedListItem"
    android:layout_below="@+id/hoursDateFieldInSelectedListItem"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/daysDateFieldInSelectedListItem"
    android:layout_toStartOf="@+id/daysDateFieldInSelectedListItem"
    android:textSize="18dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/valueFieldInSelectedListItem"
    android:layout_below="@+id/daysDateFieldInSelectedListItem"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textSize="22dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/descriptionFieldInSelectedListItem"
    android:layout_below="@+id/nameFieldInSelectedListItem"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="20dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textSize="14dp" />

1 个答案:

答案 0 :(得分:0)

我通过添加额外的布局来做到这一点。

列表项:

<--Your listItem view--!>
<RelativeLayout>
    <--Your extra View--!>
    <LinearLayout
    android:tag="0"
    android:visibility="gone">
    </LinearLayout>
</RelativeLayout>

OnItemClickListener中,将额外布局设置为可见,并相应地设置标记以保存额外视图的状态。