Android: Empty textview for first item in a listview

859 views Asked by At

I have finally implemented an extended ArrayAdapter and everything works correctly, other that the fact that the very first item added to the list display with no text in the text view. Interestingly though, once enough items have been added that the list is long enough to scroll the 'textless' row off the screen and then back on again, then it now shows correctly with text. Given that im also displaying a toast to make sure that what i need to display is actually there,. then i think the problem is really to do with the way the list is displaying rather that some programmatic error in the way the list is constructed in the first place. Im new to this and completely prepared to acknowledge any mistakes i may have made. The only question i could find was todo with the layout of the listview item being linear rather than relative layout so i changed it and it did the same. My code is below, sorry its long but i was unsure which bits could be excluded, so you've got it all. I appreciate any help at all here :)

    package com.bitcrazy.gez.electricitycost;

    import android.content.Context;
    import android.content.Intent;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ImageButton;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;

    import java.util.ArrayList;
    import java.util.List;


    public class HouseholdViewer extends ActionBarActivity {
        public Household house = new Household();
        ApplianceListAdapter ala = null;

        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_household_viewer);
            AdView mAdView = (AdView) findViewById(R.id.adViewHousehold);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
            ala = new ApplianceListAdapter(
            this, R.layout.app_list_item, house.getApplianceDescs());
    ala.notifyDataSetChanged();
    ListView lv = (ListView) findViewById(R.id.listView_appliancesList);

    lv.setAdapter(ala);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(),
                    "Appliance was clicked at " + position, Toast.LENGTH_LONG).show();
        }
    });
    Button addAppliance = (Button) findViewById(R.id.button_AddAppliance);
    addAppliance.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            Intent gotoAddAppliance = new Intent(v.getContext(), AddAppliance.class);
            startActivityForResult(gotoAddAppliance, 1);
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            Appliance a = data.getParcelableExtra("newAppliance");
            a.setCost(a.getCost(house.getElectricityCostPerKwh()) / 100);
            a.scaleCost(house.getAtts().getDaysInMonth());
            house.addAppliance(a);
            Toast.makeText(getApplicationContext(),
                    "new Appliance " + a.toString(),
                    Toast.LENGTH_LONG).show();
        }
    }
    ala.notifyDataSetChanged();
}

        @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_household_viewer, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private class ApplianceListAdapter extends ArrayAdapter<String>
{
    private int layout;
    public ApplianceListAdapter(Context context, int resource, List<String> objects)
    {
        super(context, resource, objects);
        layout = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int pos = position;
        Viewholder holder;
        if(convertView == null)
        {

            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(layout, parent, false);

            holder = new Viewholder();

            holder.thumbnail = (ImageView) convertView.findViewById(
                    R.id.imageView_applianceIcon);
            holder.description = (TextView) convertView.findViewById(
                    R.id.textView_ApplianceTextDesc);
            holder.edit = (Button) convertView.findViewById(
                    R.id.button_edit);
            holder.deleteButton = (ImageButton) convertView.findViewById(
                    R.id.imageButton_removeAppliance);
        }
        else {
            holder = (Viewholder) convertView.getTag();
            holder.description.setText(getItem(pos));
        }
        holder.deleteButton.setOnClickListener(new AdapterView.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "Delete was clicked for List Item "
                        + pos,Toast.LENGTH_SHORT).show();
            }
        });
        holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "Edit was clicked for Appliance "
                        + pos, Toast.LENGTH_SHORT).show();
            }
        });
        convertView.setTag(holder);
        return convertView;
    }
}

private class Viewholder
{
    ImageView thumbnail;
    TextView description;
    Button edit;
    ImageButton deleteButton;
}

}

3

There are 3 answers

1
Amsheer On

Move the following line outside of else condition

 holder.description.setText(getItem(pos));

Reason: You are adding a condition

  if(convertView == null){
}else{
}

You setting your text only inside of else condition. So once it enter the if condition the row is empty.

Note:

Whenever you add items to listView just consider if and else part. This most common issue.

0
madhuri H R On

Make the following changes in your ApplianceListAdapter adapter:

if(){

// your code

}else{
 holder = (Viewholder) convertView.getTag();
}

 holder.description.setText(getItem(pos));

Set the description text after if else block. Hope this works.

0
Android Boy On

Your code is :

else {
            holder = (Viewholder) convertView.getTag();
            holder.description.setText(getItem(pos));
        }

Change it in :

else {
            holder = (Viewholder) convertView.getTag();

        }

holder.description.setText(getItem(pos));