Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将图像添加到ListView_Android_Eclipse_Image_Listview - Fatal编程技术网

Android 如何将图像添加到ListView

Android 如何将图像添加到ListView,android,eclipse,image,listview,Android,Eclipse,Image,Listview,我正在尝试向这个列表视图添加一些图像,但是我找不到方法。 我还试着创建一个新的列表视图(我在YouTube上看到过),但我无法让它打开另一个activity xD private void populateListView() { // Create list of items String[] items = {"Apps", "Games", "Other"}; // Build Adapter ArrayAdapter<String> adap

我正在尝试向这个列表视图添加一些图像,但是我找不到方法。 我还试着创建一个新的列表视图(我在YouTube上看到过),但我无法让它打开另一个activity xD

private void populateListView() {
    // Create list of items
    String[] items = {"Apps", "Games", "Other"};

    // Build Adapter
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.mainitem, items);

    // Configure the list view
    ListView list = (ListView) findViewById(R.id.listViewMain);
    list.setAdapter(adapter);

}

private void registerClickCallBack() {
    final ListView list = (ListView) findViewById(R.id.listViewMain);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {

            if(position == 0) {
                Intent myIntent = new Intent(viewClicked.getContext(), Apps.class);
                startActivityForResult(myIntent, 0);
            }
            if(position == 1) {
                Intent myIntent = new Intent(viewClicked.getContext(), Games.class);
                startActivityForResult(myIntent, 0);
            }
            if(position == 2) {
                Intent myIntent = new Intent(viewClicked.getContext(), Other.class);
                startActivityForResult(myIntent, 0);
            }

        }
    });

}
private void populateListView(){
//创建项目列表
字符串[]项={“应用程序”、“游戏”、“其他”};
//构建适配器
ArrayAdapter=新的ArrayAdapter(this,R.layout.main项,items);
//配置列表视图
ListView列表=(ListView)findViewById(R.id.listViewMain);
list.setAdapter(适配器);
}
私有无效注册表ClickCallback(){
最终ListView列表=(ListView)findViewById(R.id.listViewMain);
list.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图已单击、整型位置、长id){
如果(位置==0){
Intent myIntent=newintent(viewClicked.getContext(),Apps.class);
startActivityForResult(myIntent,0);
}
如果(位置==1){
Intent myIntent=新意图(viewClicked.getContext(),Games.class);
startActivityForResult(myIntent,0);
}
如果(位置==2){
Intent myIntent=newintent(viewClicked.getContext(),Other.class);
startActivityForResult(myIntent,0);
}
}
});
}

如果你知道,请告诉我;)

要为ListView上的每一行添加一个图像,您需要定义一个自定义的.xml文件,在该文件中,您必须创建单行的布局,以显示图像和标题/描述的方式。然后,您必须扩展ArrayAdapter类,以便可以扩大在getView方法中创建的行布局,在重新定义ArrayAdapter类时必须覆盖该方法。因此,简言之:

  • 像创建普通布局文件一样创建row.xml文件
  • 重新定义ArrayAdapter类,并在getView方法中膨胀定义的行布局

  • 编辑:查看一下这个

    您需要创建一个自定义适配器。 尝试从BaseAdapter扩展并实现接口

    公共类YourCustomAdapter扩展BaseAdapter实现ListAdapter{

    您将覆盖各种方法,包括“getView”,您可以在其中展开包含图像的布局(针对该位置)

    接下来,您将在ListView上设置适配器,如:yourListView.setAdapter(new CustomAdapter())

    这是谷歌的开发者指南。
    这是为您定制的适配器

    public class CustomListAdapter extends BaseAdapter{
    
    
        private List<Location> locations;
        private static LayoutInflater inflator = null;
    
    
        public CustomListAdapter(List<Location> locations) {
    
            super();
            this.cp = 0;
            this.activity = activity;
            this.locations = locations;
    
            inflator = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return this.locations.size();
        }
    
        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
    
            this.cp = position;
            View vi = convertView;
    
            try{
    
                if (convertView == null)
                    vi = inflator.inflate(R.layout.activity_single_location_view, null);
    
                TextView locationName = (TextView) vi.findViewById(R.id.location_name);
    
    
                Location loc = locations.get(position);
    
                locationName.setText(loc.getName());
    
            }
            catch(Exception e){
                Log.i("CustomListAdapter.getView",e.toString());
                e.printStackTrace();
            }
    
    
            return vi;
        }
    
    }
    
    LocatioList.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">
    
        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:divider="#b5b5b5"
            android:dividerHeight="1dp"
            android:paddingTop="0dip"
            android:listSelector="@drawable/list_selector" />
    
    
    </RelativeLayout>
    
    
    
    对于每行项目

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/list_selector"
        android:orientation="horizontal"
        android:padding="5dip" >
    
    
          <ImageView
                android:id="@+id/list_image"
                android:layout_width="0dip"
                android:layout_height="0dip"
                android:contentDescription="@string/news_image_title_ph"
                android:src="@drawable/place_holder_image"/>
    
           <TextView
            android:id="@+id/location_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/list_image"
            android:text="@string/location_name"
            android:textColor="#040404"
            android:typeface="sans"
            android:textSize="15sp"
            android:textStyle="bold"/>
    
            <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:contentDescription="@string/news_details" />
    
    
    </RelativeLayout>
    
    
    

    您可以在添加文本视图时添加图像视图

    您将实现一个自定义适配器。
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/list_selector"
        android:orientation="horizontal"
        android:padding="5dip" >
    
    
          <ImageView
                android:id="@+id/list_image"
                android:layout_width="0dip"
                android:layout_height="0dip"
                android:contentDescription="@string/news_image_title_ph"
                android:src="@drawable/place_holder_image"/>
    
           <TextView
            android:id="@+id/location_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/list_image"
            android:text="@string/location_name"
            android:textColor="#040404"
            android:typeface="sans"
            android:textSize="15sp"
            android:textStyle="bold"/>
    
            <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:contentDescription="@string/news_details" />
    
    
    </RelativeLayout>