android Custom Adapter只附加一个项目

时间:2016-06-10 11:30:25

标签: java android listview

这是我的java代码

public class profile extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    String[] web = profile.this.getResources().getStringArray(R.array.nav_drawer_labels);


    Integer[] imageId = {
            R.drawable.ic_home,
            R.drawable.ic_tv,
            R.drawable.ic_ticket,
            R.drawable.ic_offers,
            R.drawable.ic_earning,
            R.drawable.ic_usemoney,
            R.drawable.ic_ranking,
            R.drawable.ic_faq,
            R.drawable.ic_symbol21,


    };



    profile.CustomList adapter1 = new
            profile.CustomList(profile.this, web, imageId);
    ListView list=(ListView)findViewById(R.id.list);
    adapter1.notifyDataSetChanged();
    list.setAdapter(adapter1);

}
public class CustomList extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;
    public CustomList(Activity context,
                      String[] web, Integer[] imageId) {
        super(context, R.layout.nav_drawer_row, web);
        this.context = context;

        this.web = web;
        this.imageId = imageId;

    }

    @Override
    public int getCount() {

        return web.length;
    }



    @Override
    public long getItemId(int position) {

        return position;
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();



        View rowView= inflater.inflate(R.layout.nav_drawer_row, null, true);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);





        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        txtTitle.setText(web[position]);

        imageView.setImageResource(imageId[position]);
        return rowView;
    }
}

}

这是我的字符串值

<string-array name="nav_drawer_labels">
    <item>@string/title_home</item>
    <item>@string/title_videos</item>
    <item>@string/title_coupens</item>
    <item>@string/title_offers</item>
    <item>@string/title_extraearning</item>
    <item>@string/title_usemoney</item>
    <item>@string/title_ranking</item>
    <item>@string/title_faq</item>
    <item>@string/title_signout</item>
</string-array>

当我运行它时,它只在ListView中给我一个项目

这是我的ListView xml

 <ListView
  android:layout_marginLeft="7dp"
   android:layout_marginTop="10dp"
   android:id="@+id/list"

   android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:divider="@color/dividercolour">

     </ListView>

enter image description here

我想为列表视图创建自定义适配器。因此,当我运行活动时,它只显示一个项目

2 个答案:

答案 0 :(得分:0)

尝试删除getCount()getItemId()方法

public class CustomList extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] web;
    private final Integer[] imageId;
    public CustomList(Activity context,
                      String[] web, Integer[] imageId) {
        super(context, R.layout.nav_drawer_row, web);
        this.context = context;
        this.web = web;
        this.imageId = imageId;

    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView= inflater.inflate(R.layout.nav_drawer_row, parent, false);
        Log.d("position",""+position);
        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);

        txtTitle.setText(web[position]);

        imageView.setImageResource(imageId[position]);
        return rowView;
    }
}
}

检查记录位置,是否显示一个项目或全部?

答案 1 :(得分:0)

看起来你在ArrayAdapter构造函数中错误地分配了数组。

而不是:

this.web = web;
this.imageId = imageId;

尝试:

this.web = Arrays.copyOf(web, web.length);
this.imageId = Arrays.copyOf(imageId, imageId.length);