【发布时间】:2014-03-28 04:35:29
【问题描述】:
这是我在这里的第一篇文章,但我多年来一直在阅读这个问答,我总是能找到答案,但这次我找不到它,或者我无法将多个答案与我的问题结合起来。我希望你能帮忙。
所以我有覆盖 onCreateView 的片段
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
db = new WineryDatabaseAdapter(getActivity());
bacveList = db.getBacve();
v = inflater.inflate(R.layout.sve_bacve, container, false);
Log.v("onCreateView", "oncreateview");
return v;
}
然后我就有了设置数据的方法
public void getBacveItems(){
ArrayAdapter<Bacve> ad = new BacveListAdapter(getActivity(), bacveList);
lv = (ListView) v.findViewById(R.id.listSveBacve);
lv.setAdapter(ad);
lv.setOnItemClickListener(this);
Log.v("getBacveItems", "getBacveItems");
}
在那个方法中我调用了我的适配器,所以我可以使用我的列表视图布局
public class BacveListAdapter extends ArrayAdapter<Bacve>{
List<Bacve> bacve;
Context c;
String[] values = new String[] { "prva","druga" };
public BacveListAdapter(Context c,List<Bacve> l){
//super(c,R.layout.sve_bacve_item,l);
super(c,R.layout.sve_bacve_item,l);
Log.v("BacveListAdapter", "BacveListAdapter");
this.bacve = l;
this.c = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View iv = convertView;
TextView tv;
if (iv == null){
iv = (View)((LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.sve_bacve,parent,false);
tv = new TextView(c);
}
else{
Bacve bacva = bacve.get(position);
tv = (TextView) iv.findViewById(R.id.textNazivBacve);
tv.setText(bacva.getIme());
}
return iv;
}
}
但由于某种原因,我在尝试访问电视时遇到了错误。它始终为空。
我认为它与创建/获取的视图有关,并且我正在创建/获取更高层次结构中的一些父视图。我试图调试它,但不知道如何在调试模式下使用这些 ID。好吧不是如何使用它,而是如何比较它,以便我看到它是否正确。
请帮忙:)
我在这次编辑中这样做了,但没有。是因为我使用的是滑动视图,而我一开始就看错了吗?
这是我的 sve_bacve_item,它有 textview。 我对@Raghunandan 解决方案很友好,但现在我在将字符串转换为持有人时遇到了问题。我正在努力解决这个问题。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textNazivBacve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="@+id/bacveIcon"
android:text="@string/BacveNaziv"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/bacveIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/textNazivBacve"
android:adjustViewBounds="true"
android:contentDescription="@string/Bacve"
android:maxHeight="120dp"
android:maxWidth="120dp"
android:src="@drawable/bacve_item" />
</RelativeLayout>
BR
【问题讨论】:
-
为什么 iv == null?也许是个愚蠢的问题,但他不应该膨胀 R.layout.sve_bacve 吗?
-
当它为空时你会膨胀,当你膨胀布局时你需要初始化视图。阅读stackoverflow.com/questions/11945563/…
-
请发布 R.layout.sve_bacve_item
-
@Veki 你在片段中膨胀
sve_bacve.xml并且你在适配器的 getView 中膨胀 ame 布局