如何处理“查看更多”按钮

时间:2018-10-22 08:50:10

标签: android firebase listview

BusinessForSaleActivity

public class BusinessForSaleActivity extends AppCompatActivity {

    DatabaseReference databaseLinks;
    ListView listViewBusinessForSale;
    Button readMore;
    ArrayList<BusinessForSale> list;
    //CardView cardView;

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

        listViewBusinessForSale = (ListView)findViewById(R.id.ListViewBusinessForSale);

        list = new ArrayList<>();

        databaseLinks = FirebaseDatabase.getInstance().getReference("BusinessList");

        databaseLinks.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                list.clear();
                try{
                    for(DataSnapshot businessSnapshot : dataSnapshot.getChildren()){
                        BusinessForSale b = businessSnapshot.getValue(BusinessForSale.class);

                        list.add(b);
                    }
                }
                catch (Exception ex){
                    Log.e("Error Descr:",ex.getMessage());
                }


                BusinessForSaleList adapter=new BusinessForSaleList(BusinessForSaleActivity.this,list);
                listViewBusinessForSale.setAdapter(adapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(BusinessForSaleActivity.this,"Something is wrong",Toast.LENGTH_SHORT).show();
            }
        });


    }
}

BusinessForSaleList ===============(适配器类)===========

public class BusinessForSaleList extends ArrayAdapter<BusinessForSale> {

        private Activity context;
        private List<BusinessForSale> businessForSaleList;

        public BusinessForSaleList(Activity context,List<BusinessForSale> businessForSaleList){
            //super(context,R.layout.list_layout,businessForSaleList);
            super(context,R.layout.list,businessForSaleList);
            this.context=context;
            this.businessForSaleList=businessForSaleList;
        }

        @NonNull
        @Override
        public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LayoutInflater inflater=context.getLayoutInflater();

            View listViewItem=inflater.inflate(R.layout.list,null,true);

            ImageView imgBuiness=(ImageView)listViewItem.findViewById(R.id.imgViewBusiness);
            TextView txtTitle = (TextView)listViewItem.findViewById(R.id.txtViewTitle);
            TextView txtType = (TextView)listViewItem.findViewById(R.id.txtViewType);
            TextView txtDescr = (TextView)listViewItem.findViewById(R.id.txtViewDescr);
            Button btnReadMore = (Button)convertView.findViewById(R.id.buttonReadMore);

            BusinessForSale businessForSale = businessForSaleList.get(position);
            Picasso.get().load(businessForSale.getImage().toString()).into(imgBuiness);
            txtTitle.setText(businessForSale.getBusiness_Title());
            txtType.setText(businessForSale.getBusiness_Type());
            txtDescr.setText(businessForSale.getDescription());


            return listViewItem;
        }
    }

BusinessForSaleActivity.xml ============================================ ==

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BusinessForSaleActivity">

    <ListView
        android:id="@+id/ListViewBusinessForSale"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:footerDividersEnabled="false"
        />

</RelativeLayout>

List.xml ========================================== ====================

[<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e0e0e0">

    <android.support.v7.widget.CardView
        android:id="@+id/cardBusinessForSale"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:cardCornerRadius="8dp"
        app:cardElevation="4dp">
        <LinearLayout
            android:id="@+id/L1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imgViewBusiness"
                android:layout_width="match_parent"
                android:layout_height="160dp"
                android:layout_margin="4dp"
                android:scaleType="centerCrop"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="16dp">
                <TextView
                    android:id="@+id/txtViewTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="TextView"
                    android:layout_marginBottom="8dp"
                    android:textColor="#000"
                    android:textSize="18sp"/>
                <TextView
                    android:id="@+id/txtViewType"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="TextView"
                    android:textColor="#555" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/txtViewDescr"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="TextView"
                    android:maxLines="3"
                    android:textSize="14sp" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp">
                <Button
                    android:id="@+id/buttonReadMore"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:theme="@style/PrimaryFlatButton"
                    android:text="Read More" />

                <Button
                    android:id="@+id/buttonAddToFvrt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:theme="@style/PrimaryFlatButton"
                    android:text="Add to Favorites" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>



</RelativeLayout>][1]

我想在单击“更多”时重定向到新的活动。我正在使用list.xml布局将Firebase数据提取到listview中。

1 个答案:

答案 0 :(得分:-1)

当您使用列表并且需要处理对项目或其中包含的元素的单击时,常见的方法是在getView()方法或onBind()方法中设置侦听器,具体取决于您使用的专家正在使用。

在这种情况下,在您的“ getView()”方法中,在引用您的按钮之后,您需要在其上设置一个侦听器,以检索包含它的项的位置并打开相对活动(或唯一活动设置)正确的值)。您可以通过检索其父级的父级位置来做到这一点,但这听起来很复杂。

最佳和通用的方法是通过以下方式为按钮分配一个标签,该标签是通过getView()方法分配给其父对象的位置:

 @NonNull
 @Override
 public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View listViewItem=inflater.inflate(R.layout.list,null,true);
    ...
    Button btnReadMore = (Button)convertView.findViewById(R.id.buttonReadMore);
    btnReadMore.setTag(position + "");
}

此后,每个按钮都包含该项目在列表中的位置。现在,将监听器分配给按钮并检索正确的位置:

btnReadMore.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view,....){
    Button btnReadMore = (Button) view; //This cast is unless, it's here only for more understanding
    String positionString = btnReadMore.getTag();
    int position = Integer.parseInt(positionString);
    BusinessForSale correctItem = BusinessForSaleList.get(position);
    //here start activity you need.
}});

希望这项帮助! :D