ListView上的图片?

时间:2016-10-26 19:18:35

标签: java android listview

我有这个:

public class First_fragment extends Fragment {


    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.activity_profesorado,container,false);
        String[] menuItems = { 
            "Do some","kkk", ",,,,,", "Do some","kkk", ",,,,,", 
            "Do some", "kkk", ",,,,,", "Do some", "kkk", ",,,,,", 
            "Do some", "kkk", ",,,,,"
        };
        ListView listView = (ListView) v.findViewById(R.id.mymenu);
        ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
            getActivity(), android.R.layout.simple_list_item_1, menuItems
        );
        listView.setAdapter(listViewAdapter);
        return v;
    }
}

如果文字正确,我想使用IMAGES而不是文字。 listview中的10张图片。怎么办呢?你有什么例子吗?我真的厌倦了这一点,因为我整天都在为此编程而且我失败了。

1 个答案:

答案 0 :(得分:0)

使用ListActivityItemsAdapter可能会这样做。见下面的代码:

public class ItemsList extends ListActivity {

private ItemsAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.items_list);

    this.adapter = new ItemsAdapter(this, R.layout.items_list_item, ItemManager.getLoadedItems());
    setListAdapter(this.adapter);
}

private class ItemsAdapter extends ArrayAdapter<Item> {

    private Item[] items;

    public ItemsAdapter(Context context, int textViewResourceId, Item[] items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.items_list_item, null);
        }

        Item it = items[position];
        if (it != null) {
            ImageView iv = (ImageView) v.findViewById(R.id.list_item_image);
            if (iv != null) {
                iv.setImageDrawable(it.getImage());
            }
        }

        return v;
    }
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    this.adapter.getItem(position).click(this.getApplicationContext());
}
}
编辑:看起来你是Android开发的新手。您必须使用ImageView

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/picture_you_want_to_use" />