Listview绑定到具有适配器的cardview,并且同一cardview具有另一个Listview绑定到具有适配器的另一个cardview

时间:2019-03-08 07:37:38

标签: java android listview android-adapter android-cardview

这是我在这里的帖子。

从4天前开始我就一直陷入困境:(

我想创建一个结合2个列表视图和2个不同适配器的活动。

我正在尝试让一个应用程序显示基于其他数据的数据。例如

主要活动必须显示“无限”的名片视图数量,该数量基于列出一个图像和TextLabel 1的第一个适配器

并且必须同时在列表视图中显示数据(黄色框)。黄色框会为另一种布局显示绿色框,并在其中显示数据。Ups我忘记了每个2个文本标签,因为绿色框也是cardviews。

分开的展位可以正常工作,但我需要做一些像Inflate一样的工作。 我希望所附的“绘画”图片足够Here is the image link

     <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_cards"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PrezentareCabinetActivity">

    <ListView
        android:id="@+id/list_cards_preturi"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginRight="0dp"
        android:divider="@android:color/transparent" />

</RelativeLayout>  

card_item_lista_preturi.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/lineartest"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:orientation="vertical"
    android:paddingLeft="@dimen/cardview_default_elevation"
    android:paddingTop="@dimen/cardview_default_elevation"
    android:paddingRight="@dimen/cardview_default_elevation"
    android:paddingBottom="@dimen/cardview_default_elevation">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_dark"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:visibility="visible">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="115dp"
                android:layout_height="97dp"
                app:srcCompat="@drawable/logocab" />

            <TextView
                android:id="@+id/serviciu_titlu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginStart="7dp"
                android:layout_marginLeft="7dp"
                android:layout_marginTop="30dp"
                android:layout_toEndOf="@+id/imageView"
                android:layout_toRightOf="@+id/imageView"
                android:textAppearance="@style/TextAppearance.AppCompat.Display1"
                android:textSize="26sp" />

            <ListView
                android:id="@+id/list_cards_preturi_second_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageView"
                android:layout_marginTop="8dp"
                android:divider="@android:color/transparent" />


        </RelativeLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

preturi_cardview_second.xml

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:orientation="vertical"
    android:paddingBottom="@dimen/cardview_default_elevation"
    android:paddingLeft="@dimen/cardview_default_elevation"
    android:paddingRight="@dimen/cardview_default_elevation"
    android:paddingTop="@dimen/cardview_default_elevation">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:visibility="visible"
        app:cardBackgroundColor="@color/design_default_color_primary">


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

            <LinearLayout
                android:id="@+id/hgfhgf"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:background="@android:color/holo_blue_dark"
                android:orientation="horizontal">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"

                    android:background="@drawable/borders">

                    <TextView
                        android:id="@+id/serviciu_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"

                        android:textSize="20sp" />
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/hgfhgf"
                android:layout_marginTop="1dp"
                android:background="@android:color/holo_blue_dark"
                android:orientation="horizontal"
                android:visibility="visible">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/borders">

                    <TextView
                        android:id="@+id/pret_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"

                        android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true"
                        android:layout_marginStart="0dp"
                        android:layout_marginLeft="0dp"
                        android:layout_marginTop="0dp"
                        android:gravity="center"
                        android:textSize="20sp" />
                </RelativeLayout>
            </LinearLayout>
        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

PreturiAdapter.java

       package com.example.bludental2;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class PreturiAdapter extends ArrayAdapter<PreturiModel> {


    public PreturiAdapter(Context context) {
        super(context, R.layout.card_item_lista_preturi);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.card_item_lista_preturi, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        PreturiModel model = getItem(position);
        holder.titluserv.setText(model.getTitluservid());
        return convertView;
    }


    static class ViewHolder {
        TextView titluserv;

        ViewHolder(View view) {


            titluserv = (TextView) view.findViewById(R.id.serviciu_titlu);

        }

    }


}

PreturiModel.java

  package com.example.bludental2;


public class PreturiModel {
    private int titluservid;


    // private int serviciutextid;
    //   private int prettextid;


    public int getTitluservid() {
        return titluservid;
    }

    //  public int getServiciutextid() {
    //      return serviciutextid;
    //   }

    ///   public int getPrettextid() {
    //      return prettextid;
    //  }

    public PreturiModel(int titluservid) {


        this.titluservid = titluservid;
        //      this.serviciutextid = serviciutextid;
        //   this. prettextid= prettextid;


    }


}

ListapreturiActivity.java

  package com.example.bludental2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class ListapreturiActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //    setContentView(R.layout.card_item_lista_preturi);

        setContentView(R.layout.activity_listapreturi);


        ListView lv1 = (ListView) this.findViewById(R.id.list_cards_preturi);
        ArrayAdapter adapter1 = new ArrayAdapter<String>(
                this, R.layout.card_item_lista_preturi);


        ArrayAdapter adapter2 = new ArrayAdapter<String>(
                this, R.layout.preturi_cardview_second);
        lv1.setAdapter(adapter1);

        adapter1.addAll(new PreturiModel(R.string.titlupreturi1));
        adapter1.addAll(new PreturiModel(R.string.titlupreturi1));
        adapter1.addAll(new PreturiModel(R.string.titlupreturi1));
        adapter1.addAll(new PreturiModel(R.string.titlupreturi1));
        adapter1.addAll(new PreturiModel(R.string.titlupreturi1));
        adapter1.addAll(new PreturiModel(R.string.titlupreturi1));
        adapter1.addAll(new PreturiModel(R.string.titlupreturi1));
        adapter1.addAll(new PreturiModel(R.string.titlupreturi1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));


    }


}

PreturiModelSecond.java

 package com.example.bludental2;

public class PreturiModelSecond {


    private int serviciutextid;
    private int prettextid;


    public int getServiciutextid() {
        return serviciutextid;
    }

    public int getPrettextid() {
        return prettextid;
    }

    public PreturiModelSecond(int serviciutextid, int prettextid) {


        this.serviciutextid = serviciutextid;
        this.prettextid = prettextid;


    }


}

PreturiAdapterSecond.java

 package com.example.bludental2;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;



public class PreturiAdapterSecond extends ArrayAdapter<PreturiModelSecond> {
    public PreturiAdapterSecond(Context context) {
        super(context, R.layout.preturi_cardview_second);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = inflater.inflate(R.layout.preturi_cardview_second, parent, false);
            holder = new ViewHolder(convertView);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        PreturiModelSecond model = getItem(position);



        holder.serviciutext.setText(model.getServiciutextid());
        holder.prettext.setText(model.getPrettextid());



        return convertView;
    }

    static class ViewHolder {



        TextView serviciutext;
        TextView prettext;






        ViewHolder(View view) {


            serviciutext = (TextView) view.findViewById(R.id.serviciu_text);
            prettext = (TextView) view.findViewById(R.id.pret_text);








        }
    }
}

ActivityPreturiSecond.java

   package com.example.bludental2;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class ActivityPreturiSecond extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.card_item_lista_preturi);

        ListView lv2Cards = (ListView) findViewById(R.id.list_cards_preturi_second_list);
        PreturiAdapterSecond adapter2 = new PreturiAdapterSecond(this);

        lv2Cards.setAdapter(adapter2);


        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));
        adapter2.addAll(new PreturiModelSecond(R.string.serviciu1, R.string.pret1));


        //  adapter.addAll(new PreturiModel( R.string.serviciu1,  R.string.pret1));
        //   adapter.addAll(new PreturiModel(  R.string.serviciu2,  R.string.pret2));
        //  adapter.addAll(new PreturiModel(R.string.titlupreturi1,  R.string.serviciu3,  R.string.pret3));
        //  adapter.addAll(new PreturiModel(R.string.titlupreturi1,  R.string.serviciu4,  R.string.pret4));
        //  adapter.addAll(new PreturiModel(R.string.titlupreturi1,  R.string.serviciu5,  R.string.pret5));


        //  adapter2.addAll(new PreturiModelSecond(R.string.serviciu2, R.string.pret2));


    }


}

我希望这能奏效,谢谢。

0 个答案:

没有答案