Android custom listview working but having problems

Asked

Viewed 192 times

1

Hello, I am making a Binding and I have only one of the properties of my Model that are appearing in my listview. Follow the code:

Adapter:

public class ProjectAdapter extends ArrayAdapter<Project>{

    private List<Project> _projects;

    public ProjectAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        // TODO Auto-generated constructor stub
    }

    public ProjectAdapter(Context context, List<Project> projects) {
        super(context, R.layout.project_list_view, projects);

        this._projects = projects;

    }

    @SuppressLint("InflateParams") @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {

            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.project_list_view, null, true);

        }

        Project project = this._projects.get(position);

        if (project != null) {

            TextView txtPName = (TextView) v.findViewById(R.id.txtProjectName);
            TextView txtCName = (TextView) v.findViewById(R.id.txtCustomerName);
            TextView txtCEmail = (TextView) v.findViewById(R.id.txtCustomerEmail);
            TextView txtCPhone = (TextView) v.findViewById(R.id.txtCustomerPhone);
            TextView txtWTime = (TextView) v.findViewById(R.id.txtWorkingTime);
            TextView txtAMoney = (TextView) v.findViewById(R.id.txtAmountMoney);

            if (txtPName != null) {
                txtPName.setText(project.getName());
            }
            if (txtCName != null) {
                txtCName.setText(project.getCustomerName());
            }
            if (txtCEmail != null) {
                txtCEmail.setText(project.getCustomerEmail());
            }
            if (txtCPhone != null) {
                txtCPhone.setText(project.getCustomerPhone());
            }
            if (txtWTime != null) {
                txtWTime.setText(project.getWorkingTime().toString());
            }
            if(txtAMoney != null){
                txtAMoney.setText(project.getAmountMoney().toString());
            }
        }

        return v;

    }
}

Model:

public class Project {

    public int Id;  
    public String Name;
    public String CustomerName;
    public String CustomerEmail;
    public String CustomerPhone;
    public Float WorkingTime = null,
                    AmountMoney = null;

    public int getId() {
        return Id;
    }
    public void setId(int id) {
        Id = id;
    }   


    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }

    public String getCustomerName() {
        return CustomerName;
    }
    public void setCustomerName(String customerName) {
        CustomerName = customerName;
    }

    public String getCustomerEmail() {
        return CustomerEmail;
    }
    public void setCustomerEmail(String customerEmail) {
        CustomerEmail = customerEmail;
    }

    public String getCustomerPhone() {
        return CustomerPhone;
    }
    public void setCustomerPhone(String customerPhone) {
        CustomerPhone = customerPhone;
    }

    public Float getWorkingTime() {
        return WorkingTime;
    }
    public void setWorkingTime(Float workingTime) {
        WorkingTime = workingTime;
    }

    public Float getAmountMoney() {
        return AmountMoney;
    }
    public void setAmountMoney(Float amountMoney) {
        AmountMoney = amountMoney;
    }
}

main_page.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="universo91.freelancecalculator.MainPageActivity" >

    <ListView
        android:id="@+id/listProject"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtMProject"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp" >
    </ListView>

    <TextView
        android:id="@+id/txtMProject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/lbl_page_title"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

project_list_view.xaml (should be each item in my listview):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >   

        <TextView
            android:id="@+id/txtProjectName"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"                  
        />

        <TextView
            android:id="@+id/txtCustomerName"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"         
        />

        <TextView
            android:id="@+id/txtCustomerEmail"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"            
        />

        <TextView
            android:id="@+id/txtCustomerPhone"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"            
        />

        <TextView
            android:id="@+id/txtWorkingTime"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"            
        />

        <TextView
            android:id="@+id/txtAmountMoney"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp"            
        />    
</LinearLayout>

Exit: inserir a descrição da imagem aqui

Note in the print that this desparelho and also do not appear the other fields without being the Projectname.

2 answers

1

Note that all project_list_view.xml elements are layout_width="fill_parent" and Linearlayout is horizontal oriented.

Switch to the vertical orientation that should work.

  • Hi, I was wondering why the only field that does not appear is txtCustomerEmail.

  • I got it, I was getting it twice in the email. I’ll edit the post

1


I see two things that might be in trouble. The orientation of your Linearlayout is horizontal and all items are with fill_parent (we now use match_parent). Try changing the project_list_view.xaml file to the code below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >   

        <TextView
            android:id="@+id/txtProjectName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"                  
        />

        <TextView
            android:id="@+id/txtCustomerName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"         
        />

        <TextView
            android:id="@+id/txtCustomerEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"            
        />

        <TextView
            android:id="@+id/txtCustomerPhone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"            
        />

        <TextView
            android:id="@+id/txtWorkingTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"            
        />

        <TextView
            android:id="@+id/txtAmountMoney"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"            
        />    
</LinearLayout>
  • Hi, I was wondering why the only field that does not appear is txtCustomerEmail.

  • I got it, I was getting it twice in the email.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.