带有滑动标签的Android自定义适配器图片列表视图

时间:2015-07-15 20:44:04

标签: android listview android-arrayadapter

我正在尝试制作带有标签和滑动视图的Android应用程序,我在一个标签内有一个列表视图,而且我还在列表中添加了一个图像视图,并使用自定义适配器填充列表中的数据,我在将数据加载到列表时遇到了一些错误(但是工作完美的活动类),行和错误都是 写在下面

 Adapter adapter=new Adapter(this, names , distripton, price, images); 
 error is the constructor Adapter(javafragment,string[],string[])is undefined

我的xml页面是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_dark"
     >
     <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
</RelativeLayout>

java片段类是

package com.example.newlistimage;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class Javaframent extends Fragment {

    ListView lv;
    String[] names={"haseem","hac"};
    String[] distripton={"dis1","dis2"};
    String[] price={"10","20"};
    int[] images={R.drawable.ic_launcher,R.drawable.ic_launcher};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        lv=(ListView) getActivity().findViewById(R.id.listView1);
        Adapter adapter=new Adapter(this, names , distripton, price, images);
        lv.setAdapter(adapter);

        return inflater.inflate(R.layout.java_layout ,container,false);
    }

}

自定义适配器类

package com.example.newlistimage;

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

public class Adapter extends ArrayAdapter<String>  {

    int[] images={};
    String[] names={};
    String[] prices={};
    String[] discriptions={};
    Context c;
    LayoutInflater inflater; 


        public Adapter(Context context,String[] names,String[] distrion,String[]    price,int[] img) {
        super(context, R.layout.model,names);
            this.c=context;
            this.names=names;
            this.prices=price;
            this.discriptions=distrion;
            this.images=img;

    }

    public class ViewHolder
    {
        TextView nametv;
        TextView discriptiontv;
        TextView pricetv;
        ImageView images;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub


        if(convertView==null)
        {
            inflater=(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView=inflater.inflate(R.layout.model, null);

        }

        final ViewHolder holder=new ViewHolder();
        holder.nametv=(TextView) convertView.findViewById(R.id.nametv);
        holder.discriptiontv=(TextView) convertView.findViewById(R.id.discriptiontv);
        holder.pricetv=(TextView) convertView.findViewById(R.id.moneytv);
        holder.images=(ImageView) convertView.findViewById(R.id.imageView1);

        holder.images.setImageResource(images[position]);
        holder.nametv.setText(names[position]);
        holder.pricetv.setText(prices[position]);
        holder.discriptiontv.setText(discriptions [position]);

    //  return super.getView(position, convertView, parent);
        return convertView;
    }

}

1 个答案:

答案 0 :(得分:0)

问题在于:

Adapter adapter=new Adapter(this, names , distripton, price, images);
在片段中

以获取上下文,您必须调用getActivity()方法。

所以你的代码就像这样:

Adapter adapter=new Adapter(getActivity(), names , distripton, price, images);

更新:

我认为NullPointerException是关于这行代码的:

 lv=(ListView) getActivity().findViewById(R.id.listView1);

当您尝试使用您的活动视图查找列表视图时,您应该创建一个视图并按照该列表查找列表视图

我编辑了你应该运行的代码:

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.java_layout ,container,false);
        lv=(ListView) v.findViewById(R.id.listView1);
        Adapter adapter=new Adapter(this, names , distripton, price, images);
        lv.setAdapter(adapter);

        return v;
    }