listview项只显示一个textview data-android

时间:2017-02-12 12:57:15

标签: android listview

我创建了一个列表视图,其中包含1个图像和2个文本。 但在输出中,列表视图仅显示第二个textview数据。第一个textview是空白的。

这是我的listview的单项布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal"
android:background="#ffffff">

<!-- icon -->
<ImageView
    android:id="@+id/item_icon"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="10dp"
    android:src="@drawable/icon"
    />

<!-- title -->
<TextView
    android:id="@+id/item_title"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:text="hello"
    android:layout_marginTop="13dp"
    android:textSize="18dp" />

<TextView
    android:id="@+id/item_counter"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="10dp"
    android:text="1"
    android:background="@drawable/counter"
    android:gravity="center"
    android:textColor="#FFFFFF"
    android:textSize="12sp"
    android:textStyle="bold" />

<!-- counter -->
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow"
    android:layout_centerVertical="true"
    android:layout_marginTop="14dp"
    android:id="@+id/arrow"/>

这是数据模型代码:

    public class DataModel {

    String name;
    String counter;

    public DataModel(String name,String counter ) {
        this.name=name;
        this.counter=counter;
    }

    public String getName() { return name;}

    public String getCounter() {
        return counter;
    }
}

这里是CustomAdapter.java

    public class CustomAdapter extends ArrayAdapter<DataModel> {

    private ArrayList<DataModel> dataSet;
    Context mContext;

    // View lookup cache
    private static class ViewHolder {
        TextView txtName;
        TextView txtCounter;
        ImageView icon;
    }

    public CustomAdapter(ArrayList<DataModel> data, Context context) {
        super(context, R.layout.category_list, data);
        this.dataSet = data;
        this.mContext=context;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        DataModel dataModel = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag

        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.category_list, parent, false);
            viewHolder.txtName = (TextView) convertView.findViewById(R.id.item_title);
            viewHolder.txtCounter = (TextView) convertView.findViewById(R.id.item_counter);
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.item_icon);
            convertView.setTag(viewHolder);
        } 
        else 
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txtName.setText(dataModel.getName());
        viewHolder.txtCounter.setText(dataModel.getCounter());
        // Return the completed view to render on screen
        return convertView;
    }
}

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById();
    clickListeners();


    listView=(ListView)findViewById(R.id.caregoryList);

    dataModels= new ArrayList<>();


    dataModels.add(new DataModel("Banana Bread", " 9"));
    dataModels.add(new DataModel("Cupcake", "9"));
    dataModels.add(new DataModel("Donut","9"));
    dataModels.add(new DataModel("Eclair", "9"));
    dataModels.add(new DataModel("Froyo", "0"));
    dataModels.add(new DataModel("Gingerbread", "0"));
    dataModels.add(new DataModel("Honeycomb","1"));
    dataModels.add(new DataModel("Ice Cream Sandwich", "1"));
    dataModels.add(new DataModel("Jelly Bean", "2"));
    dataModels.add(new DataModel("Kitkat", "3"));
    dataModels.add(new DataModel("Lollipop","4"));



    adapter= new CustomAdapter(dataModels,getApplicationContext());

    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            DataModel dataModel= dataModels.get(position);
          Snackbar.make(view, dataModel.getName()+"\n"+dataModel.getName()+" API: "+dataModel.getCounter(), Snackbar.LENGTH_LONG)
                    .setAction("No action", null).show();
        }
    });

所以,运行此代码后,listview只显示第二个计数器文本。第一个textview显示为空白。

2 个答案:

答案 0 :(得分:0)

我对您的XML进行了一些更改,复制并粘贴它并尝试:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal"
android:background="#ffffff">

<!-- icon -->
<ImageView
    android:id="@+id/item_icon"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="10dp"
    android:src="@drawable/icon"
    />

<!-- title -->
<TextView
    android:id="@+id/item_title"
    android:layout_width="32dp"
    android:layout_weight="1"
    android:layout_height="32dp"
    android:text="hello"
    android:layout_marginTop="13dp"
    android:textSize="18dp" />

<TextView
    android:id="@+id/item_counter"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="10dp"
    android:text="1"
    android:background="@drawable/counter"
    android:gravity="center"
    android:textColor="#FFFFFF"
    android:textSize="12sp"
    android:textStyle="bold" />

<!-- counter -->
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow"
    android:layout_centerVertical="true"
    android:layout_marginTop="14dp"
    android:id="@+id/arrow"/>
<LinearLayout/>

答案 1 :(得分:0)

对于这种方式,你的xml和代码几乎没有问题,你只是缺少textColor属性。文本是默认的白色(可能与您选择的主题相关)。 只需在textView item_title中添加一种颜色,例如:

android:textColor="@android:color/holo_orange_dark"

你会看到文字。