[英]list view doesn't show in my activity
I'm not very experienced with android,I created a listView in layout of MainActivity and a layout with two text view and a ImageView for every row in my list (name of layout = view_of_list) and I have created MyContact class which includes three members: String name, String phone, int Id. 我对Android不太熟悉,我在MainActivity的布局中创建了一个ListView,并且为列表中的每一行(布局名称= view_of_list)创建了两个文本视图和一个ImageView布局,并且我创建了MyContact类,其中包括三个成员:字符串名称,字符串电话,整数ID。 I also created CustomAdapter class. 我还创建了CustomAdapter类。 finally I set my CostumAdapter to my listView. 最后,我将CostumAdapter设置为listView。 when I run the code and it crashes; 当我运行代码并崩溃时; Android Monitor says in CustomAdapter.java in line: Android Monitor在CustomAdapter.java中说:
rowView = inflater.inflate(R.layout.view_of_list, parent, false);
got this error: 得到这个错误:
Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup, boolean)' on a null object reference 尝试在空对象引用上调用虚拟方法'android.view.View android.view.LayoutInflater.inflate(int,android.view.ViewGroup,boolean)'
where is my mistake? 我的错误在哪里?
activity_main.xml activity_main.xml
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.list.steve.list.MainActivity">
<ListView
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp"
android:id="@+id/list_view"/>
</android.widget.RelativeLayout>
view_of_list.xml view_of_list.xml
<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"
android:id="@+id/relative_layout">
<ImageView
android:id="@+id/img_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_alignTop="@id/img_profile"
android:layout_toRightOf="@id/img_profile"
android:layout_marginLeft="8dp"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_below="@id/tv_phone"
android:layout_toRightOf="@id/img_profile"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"/>
</RelativeLayout>
MyContact.java MyContact.java
public class Mycontact {
private String name;
private String phone;
private int id;
public Mycontact(String name, String phone, int id){
this.name = name;
this.phone = phone;
this.id = id;
}
public void setName(String name){
this.name = name;
}
public void setPhone(String phone){
this.phone = phone;
}
public void setId(int id){
this.id = id;
}
public String getName(){
return name;
}
public String getPhone(){
return phone;
}
public int getId(){
return id;
}
CustomAdapter.java CustomAdapter.java
public class CustomAdapter extends ArrayAdapter {
private ArrayList mycontacts;
private Mycontact contact;
private LayoutInflater inflater;
private ViewHolder holder;
public CustomAdapter(Context context, ArrayList contact ) {
super(context, R.layout.view_of_list, contact);
this.mycontacts = contact;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View rowView = convertView;
if(convertView == null) {
rowView = inflater.inflate(R.layout.view_of_list, parent, false);
holder.tv_name = (TextView) rowView.findViewById(R.id.tv_name);
holder.tv_phone = (TextView) rowView.findViewById(R.id.tv_phone);
holder.img_profile = (ImageView) rowView.findViewById(R.id.img_profile)
rowView.setTag(holder);
}else{
holder = (ViewHolder) rowView.getTag();
}
contact = (Mycontact)mycontacts.get(position);
holder.tv_name.setText(contact.getName());
holder.tv_phone.setText(contact.getPhone());
holder.img_profile.setImageResource(contact.getId());
return rowView;
}
public class ViewHolder{
TextView tv_name;
TextView tv_phone;
ImageView img_profile;
}
} }
MainActivity.java MainActivity.java
public class MainActivity extends AppCompatActivity {
Mycontact contact;
ArrayList<Mycontact> mycontacts;
ListView listView;
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mycontacts = new ArrayList<Mycontact>();
adapter = new CustomAdapter(this,mycontacts);
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
public void AddContact(){
mycontacts.add(new Mycontact("steve","09012345678",R.drawable.one));
mycontacts.add(new Mycontact("hasan","091112345678",R.drawable.two));
mycontacts.add(new Mycontact("hosein", "09151338089",R.drawable.three));
}
} }
you should call AddContact() method before set adapter on your list. 您应该在列表上设置适配器之前调用AddContact()方法。 and Initialize your inflater in constructor like this 然后像这样在构造函数中初始化充气机
public CustomAdapter(Context context, ArrayList contact ) {
super(context, R.layout.view_of_list, contact);
this.mycontacts = contact;
this.inflater = LayoutInflater.from(context);
}
also you should construct viewHolder class in getView() method like this 你也应该像这样在getView()方法中构造viewHolder类
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View rowView = convertView;
holder = new ViewHolder();
if(convertView == null) {
rowView = inflater.inflate(R.layout.view_of_list, parent, false);
holder.tv_name = (TextView) rowView.findViewById(R.id.tv_name);
holder.tv_phone = (TextView) rowView.findViewById(R.id.tv_phone);
holder.img_profile = (ImageView) rowView.findViewById(R.id.img_profile)
rowView.setTag(holder);
}else{
holder = (ViewHolder) rowView.getTag();
}
contact = (Mycontact)mycontacts.get(position);
holder.tv_name.setText(contact.getName());
holder.tv_phone.setText(contact.getPhone());
holder.img_profile.setImageResource(contact.getId());
return rowView;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.