简体   繁体   中英

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. I also created CustomAdapter class. finally I set my CostumAdapter to my listView. when I run the code and it crashes; Android Monitor says in CustomAdapter.java in line:

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

where is my mistake?

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

<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

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

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

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. 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

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM