通过单击列表视图中创建的卡,从一个活动移动到另一个活动

时间:2017-10-25 16:04:55

标签: java android listview cardview

#beginner ...我使用列表视图创建了卡片列表here is the image 我想通过点击列表视图中的卡片来到新屏幕...请帮我解决这个问题....请给我详细说明我可以通过哪些方式完成任务来解决我的问题....  ....这是我编码的::::

Main_Activity.java

package com.android.mezohn.nepstudy.app;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.android.mezohn.nepstudy.Adapter.subjectAdapter;
import com.android.mezohn.nepstudy.R;
import com.android.mezohn.nepstudy.model.ListDetails;
import com.android.mezohn.nepstudy.model.model;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private ArrayList<model>models;
    private subjectAdapter subjectAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        listView = (ListView)findViewById(R.id.list_view);
        models = ListDetails.getList();

        subjectAdapter = new subjectAdapter(MainActivity.this,models);
        listView.setAdapter(subjectAdapter);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

model.java

package com.android.mezohn.nepstudy.model;

/**
 * Created by ecspc on 25/10/2017.
 */

public class model {

    private int subjectImage;
    private String subjectTitle;
    private String count;

    public model(int subjectImage, String subjectTitle, String count) {
        this.subjectImage = subjectImage;
        this.subjectTitle = subjectTitle;
        this.count = count;
    }

    public int getSubjectImage() {
        return subjectImage;
    }

    public void setSubjectImage(int subjectImage) {
        this.subjectImage = subjectImage;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }

    public String getSubjectTitle() {
        return subjectTitle;

    }

    public void setSubjectTitle(String subjectTitle) {
        this.subjectTitle = subjectTitle;
    }
}

subjectAdapter.java

package com.android.mezohn.nepstudy.Adapter;

import android.content.Context;
import android.graphics.ColorSpace;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.mezohn.nepstudy.R;
import com.android.mezohn.nepstudy.model.model;

import java.util.ArrayList;

/**
 * Created by ecspc on 25/10/2017.
 */

public class subjectAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<model> models;

    public subjectAdapter(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_view,null);

            ImageView images = convertView.findViewById(R.id.cardImage);
            TextView title = convertView.findViewById(R.id.cardTitle);
            TextView count = convertView.findViewById(R.id.count);
            model model = models.get(position);
            images.setImageResource(model.getSubjectImage());
            title.setText(model.getSubjectTitle());
            count.setText(model.getCount());
        }

        return convertView;
    }
}

ListDetails.java

package com.android.mezohn.nepstudy.model;

import com.android.mezohn.nepstudy.R;

import java.util.ArrayList;

/**
 * Created by ecspc on 25/10/2017.
 */

public class ListDetails {

    public static ArrayList<model> getList(){
        ArrayList<model> subjectList = new ArrayList<>();
        subjectList.add(new model(R.drawable.phy,"Physics","4 chapters available"));
        subjectList.add(new model(R.drawable.che,"Chemistry","5 chapters available "));
        subjectList.add(new model(R.drawable.que,"Model Questions","10 model Questions available"));

       return subjectList;
    }
}

我是初学者,所以我希望我能得到足够的帮助。

1 个答案:

答案 0 :(得分:0)

因此,您似乎没有尝试向ListView添加侦听器。您可以通过AdapterView.OnItemClickListener

执行此操作
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        //.. launch your new activity here using Intent
        Intent intent = new Intent(MainActivity.this, NewActivity.class);
        startActivity(intent);
    }
});

另一方面,你应该更好地命名你的课程。推荐阅读:Effective Java

你还应该阅读:Making a listview scroll smooth谈论ViewHolder模式,或者只要你知道更多的android就转移到RecyclerView。