Android:列表视图中第一项的空文本视图

时间:2015-06-08 06:02:39

标签: android listview textview

我最终实现了一个扩展的ArrayAdapter并且一切正常,其他事实是添加到列表的第一个项目在文本视图中没有文本显示。 有趣的是,一旦添加了足够多的项目,列表足够长,可以在屏幕上滚动“无文字”行,然后重新打开,那么它现在可以正确显示文本。鉴于即时通讯还会显示一个祝酒词以确保我需要显示的内容实际上就在那里。那么我认为问题实际上与列表显示的方式有关,而不是首先构建列表的方式中的一些编程错误。我是新手,完全准备承认我可能犯的任何错误。我能找到的唯一问题是todo,listview项目的布局是线性的而不是相对布局所以我改变它并且它做了同样的事情。我的代码在下面,对不起它很长但我不确定哪些位可以被排除,所以你已经得到了所有。 我感谢任何帮助:)

    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 个答案:

答案 0 :(得分:2)

将以下行移至else条件

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

原因: 您正在添加条件

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

您只能在其他情况下设置文字。因此,一旦它进入if条件,该行就为空。

注意:

每当你向listView添加项目时,只考虑if和else部分。这是最常见的问题。

答案 1 :(得分:1)

您的代码是:

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

将其更改为:

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

        }

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

答案 2 :(得分:0)

在您的ApplianceListAdapter适配器中进行以下更改:

if(){

// your code

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

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

在if else阻止之后设置描述文本。 希望这有效。