【问题标题】:list adapter: get item position without onclick ect列表适配器:在没有 onclick 等的情况下获取项目位置
【发布时间】:2015-12-22 23:50:49
【问题描述】:

我终于能够在第一个 url 适配器类中获取第二个 json url,但现在的问题就像没有被组织化,我认为这是因为我没有指定每个项目的位置来获取 url2,对吧?如果是这样,我该如何解决这个问题, 其他活动在获取第二个 url 时效果很好,但它在项目点击时工作,同时在这个活动中我试图在不点击的情况下获取。 在我从第一个 url 模型类获取数据并将其设置为我调用的视图之后,我在适配器类中使用 volley fetchurl2(字符串);在返回 ConvertView 之前;

【问题讨论】:

    标签: android json listview adapter android-volley


    【解决方案1】:

    您是说列表中的 onItemClicked 正在返回不正确的项目吗?

    好吧,我将简要介绍一下我遇到的一些问题,以及它们是否听起来像其中任何一个。

    1. 实现 onClickListener 并检索在列表视图中单击的视图的 ID 无法正常工作。这对我来说似乎是零星的,但我总是不得不绕过该功能。

    解决方案:实现 onListItemClickListener 为您提供点击的位置,因此它不太依赖 Id(例如:R.id.viewId)

    1. 列表视图不断更改数字和值,使其成为您认为单击后会出现的错误值

    解决方案:将对象引用保存在封装的 BaseAdapter 类中。我对 Volley 没有太多经验,但从头开始需要扩展 BaseAdapter 的类,并且您可以扩充自己的视图并填充数据。

    1. 确保您设置了正确的数据以传递到您的 fetch 类中

    这可能是 #1s 问题的结果,而且通常是这样,但如果您正在分配视图控件,请确保它们都有自己的唯一引用,并相对于膨胀的视图进行检索:

       //Method inflates a view as part of an implementation of BaseAdapter
       @Override
    public View getView(int position, View convertView, ViewGroup parent) {  
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LayoutInflaterService);
        convertView = inflater.inflater(R.layout.MYVIEW, parent, false);
        //Note the retrieval relative to the view being inflated 
        final TextView tvURL = (TextView)convertView.findViewById(R.id.tvURL);
        tvURL.setText("www.stackoverflow.com");
        tvURL.setOnClickListener(new View.OnClickListener()
        fetchURL(tvURL.getText().toString());
        return convertView;
    }
    

    编辑

    一个示例适配器:

    public class MyAdapter extends BaseAdapter {
        private Context context = null;
        private ArrayList<CustomItem> items = new ArrayList<>();
        public MyAdapter(Context context, ArrayList<CustomItem> items)
        {
            this.context = context;
            this.items = items;
        }
        @Override
        public int getCount() {
            return items.size();
        }
    
        @Override
        public Object getItem(int position) {
            //will never exceed position as that has a range of 0 to the size we give
            return items.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.mylistitem);
            TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
            ImageView ivFeature = (ImageView)convertView.findViewById(R.id.ivFeature);
            TextView tvDescription = (TextView)convertView.findViewById(R.id.tvDescription);
            CustomItem item = items.get(position);
            tvName.setText(item.getName());
            tvDescription.setText(item.getDescription());
            //Call for your item to process a picture into a bitmap then
            ivFeature.setImageBitmap(item.getFeatureImage());
            return convertView;
        }
    }
    
    /*
        //Instantiation:
        myListView = (ListView)findViewById(R.id.myListView);
        MyAdapter adapter = new MyAdapter(Activity.this, myItemsList);
        myListView.setAdapter(adapter);
        myListView.invalidate();
    
     */
    

    【讨论】:

    • 感谢您的回答,但现在 onclick 方法在活动 A 中工作正常,我需要的是在活动 B 中我想要相同的工作“获取 json url”但没有 OnClick
    • 如果你有 url,你可以在 convertview 方法中调用它或使用 AsyncTask 异步预填充预期结果
    • 是的,但结果是:一些图像视图有时是空的,有时是填充的,所有项目的文本视图相同的文本,一些图像发生变化并继续加载其他项目图像
    • 您是否选择了 BaseAdapter 解决方案?如果你愿意,我可以给你做一个完整的例子
    • 我不明白你的意思因为我的adabter extents baseadapter
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    相关资源
    最近更新 更多