0

我尝试使用 CustomAdapter 创建一个简单的 ListView,但 ListView 不显示任何元素。我尝试更改背景颜色并且它正确更改......所以不是 ListView 没有显示,而是我认为的元素。

任何帮助都会很好,我已经搜索了几个小时有什么问题......

有我的代码:

activity_main.xml

<LinearLayout 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:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >
    </ListView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/lblNFC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lblNFC_text" />

        <!-- Read Only -->

        <EditText
            android:id="@+id/editTextNFC"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:cursorVisible="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:hint="Click scan to search NFC"
            android:inputType="none" />

        <Button
            android:id="@+id/btnFindNFC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="ButtonOnClick"
            android:text="@string/btnFindNFC_text" />
    </LinearLayout>

</LinearLayout>

list_item_var.xml

<?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="match_parent"
    android:orientation="horizontal" >

        <TextView
            android:id="@+id/var"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="18dp"
            android:textStyle="bold"
            android:typeface="serif" >

            </TextView>

            <TextView
                android:id="@+id/value"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="18dp"
                android:textStyle="bold"
                android:typeface="serif" >
            </TextView>

    </LinearLayout>

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {

    private ArrayList<VarDetails> _data;
    Context _c;

    CustomAdapter(ArrayList<VarDetails> data, Context c) {
        _data = data;
        _c = c;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return _data.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return _data.get(position);
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) _c
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item_var, null);
        }

        TextView varView = (TextView) v.findViewById(R.id.var);
        TextView valueView = (TextView) v.findViewById(R.id.value);

        VarDetails var = _data.get(position);
        varView.setText(var.var);
        valueView.setText("Value: " + var.value);

        return v;
    }

}

VarActivity.java

public class VarActivity extends Activity {
    ListView varList;
    ArrayList<VarDetails> details;
    AdapterView.AdapterContextMenuInfo info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        varList = (ListView) findViewById(R.id.list);

        details = new ArrayList<VarDetails>();

        VarDetails Detail;

        Detail = new VarDetails();
        Detail.setVar("Var1");
        Detail.setValue("Value1");
        details.add(Detail);

        Detail = new VarDetails();
        Detail.setVar("Var2");
        Detail.setValue("Value2");
        details.add(Detail);

        Detail = new VarDetails();
        Detail.setVar("Var3");
        Detail.setValue("Value3");
        details.add(Detail);

        varList.setAdapter(new CustomAdapter(details, this));
    }
}

VarDetails.java

public class VarDetails {

    String var;
    String value;

    public String getVar() {
        return var;
    }

    public void setVar(String var) {
        this.var = var;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}
4

3 回答 3

0

更改 list_item_var.xml

<?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="match_parent"
    android:orientation="horizontal" >

<TextView
    android:id="@+id/var"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textSize="18dp"
    android:textStyle="bold"
    android:typeface="serif" >
</TextView>

<TextView
    android:id="@+id/value"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textSize="18dp"
    android:textStyle="bold"
    android:typeface="serif" >
</TextView>

</LinearLayout>
于 2013-04-24T07:53:07.203 回答
0

您应该以某种方式将 VarDetail 的数据与自定义适配器中的布局元素绑定

v = vi.inflate(R.layout.list_item_var, null);

例如,您可以尝试

TextView varView = (TextView) v.findViewById(R.id.var);
varView.setText(_data.get(position).getVar());
于 2013-04-23T18:08:10.573 回答
0

list_item_var.xml 中的 LinearLayout 有问题。方向是水平的,所有子文本视图都有 layout_width="match_parent",因此文本视图将被推出视图,因为第一个视图已扩展所有宽度空间。尝试将它们设置为“wrap_content”。如果你的 textview 有不同的比例,还要设置 layout_weight。

于 2013-04-23T18:40:06.570 回答