使用CardView BaseAdapter OnItemClick自定义ListView

时间:2018-09-09 08:04:00

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

我真的需要帮助和一些解决方案

我尝试使用ListViewCardView来创建自定义BaseAdapter,我有5个项目列表。每个列表都包含图像,标题和描述。 我遇到了一些问题,我不知道如何使用单击每个ListView进入不同的新活动

这是baseAdapter Java代码

package com.projectskripsi.wisatajogja.adapter;

import android.content.Context;
import android.media.Image;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.projectskripsi.wisatajogja.R;
import com.projectskripsi.wisatajogja.model.Model;

import java.util.ArrayList;



public class WisataAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Model> models;

    public WisataAdapter(Context context, ArrayList<Model> models) {
        this.context = context;
        this.models = models;
    }

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

    @Override
    public Object getItem(int position) {
        return models.get(position);
    }

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

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

            convertView = View.inflate(context , R.layout.list_item1 ,null);
        }
        ImageView images = (ImageView) convertView.findViewById(R.id.wisataimage);
        TextView title = (TextView) convertView.findViewById(R.id.namawisataTV);
        TextView about = (TextView) convertView.findViewById(R.id.wisatadesTV);

        Model model = models.get(position);

        images.setImageResource(model.getWisataimage());
        title.setText(model.getWisataTitle());
        about.setText(model.getAboutTitle());

        return convertView;
    }
}

和此模型java

package com.projectskripsi.wisatajogja.model;




public class Model {

    private int wisataimage;
    private String wisataTitle;
    private String aboutTitle;

    public Model(int wisataimage, String wisataTitle, String aboutTitle) {
        this.wisataimage = wisataimage;
        this.wisataTitle = wisataTitle;
        this.aboutTitle = aboutTitle;
    }

    public int getWisataimage() {
        return wisataimage;
    }

    public void setWisataimage(int wisataimage) {
        this.wisataimage = wisataimage;
    }

    public String getWisataTitle() {
        return wisataTitle;
    }

    public void setWisataTitle(String wisataTitle) {
        this.wisataTitle = wisataTitle;
    }

    public String getAboutTitle() {
        return aboutTitle;
    }

    public void setAboutTitle(String aboutTitle) {
        this.aboutTitle = aboutTitle;
    }
}

以及此MainActivity Java

package com.projectskripsi.wisatajogja;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.projectskripsi.wisatajogja.adapter.WisataAdapter;
import com.projectskripsi.wisatajogja.model.ListDetails1;
import com.projectskripsi.wisatajogja.model.Model;

import java.util.ArrayList;

public class GunungKidul extends AppCompatActivity {

    private ListView listView;
    private ArrayList<Model> models;
    private WisataAdapter wisataAdapter;


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

        listView = (ListView) findViewById(R.id.listwisata1);
        models = ListDetails1.getList();

        wisataAdapter = new WisataAdapter(GunungKidul.this,models);
        listView.setAdapter(wisataAdapter);

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

                Model model = models.get(position);

                Intent intent = new Intent(GunungKidul.this, Pantai_Kesirat.class);
                intent.putExtra("wisataTittle", listView.getItemAtPosition(position).toString());
                startActivity(intent);




            }
        });

    }
}

谢谢您的回答。我真的需要帮助人员。

3 个答案:

答案 0 :(得分:1)

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Model model= (Model) wisataAdapter.getItem(position);

                if(position==0){
                   Intent intent = new Intent(GunungKidul.this, Pantai_Kesirat.class);
                   //To pass whole model data with intent:
                   intent.putExtra("MyClassKey", model);
                   startActivity(intent);
                }
                else if{
                    Intent intent = new Intent(GunungKidul.this,  Goa_Pindul.class);
                    //To pass whole model data with intent:
                    intent.putExtra("MyClassKey", model);
                    startActivity(intent);
                }
        else if{
                    Intent intent = new Intent(GunungKidul.this, Pantai_Baron.class);
                    //To pass whole model data with intent:
                    intent.putExtra("MyClassKey", model);
                    startActivity(intent);
                }
        else if{
                    Intent intent = new Intent(GunungKidul.this, Goa_Jomblang.class);
                    //To pass whole model data with intent:
                    intent.putExtra("MyClassKey", model);
                    startActivity(intent);
                }
        else if{
                    Intent intent = new Intent(GunungKidul.this, AirTerjun.class);
                    //To pass whole model data with intent:
                    intent.putExtra("MyClassKey", model);
                    startActivity(intent);
                }



                            }
                        })

;

使用此代码,但请始终记住您的卡位置或索引值以及单击cardView时应打开的活动,就像您单击“第一个cardView打开Pantai_Kesirat活动”一样

答案 1 :(得分:0)

像这样在模型类中使用Serializable:

Camera.main

然后,您的第一个活动将数据传递给listview项目click事件上的另一个活动,如下所示:

package com.projectskripsi.wisatajogja.model;

    public class Model implements Serializable {

        private int wisataimage;
        private String wisataTitle;
        private String aboutTitle;

        public Model(int wisataimage, String wisataTitle, String aboutTitle) {
            this.wisataimage = wisataimage;
            this.wisataTitle = wisataTitle;
            this.aboutTitle = aboutTitle;
        }

        public int getWisataimage() {
            return wisataimage;
        }

        public void setWisataimage(int wisataimage) {
            this.wisataimage = wisataimage;
        }

        public String getWisataTitle() {
            return wisataTitle;
        }

        public void setWisataTitle(String wisataTitle) {
            this.wisataTitle = wisataTitle;
        }

        public String getAboutTitle() {
            return aboutTitle;
        }

        public void setAboutTitle(String aboutTitle) {
            this.aboutTitle = aboutTitle;
        }
    }

现在在Pantai_Kesirat类中,您创建了一个模型类Object,例如:

public class GunungKidul extends AppCompatActivity {

    private ListView listView;
    private ArrayList<Model> models;
    private WisataAdapter wisataAdapter;


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

        listView = (ListView) findViewById(R.id.listwisata1);
        models = ListDetails1.getList();

        wisataAdapter = new WisataAdapter(GunungKidul.this,models);
        listView.setAdapter(wisataAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              Intent intent = new Intent(GunungKidul.this, Pantai_Kesirat.class);
              //To pass whole model data with intent:
             intent.putExtra("MyClassKey", models.get(position));
                startActivity(intent);




            }
        });

    }
}

然后在第二个Activity的Model model; 方法中,从意图中接收数据,例如:

onCreate()

然后借助模型类的getter和setter方法在需要的地方使用模型类的数据

答案 2 :(得分:0)

欢迎使用StackOverflow:)

因此,对于您定义的问题,我有一个简单的解决方案,请检查它是否对您有用。

  1. 您实现一个单击侦听器,在MainActivity(具有CardViews列表的那个)中说“ CardClickListener”。
  2. 在启动ListView的适配器时,
  3. 通过 this
  4. 在适配器中,每次单击CardItem时,都会调用此clickListener并将与之关联的数据对象传递给MainActivity。
  5. 在MainActivity中解析数据对象,然后基于该数据对象进入各自的活动。

尽管此解决方案肯定可以工作,但我不认为这是解决您问题的最佳解决方案。工作正常后,请尝试对此进行改进。

希望这会有所帮助!