片段内带有基本适配器的列表视图

Posted

技术标签:

【中文标题】片段内带有基本适配器的列表视图【英文标题】:listview with base adapter inside a fragment 【发布时间】:2015-06-11 01:53:06 【问题描述】:

我有一个带有 bade 适配器的列表视图,它实际上是一个静态的,每行包含 2 个文本视图和一个图像片段类它给我错误该行包含另一个活动的意图,但该意图给出了一个错误,并且将 adpater 设置为列表视图也会给出另一个错误 这是代码:

public class consulterfragment extends Fragment implements AdapterView.OnItemClickListener 
ListView list;
@Nullable    
@Override    
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
   View view= inflater.inflate(R.layout.consulter_fragment_layout,container,false) ;
    list= (ListView) view.findViewById(R.id.listView);
    list.setAdapter(new SanaAdapter(this));
    list.setOnItemClickListener(this);
    return view;


public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    Intent intent = new Intent(this, note_details.class);
    startActivity(intent);
    MyViewHolder holder = (MyViewHolder) view.getTag();
    SingleRow temp = (SingleRow) holder.myImage.getTag();
    intent.putExtra("titles", temp.titles);
    startActivity(intent);


class SingleRow 
    String titles;
    String description;
    int image;

    SingleRow(String titles, String description, int image) 
        this.titles = titles;
        this.description = description;
        this.image = image;
    

class MyViewHolder 
    ImageView myImage;
    TextView myTitle;
    TextView myDescription;

    MyViewHolder(View v) 
        myImage = (ImageView) v.findViewById(R.id.imageView2);
        myTitle = (TextView) v.findViewById(R.id.textView4);
        myDescription = (TextView) v.findViewById(R.id.textView5);
    


class SanaAdapter extends BaseAdapter 
    ArrayList<SingleRow> list;
    Context context;

    SanaAdapter(Context c) 
        context = c;
        list = new ArrayList<SingleRow>();
        Resources res = c.getResources();
        String[] titles = res.getStringArray(R.array.titles);
        String[] descriptions = res.getStringArray(R.array.decriptions);
        int[] images = R.drawable.ico1, R.drawable.ico2, R.drawable.ico3, R.drawable.ico4, R.drawable.ico5, R.drawable.ico6, R.drawable.ico7, R.drawable.ico8,
                R.drawable.ico9, R.drawable.ico10, R.drawable.ico1, R.drawable.ico2, R.drawable.ico3, R.drawable.ico4, R.drawable.ico5, R.drawable.ico6, R.drawable.ico7, R.drawable.ico8,
                R.drawable.ico9, R.drawable.ico10;
        for (int i = 0; i < 10; i++) 
            list.add(new SingleRow(titles[i], descriptions[i], images[i]));
        
    

    public int getCount() 
        return list.size();
    

    @Override
    public Object getItem(int i) 
        return list.get(i);
    

    @Override
    public long getItemId(int i) 
        return i;
    

    @Override
    public View getView(int i, View convertview, ViewGroup viewGroup) 
        View row = convertview;
        MyViewHolder holder = null;
        if (row == null) 
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.notelist_row, viewGroup, false);
            holder = new MyViewHolder(row);
            row.setTag(holder);
            Log.d("SANA", "Creating a new row");
         else 
            holder = (MyViewHolder) row.getTag();
            Log.d("SANA", "Recycling shit hhhhhh");
        

        SingleRow temp = list.get(i);

        holder.myTitle.setText(temp.titles);
        holder.myDescription.setText(temp.description);
        holder.myImage.setImageResource(temp.image);
        return row;
        
    

第一个错误是设置适配器 它说SanaAdapter 中的SanaAdapter(android.context.Context) 不能应用

第二个错误在于它所说的意图 无法解析构造函数Intent(com.example.hp.memorybackup.consulterfragment,java.lang.class&lt;com.example.hp.memorybackup.notedetails&gt;)

非常感谢您的帮助

【问题讨论】:

使用getActivity() 而不是this 【参考方案1】:

在 onCreateView 你正在调用

list.setAdapter(new SanaAdapter(this)); 

但正确的是

list.setAdapter(new SanaAdapter(getActivity()); 

也应该突出显示,是吗? Fragment 扩展了 Object,它不是 Context 的子对象。

【讨论】:

以上是关于片段内带有基本适配器的列表视图的主要内容,如果未能解决你的问题,请参考以下文章

如何从片段内的列表视图打开链接网址?

从活动更新视图页面中片段中的列表视图

带有 ListAdapter 的 RecyclerView

带有复选框和自定义适配器的 ListView,片段无法正常工作

将 JSON 字符串从片段传递到适配器的问题

Android - 为列表视图填充适配器的异步任务