抄襲別人的代碼,我只有一半的理解中,我已經成功地使一個列表視圖,其中的每個元素包含三個TextViews和一個複選框。使聽衆的按鈕一個ListView
的代碼涉及以下兩個XML文件。首先 「customrowview.xml」:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="142dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="@+id/editbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
然後 「customlistview.xml」:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000fff"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
<TextView android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFff00"
android:text="No data"
/>
</LinearLayout>
我得到通過訪問列表:
listView = (ListView) findViewById(R.id.mylist);
代碼還包括:
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.custom_row_view,
new String[] {"label","daysordate","time"},
new int[] {R.id.text1,R.id.text3, R.id.text2}
);
listView.setAdapter(adapter);
現在我想做的是索姆就像listView.setlistenerforbuttonwithinlist()!
但不能工作了如何做到這一點。我知道有過那麼之前對相關的問題,但我不明白:-(
編輯答案:看到azgolfers的答案後...我犯了一個自定義適配器如下:
public class myadapter extends SimpleAdapter
{
LayoutInflater mLayoutInflater;
public void myadapter(Context context)
{
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = null;
if (convertView != null)
{
view = convertView;
}
else
{
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
}
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
buttonEdit.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Log.i("xx","Button pressed!");
}
});
return super.getView(position, convertView, parent);
}
public myadapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to)
{
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
}
不幸的是這個崩潰在該行
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
有了一個空指針異常......不知道爲什麼:-(
null是不允許的,所以更改它太虛假了。但後來還是得到了在該行一個空指針異常:-( – Mick 2012-07-16 11:28:32
是的,當然虛假如果趕上NPE,我覺得LayoutInflater == NULL嘗試通過這條線附近等調用構造:。 LayoutInflater infalter =(LayoutInflater) \t \t \t \t \t的getContext()。getSystemService(上下文。LAYOUT_INFLATER_SERVICE); \t \t \t convertView = infalter.inflate(R.layout.choose_color_listview_item,parent,false); – 2012-07-16 11:35:05
「我認爲LayoutInflater == null」是的,你是對的。它讓我更早地發現了一個問題。謝謝你的幫助。 – Mick 2012-07-16 11:48:18