如何使用适配器在片段中为listview创建OnClick侦听器?

时间:2018-03-08 06:08:22

标签: java android android-fragments

我正在尝试使用适配器在多个片段中创建nested listviews。我能够在单个活动中创建它,但我不能使用适配器在片段中创建它。将第二个片段作为fragment2引用,将第二个列表作为listview2引用。

package com.example.admin.volleyrequestandresponse.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.admin.volleyrequestandresponse.R;
import com.example.admin.volleyrequestandresponse.activities.NavigationActivity;
import com.example.admin.volleyrequestandresponse.fragments.HomeFragment;

import java.util.ArrayList;


public class HomeFragmentAdapter extends BaseAdapter {

    private ArrayList<String> stringArray;
    private ArrayList<String> integerArray;
    int[] logos;
    Context context;
    NavigationActivity navigationActivity;

    public HomeFragmentAdapter(Context context, ArrayList<String> stringArray, ArrayList<String> integerArray, int[] logos) {
        this.context = context;
        this.stringArray = stringArray;
        this.integerArray = integerArray;
        this.logos = logos;
        navigationActivity = new NavigationActivity();
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.home_adapter, null);
            holder = new ViewHolder();
            holder.imageview = (ImageView) convertView.findViewById(R.id.indeximg);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.indexname);
            holder.text = (TextView) convertView.findViewById(R.id.indexvalue);
            holder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.home_adapter);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.imageview.setImageResource(logos[position]);
        holder.txtTitle.setText(stringArray.get(position));
        holder.text.setText(integerArray.get(position));

        return convertView;
    }


    private class ViewHolder {
        RelativeLayout relativeLayout;
        TextView text;
        TextView txtTitle;
        ImageView imageview;
    }
}
  

调用适配器的片段代码

homeFragmentAdapter = new HomeFragmentAdapter(getActivity(),index_string,index_values,index_logo);
listview_HomeFragment.setAdapter(homeFragmentAdapter);

0 个答案:

没有答案