我希望對此問題有所瞭解。 我是這個ListView的新手。我設法從數據庫中輸入數據,並將列表項的標識設置爲數據庫記錄的標識。Android自定義ListView與TableRow之內。 setOnclickListener?
對佈局使用android.R.layout.simple_list_item_1和我的文本所在的TextView使用android.R.id.text1時,一切正常。 我甚至設置了setOnItemClickListener並得到了一個Toast whit正確的數據(項目位置和我設置的_id)。
比我想使用TableRow或TableLayout作爲列表項的自定義佈局。 這有效,但我不知道如何獲取TableRow中的視圖引用(即TableRow本身)?
setOnItemClickListener停止工作,我無法在該行上使用setOnclickListener。
如果我想獲得ListView項目的POSITION和ID,還是應該使用更智能的東西,是否有可能以某種方式使用這種技術?
這是我的代碼:
public class ListView1 extends ListActivity {
private DBAdapter db;
private Cursor getMatchigItems;
private OnItemClickListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
db = new DBAdapter(this);
db.open();
getMatchigItems = db.getMatchingItems(4, 1);
startManagingCursor(getMatchigItems);
// SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
// android.R.layout.simple_list_item_1,
// getMatchigItems,
// new String[]{db.KEYITEMS_TXTITEMNAME, db.KEYITEMS_ROWID},
// new int[]{android.R.id.text1});
// setListAdapter(adapter);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.table_row,
getMatchigItems,
new String[]{db.KEYITEMS_TXTITEMNAME, db.KEYITEMS_ROWID},
new int[]{R.id.textViewItems1});
setListAdapter(adapter);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(listener);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "id: " + String.valueOf(id) + "\npos: " + String.valueOf(position), Toast.LENGTH_SHORT).show();
};
});
}
}
而這正是佈局的TableRow我想用:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableRowLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow
android:id="@+id/tableRowLayoutRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rowcat"
android:layout_marginTop="2dp"
android:minHeight="40dp" android:gravity="center_vertical">
<View android:id="@+id/imagePlaceholderBlank1Items"
android:layout_width="40dp"
android:minHeight="40dp" android:layout_height="fill_parent" android:background="@drawable/editicon"/>
<TextView android:id="@+id/textViewItems1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tule bo ime artikla" style="@style/whiteText" android:layout_marginLeft="10dp" android:ellipsize="marquee" android:lines="1" android:focusable="true" android:width="220dp" android:scrollHorizontally="true" android:marqueeRepeatLimit="marquee_forever" android:focusableInTouchMode="true"/>
<TextView android:id="@+id/imagePlaceholderButton1Items"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:minHeight="40dp"
android:gravity="center" android:textColor="#FFF" android:background="@drawable/buttonoff" android:tag="off"/>
</TableRow>
</TableLayout>
提前任何建議謝謝!
你爲什麼使用TableLayout?使用水平LinearLayout可以更有效地實現您的佈局。 –
是的,我認爲是。這個故事是我正在爲表中的某些錶行充氣。當我完成這個項目時,我發現Android需要花費大量的時間和精力才能充滿大約15行,所以現在我想保留儘可能多的工作,但是看起來我不會:)有可能在列表視圖項中點擊圖片時發生變化? – PacQ