如何从列表视图中获取项目

时间:2018-03-07 14:55:32

标签: android listview android-fragments android-arrayadapter android-videoview

我有视频观看,我需要从列表视图中获取视频的网址,列表视图是不同视频的列表。我尝试使用此代码执行此操作:

.....ArrayList<Row> list = new ArrayList<Row>(); 
.....final CustomAdapter adapter = new CustomAdapter (this, list); 
.....(String.valueOf(adapter.list.get(position).getURL())); 

但它确实起作用。我究竟做错了什么?请帮忙。 THX。

这里是自定义适配器代码(CustomAdapter.java):

public class CustomAdapter extends BaseAdapter implements Filterable {
    Context context;
    ArrayList<Row> list;
    ArrayList<Row> FilterList;
    ValueFilter valueFilter;
    public CustomAdapter(Context context, ArrayList<Row> list) {
        this.context = context;
        this.list = list;
        FilterList = list;
    }
    @Override
    public int getCount() {return list.size();}
    @Override
    public Object getItem(int position) {return list.get(position);}
    @Override
    public long getItemId(int position) {return list.indexOf(getItem(position));}
    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        view = null;
        if (view == null) {
            view = inflater.inflate(R.layout.custom_layout, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
            TextView textView_nome = (TextView) view.findViewById(R.id.textView_nome);
            TextView textView_descrizione = (TextView) view.findViewById(R.id.textView_descrizione);
            Row row = list.get(position);
            imageView.setImageResource(row.getIMAGES());
            textView_nome.setText(row.getNAMES());
            textView_descrizione.setText(row.getDESCRIPTIONS());
        }
            return view;
    }
    @Override
    public Filter getFilter() {
        if (valueFilter == null) {
            valueFilter = new ValueFilter();
        }
        return valueFilter;
    }
    private class ValueFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint != null && constraint.length() > 0) {
                ArrayList<Row> lista = new ArrayList<Row>();
                for (int i = 0; i < FilterList.size(); i++) {
                    if ((FilterList.get(i).getNAMES().toUpperCase())
                            .contains(constraint.toString().toUpperCase())) {
                        Row row = new Row(FilterList.get(i)
                                .getNAMES(), FilterList.get(i)
                                .getDESCRIPTIONS(), FilterList.get(i)
                                .getURL(), FilterList.get(i)
                                .getIMAGES());
                        lista.add(row);
                    }
                }
                results.count = lista.size();
                results.values = lista;
            } else {
                results.count = FilterList.size();
                results.values = FilterList;
            }
            return results;
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            list = (ArrayList<Row>) results.values;
            notifyDataSetChanged();
        }
    }
}

这里是单行代码(Row.java):

public class Row {
    String NAMES;
    String DESCRIPTIONS;
    String URL;
    int IMAGES;
    public Row(String NAMES, String DESCRIPTIONS, String URL, int IMAGES) {
        this.NAMES = NAMES;
        this.DESCRIPTIONS = DESCRIPTIONS;
        this.URL = URL;
        this.IMAGES = IMAGES;
    }
    public String getNAMES() {return NAMES;}
    public void setNAMES(String NAMES) {this.NAMES = NAMES;}
    public String getDESCRIPTIONS() {return DESCRIPTIONS;}
    public void setDESCRIPTIONS(String DESCRIPTIONS) {this.DESCRIPTIONS = DESCRIPTIONS;}
    public String getURL() {return URL;}
    public void setURL(String URL) {this.URL = URL;}
    public int getIMAGES() {return IMAGES;}
    public void setIMAGES(int IMAGES) {this.IMAGES = IMAGES;}
}

这里是listview片段的代码(Canali.java):

public class Canali extends Fragment {

    int[] IMAGES = {R.drawable.rai1, ... };

    String[] NAMES = {"Rai 1", ...};

   String[] DESCRIPTIONS = {"1", ...};

    String[] URL = {"http://---.ts", ...};

    public Canali() {}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_canali, container, false);
    }
    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final ListView listView = (ListView)view.findViewById(R.id.listView);
        final EditText editText = (EditText) view.findViewById(R.id.editText);
        final ImageButton imageSearch = (ImageButton) view.findViewById(R.id.imageSearch);
        imageSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager inputManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);}});
        ArrayList<Row> list = new ArrayList<Row>();
        for (int i = 0; i <NAMES.length; i++) {Row row = new Row(NAMES[i] , DESCRIPTIONS[i] , URL[i], IMAGES[i]); list.add(row);}
        final CustomAdapter adapter = new CustomAdapter(getActivity(), list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(getActivity(), Player.class);
                i.putExtra("id", position);
                startActivity(i);}});
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {adapter.getFilter().filter(cs);}
            @Override
            public void beforeTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {}
            @Override
            public void afterTextChanged(Editable cs) {}
        });
    }
}

1 个答案:

答案 0 :(得分:0)

onItemClick上,你可以这样做:

Row row = (Row) adapter.getItem(position);
String url = row.getURL();
i.putExtra("url", url);