我尝试使用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;
}
}
答案 0 :(得分: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());
答案 1 :(得分:0)
list_item_var.xml中的LinearLayout出了问题。方向是水平的,所有子文本视图都有layout_width =&#34; match_parent&#34;,因此文本视图将被推出视图,因为第一个已扩展到所有宽度空间。尝试将它们设置为&#34; wrap_content&#34;。如果textview具有不同的比例,还要设置layout_weight。
答案 2 :(得分: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>