从自定义列表视图的客户列表适配器中提取条目以获取新意图

时间:2014-09-22 03:25:00

标签: android listview android-arrayadapter android-viewholder

我正在尝试点击列表条目。我的列表是一个自定义列表视图,从ArrayAdapter中提取数据。该列表显示了Array条目的3个部分和一张图片,但我想将这些项目和其余的数组条目发送到下一个活动,以显示有关所选项目的更多信息。

下面是我的Class,里面有适配器。你会看到一个评论部分/ ** ** /我一直在试图弄清楚如何把这些项目放入其中,但我似乎无法弄明白。每本书都有12个属性,但在点击以获取更多信息之前,listView中只显示了4个。

我正在使用Viewholder,所以我不确定我是否会影响我,但我确实计划在项目点击之前在列表视图中有一个相当长的列表。

public class Novels extends ListActivity{

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

        setListAdapter(new NovelAdapter(this, R.layout.list_item, NovelsStore.getNovels()));

        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public Novels() {
    }

    public class NovelAdapter extends ArrayAdapter<Book> {

        LayoutInflater layoutInflater;

        public NovelAdapter(Context context, int textViewResourceId, List<Book> novels) {
            super(context, textViewResourceId, novels);
            layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent){
            ViewHolder holder;
            Book novels = getItem(position);

            // if no references in the view
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.list_item, null);

                // put them into a holder
                holder = new ViewHolder();

                holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
                holder.tvSeries = (TextView) convertView.findViewById(R.id.tvSeries);
                holder.tvEra = (TextView) convertView.findViewById(R.id.tvEra);
                holder.ivBookImage = (ImageView) convertView.findViewById(R.id.ivBookImage);

                //set the view tag
                convertView.setTag(holder);
            } else {
                // in case we have a recycled view, just get the Tag
                holder = (ViewHolder) convertView.getTag();
            }

            //Assign Property to the TextViews
            holder.tvTitle.setText(novels.getTitle());
            holder.tvSeries.setText(novels.getSeries());
            holder.tvEra.setText(novels.getEra());
            holder.ivBookImage.setImageResource(novels.getBookImage());

            return convertView;
        }

        public class ViewHolder {
            public TextView tvTitle;
            public TextView tvSeries;
            public TextView tvEra;
            public ImageView ivBookImage;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // If app icon in the action bar gets clicked; go back
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

对此的任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

如果您可以Bookposition,您实际上可以在Book发送serializable对象,您可以将整个对象放入Intent。像这样的东西

@Override 
protected void onListItemClick(ListView listView, View view, int position, long id) { 
    super.onListItemClick(listView, view, position, id); 

    Intent i = new Intent(getApplicationContext(), SingleItemView.class); 
    i.putExtra("my_book", NovelsStore.getNovels().get(position)); 
    startActivity(i); 
}

你可以在SingleItemView中找到该对象,如下所示

Book b = getIntent().getExtra("my_book");

你应该做空检查。