<?xml version="1.0" encoding="utf-8"?>[/code]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/title_item" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_launcher"
android:id="@+id/imageView"
android:layout_below="@+id/title_item"
android:layout_alignParentStart="true"
android:layout_marginStart="75dp" />
</RelativeLayout>
[code=java]package wz.com.passdata.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import wz.com.passdata.R;
/**
* 自定义适配器
* 继承ArrayAdapter
* 并将泛指类指定为ListViewGetItem
* Created by lenovo on 2017/10/29.
*/
public class MyListAdapter extends ArrayAdapter<ListViewGetItem> {
private int resourceId;
public MyListAdapter(Context context,int textId,List<ListViewGetItem> listdata){
super(context,textId,listdata);
resourceId=textId;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ListViewGetItem getItem=getItem(position);
View view= LayoutInflater.from(getContext()).inflate(resourceId,null);
//实例化item布局控件
ImageView img=(ImageView) view.findViewById(R.id.imageView);
TextView name=(TextView) view.findViewById(R.id.title_item);
//获取资源
img.setImageResource(getItem.getImageId());
name.setText(getItem.getName());
return view;
}
}
package wz.com.passdata.adapter;
import android.widget.ImageView;
/**
* 适配器的匹配类型
* ListViewGetItem是作为ListView适配器的适配类型
* Created by lenovo on 2017/10/29.
*/
public class ListViewGetItem {
private String name;
private int imageId;
//实例化一个对象,对类中对象初始化,子类继承父类是自动初始化父类
public ListViewGetItem(int imageId, String name) {
this.imageId = imageId;
this.name = name;
}
public int getImageId() {
return imageId;
}
public String getName() {
return name;
}
}
package wz.com.passdata;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import wz.com.passdata.adapter.ListViewGetItem;
import wz.com.passdata.adapter.MyListAdapter;
/**
* ListView显示
* Created by lenovo on 2017/10/29.
*/
public class MyListView extends Activity{
ListView listView;
//item数据组
private List<ListViewGetItem> getitem=new ArrayList<ListViewGetItem>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//布局与activity匹配
setContentView(R.layout.viewpager1);
//实例化listView
listView=(ListView) this.findViewById(R.id.vp1_listview);
initData();//初始化listView——item数据
//ListView适配器
MyListAdapter adapter=new MyListAdapter(MyListView.this,R.layout.vp1_item,getitem);
//listview获取适配器加载数据
listView.setAdapter(adapter);
}
public void initData(){
ListViewGetItem item1=new ListViewGetItem(R.drawable.icon,"00000");
getitem.add(item1);
ListViewGetItem item2=new ListViewGetItem(R.drawable.icon,"11111");
getitem.add(item2);
ListViewGetItem item3=new ListViewGetItem(R.drawable.icon,"22222");
getitem.add(item3);
ListViewGetItem item4=new ListViewGetItem(R.drawable.icon,"33333");
getitem.add(item4);
}
}
控制台报错
Error:(13) No resource identifier found for attribute 'srcCompat' in package 'wz.com.passdata'
Error:(13) No resource identifier found for attribute 'srcCompat' in package 'wz.com.passdata'
Error:Execution failed for task ':app:processDebugResources'.
> com.android.ide.common.process.ProcessException: Failed to execute aapt
R文件因为这些报错了
以下为我的依赖包,因为实在不知道是哪里的问题,希望有解
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
}
1 个解决方案
#1
突然解决了,是我直接把imageView添加到布局了,控件属性没弄好,改成只有id和宽高,运行,以上问题到没了,没仔细看好书。渣啊
#1
突然解决了,是我直接把imageView添加到布局了,控件属性没弄好,改成只有id和宽高,运行,以上问题到没了,没仔细看好书。渣啊