Android 选中简单光标适配器项的列表视图在滚动期间取消选中
我有一个问题,我找不到答案,所以-所有的问题都解决了,所以有太多不同的问题,并没有工作。 问题是-Android 选中简单光标适配器项的列表视图在滚动期间取消选中,android,listview,cursor,adapter,Android,Listview,Cursor,Adapter,我有一个问题,我找不到答案,所以-所有的问题都解决了,所以有太多不同的问题,并没有工作。 问题是- 1。在ListView中单击的行(单击后,它们被标记为蓝色或背景,并勾选了复选框)在滚动后会失去它们被单击的标记(勾选了复选框和颜色)。 2。甚至更多-滚动期间,即使未单击,也会标记其他行。 ListView绑定到从SQLite数据库获取数据的自定义简单游标适配器。 listview的每一行都有许多带有文本框和一个复选框的列-因此listview中的默认多选模式选择在我的情况下不起作用-我使用on
1。在ListView中单击的行(单击后,它们被标记为蓝色或背景,并勾选了复选框)在滚动后会失去它们被单击的标记(勾选了复选框和颜色)。
2。甚至更多-滚动期间,即使未单击,也会标记其他行。
ListView绑定到从SQLite数据库获取数据的自定义简单游标适配器。 listview的每一行都有许多带有文本框和一个复选框的列-因此listview中的默认多选模式选择在我的情况下不起作用-我使用onItemClick来获取所选项目。在ListView活动中,我自己在列表中处理选中的项目。 ListView也位于水平滚动视图中 以下是我的类和布局: 抗糜烂活性
public class MyListActivity extends ListActivity
{ .....
public void initComponents()
{
baza = dbMan.openDB(true);
cursorListaFaktur = wyswietlWszystkieTowary();
startManagingCursor(cursorListaFaktur);
fakturyLista = this.getListView();
fakturyLista.setScrollContainer(true);
fakturyLista.setScrollingCacheEnabled(true);
fakturyLista.setItemsCanFocus(false);
fakturyLista.setTextFilterEnabled(true);
fakturyLista.setItemChecked(0, true);
String[] columns = new String[] { "NRDOK", "NAZWADOK", "ANULOWANY",
"NAZWASKR", "DATA", "SUMA", "MIEJSCOWOSC", "TERMIN",
"WYEKSPORTOWANY", "MSPRZ_DOKUMENT.UWAGI" };
int[] kontrolki = new int[] { R.id.ID_FakturyRow_NrFakt,
R.id.ID_FakturyRow_Typ, R.id.ID_FakturyRow_Anulowany,
R.id.ID_FakturyRow_DataSprzed,
R.id.ID_FakturyRow_Kontrahent,
R.id.ID_FakturyRow_Suma, R.id.ID_FakturyRow_Miejscowosc,
R.id.ID_FakturyRow_Termin, R.id.ID_FakturyRow_Wyeksp,
R.id.ID_FakturyRow_Uwagi };
String[] columnsRaportKasowy = new String[] { "NRDOK", "NAZWADOK",
"GOTOWKA", "GOTOWKA", "NAZWASKR", "DATA", "UWAGI" };
int[] kontrolkiRaportKasowy = new int[] {
R.id.ID_RaportKasowyRow_NrFakt,
R.id.ID_RaportKasowyRow_Typ,
R.id.ID_RaportKasowyRow_Wplata,
R.id.ID_RaportKasowyRow_Wyplata,
R.id.ID_RaportKasowyRow_Kontrahent,
R.id.ID_RaportKasowyRow_DataSprzed,
R.id.ID_RaportKasowyRow_Uwagi };
if (raportKasowy)
{
adapterFaktury = new MSprzedazRaportKasowyAdapter(this,
R.layout.raportkasowy_row, cursorListaFaktur,
columnsRaportKasowy, kontrolkiRaportKasowy);
}
else
{
adapterFaktury = new
MSprzedazListViewAdapter(this,R.layout.faktury_row,
cursorListaFaktur, columns, kontrolki);
}
fakturyLista.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
LinearLayout linLay = (LinearLayout) arg1;
ArrayList<View> kontrolki = arg1.getTouchables();
CheckBox chBox = (CheckBox) kontrolki.get(0);
chBox.setChecked(!chBox.isChecked());
Cursor cursor = adapterFaktury.getCursor();
cursor.moveToPosition(arg2);
String tekstKom = "";
if(MSprzedazUtils.znajdzIdWliscieZazn(
cursor.getInt(cursor.getColumnIndex("_id"))
, listaZaznaczonych) == null)
{
listaZaznaczonych.add(cursor.getInt(cursor.getColumnIndex("_id")));
linLay.setBackgroundColor(Color.rgb(110, 151, 186));
MSprzedazUtils.wpisDoLoga(tekstKom);
}
else
{
try
{
listaZaznaczonych.remove((int)
MSprzedazUtils.znajdzIdWliscieZazn(
cursor.getInt(cursor.getColumnIndex("_id")),
listaZaznaczonych));
MSprzedazUtils.wpisDoLoga(tekstKom);
}
catch(IndexOutOfBoundsException ex)
{
MSprzedazUtils.wpisDoLoga("Błąd"+tekstKom);
}
}
}
});
公共类MyListActivity扩展了ListActivity
{ .....
公共组件()
{
baza=dbMan.openDB(true);
cursorListaFaktur=wyswietlWszystkieTowary();
startManagingCursor(cursorListaFaktur);
fakturyLista=this.getListView();
fakturyLista.setScrollContainer(真);
fakturyLista.setScrolingCacheEnabled(真);
fakturyLista.setItemsCanFocus(false);
fakturyLista.setTextFilterEnabled(true);
fakturyLista.setItemChecked(0,true);
字符串[]列=新字符串[]{“NRDOK”、“NAZWADOK”、“ANULOWANY”,
“NAZWASKR”、“DATA”、“SUMA”、“MIEJSCOWOSC”、“Term”,
“WYEKSPORTOWANY”、“MSPRZ_DOKUMENT.UWAGI”};
int[]kontrolki=新int[]{R.id.id_FakturyRow_NrFakt,
R.id.id_FakturyRow_Typ,R.id.id_FakturyRow_Anulowany,
R.id.id_FakturyRow_数据处理,
R.id.id_FakturyRow_Kontrahent,
R.id.id_FakturyRow_Suma,R.id.id_FakturyRow_Miejscowosc,
R.id.id_FakturyRow_Termin,R.id.id_FakturyRow_Wyeksp,
R.id.id_FakturyRow_Uwagi};
String[]columnsRaportKasowy=新字符串[]{“NRDOK”,“NAZWADOK”,
“GOTOWKA”、“GOTOWKA”、“NAZWASKR”、“DATA”、“UWAGI”};
int[]kontrolkiRaportKasowy=新int[]{
R.id.id_raportkasowirow_NrFakt,
R.id.id_RaportKasowyRow_类型,
R.id.id_raportkasowirow_Wplata,
R.id.id_RaportKasowyRow_Wyplata,
R.id.id_raportkasowirow_Kontrahent,
R.id.id_RaportKasowyRow_数据处理,
R.id.id_RaportKasowyRow_Uwagi};
如果(raportKasowy)
{
adapterFaktury=新的MSprzedazRaportKasowyAdapter(此,
R.layout.raportkasowy_row,cursorListaFaktur,
专栏作家Raport Kasowy、kontrolkiRaportKasowy);
}
其他的
{
adapterFaktury=新
MSprzedazListViewAdapter(此,R.layout.faktury_行,
cursorListaFaktur、columns、kontrolki);
}
setOnItemClickListener(新的OnItemClickListener(){
公共链接(AdapterView arg0、视图arg1、,
整数arg2,长arg3)
{
LinearLayout linLay=(LinearLayout)arg1;
ArrayList kontrolki=arg1.getTouchables();
复选框chBox=(复选框)kontrolki.get(0);
chBox.setChecked(!chBox.isChecked());
Cursor Cursor=adapterFaktury.getCursor();
光标移动位置(arg2);
字符串tekstKom=“”;
如果(MSprzedazUtils.znajdzidwlisciezazin(
cursor.getInt(cursor.getColumnIndex(“\u id”))
,listaZaznaczonych)==null)
{
add(cursor.getInt(cursor.getColumnIndex(“_id”));
立根基色(Color.rgb(110151186));
MSprzedazUtils.wpisDoLoga(tekstKom);
}
其他的
{
尝试
{
listaZaznaczonych.remove((内部)
MSprzedazUtils.znajdzIdWliscieZazn(
cursor.getInt(cursor.getColumnIndex(“\u id”),
listaZaznaczonych);
MSprzedazUtils.wpisDoLoga(tekstKom);
}
捕获(IndexOutOfBoundsException ex)
{
wpisDoLoga(“Błd”+技术公司);
}
}
}
});
MySimpleCursorAdapter
public class MySimpleCursorAdapter extends SimpleCursorAdapter
{
class ViewHolder
{
CheckBox chckBoxZazn;
LinearLayout linLay;
TextView nrDok;
TextView typDok;
boolean isSelected;
TextView txtData;
TextView txtKontrahent;
TextView txtSuma;
TextView txtAnulowany;
TextView txtMiejscowosc;
TextView txtTermin;
TextView txtWyeksp;
TextView txtUwagi;
}
@Override
public void bindView(View convertView, Context context, Cursor cursor)
{
if (convertView.getTag() != null)
holder = (ViewHolder) convertView.getTag();
{
// holder.linLay = (LinearLayout)convertView;
holder.chckBoxZazn = (CheckBox) convertView.findViewById(R.id.ID_FakturyRow_ChckBoxFakturyWybor);
holder.nrDok = (TextView) convertView.findViewById(R.id.ID_FakturyRow_NrFakt);
holder.typDok = (TextView) convertView.findViewById(R.id.ID_FakturyRow_Typ);
holder.txtAnulowany = (TextView) convertView.findViewById(R.id.ID_FakturyRow_Anulowany);
holder.txtData = (TextView) convertView.findViewById(R.id.ID_FakturyRow_Kontrahent);
holder.txtKontrahent = (TextView) convertView.findViewById(R.id.ID_FakturyRow_DataSprzed);
holder.txtMiejscowosc = (TextView) convertView.findViewById(R.id.ID_FakturyRow_Miejscowosc);
holder.txtSuma = (TextView) convertView.findViewById(R.id.ID_FakturyRow_Suma);
holder.txtTermin = (TextView) convertView.findViewById(R.id.ID_FakturyRow_Termin);
holder.txtWyeksp = (TextView) convertView.findViewById(R.id.ID_FakturyRow_Wyeksp);
holder.txtUwagi = (TextView) convertView.findViewById(R.id.ID_FakturyRow_Uwagi);
}
holder.chckBoxZazn.setTag(cursor.getPosition());
holder.nrDok.setText(cursor.getString(cursor.getColumnIndex("NRDOK")));
holder.typDok.setText(cursor.getString(cursor.getColumnIndex("NAZWADOK")));
if (cursor.getString(cursor.getColumnIndex("DATA")) != null)
holder.txtData.setText(cursor.getString(cursor.getColumnIndex("DATA")).substring(0, 10));
else
holder.txtData.setText("-");
if (cursor.getInt(cursor.getColumnIndex("ANULOWANY")) == 0)
holder.txtAnulowany.setText("NIE");
else
holder.txtAnulowany.setText("TAK");
if (cursor.getString(cursor.getColumnIndex("NAZWASKR")) != null)
holder.txtKontrahent.setText(cursor.getString(cursor.getColumnIndex("NAZWASKR")));
else
holder.txtKontrahent.setText("-");
if (cursor.getString(cursor.getColumnIndex("MIEJSCOWOSC")) != null)
holder.txtMiejscowosc.setText(cursor.getString(cursor.getColumnIndex("MIEJSCOWOSC")));
else
holder.txtMiejscowosc.setText("-");
holder.txtSuma.setText(cursor.getString(cursor.getColumnIndex("SUMA")));
if (cursor.getInt(cursor.getColumnIndex("WYEKSPORTOWANY")) == 0)
{
holder.txtWyeksp.setText("NIE");
holder.txtWyeksp.setBackgroundColor(Color.BLACK);
}
else
{
holder.txtWyeksp.setText("TAK");
convertView.setBackgroundColor(Color.DKGRAY);
}
if (cursor.getString(cursor.getColumnIndex("TERMIN")) != null)
holder.txtTermin.setText(cursor.getString(cursor.getColumnIndex("TERMIN")).substring(0, 10));
else
holder.txtTermin.setText("-");
holder.txtUwagi.setText(cursor.getString(cursor.getColumnIndex("UWAGI")));
if (cursor.getCount() > 0 && cursor.getPosition() < cursor.getColumnCount())
holder.chckBoxZazn.setChecked(jestZaznaczony[cursor.getPosition()]);
}
@Override
public void onClick(View v)
{
if (v.isFocusable())
{
jestZaznaczony[cursor.getPosition()] = !jestZaznaczony[cursor.getPosition()];
holder.chckBoxZazn.setChecked(jestZaznaczony[cursor.getPosition()]);
}
}
}
公共类MySimpleCursorAdapter扩展了SimpleCursorAdapter
{
类视图持有者
{
复选框chckBoxZazn;
线性布局;
文本视图nrDok;
文本视图typDok;
他当选了;
TextView txtData;
TextView txtKontrahent;
TextView-txtSuma;
TextView txtAnulowany;
TextView-txtMiejscowosc;
文本视图;
TextView-txtWyeksp;
TextView-txtawagi;
}
@凌驾
公共void bindView(视图转换视图、上下文上下文、光标)
{
if(convertView.getTag()!=null)
holder=(ViewHolder)convertView.getTag();
{
//holder.linLay=(LinearLayout)convertView;
holder.chckBoxZazn=(复选框)convertView.findViewById(R.id.id\u FakturyRow\u chckboxfakturyBor);
holder.nrDok=(TextView)convertVi
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ID_FakturyRow_KontrRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/ID_FakturyRow_ChckBoxFakturyWybor"
android:layout_width="30dp"
android:layout_height="30dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="10dp"
true="" />
<TextView
android:id="@+id/ID_FakturyRow_NrFakt"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="5dp"
android:focusable="false"
android:maxLength="10"
android:maxLines="1"
android:text="NrFakt"
android:textAppearance="?android:attr/textAppearanceSmall"
true="" />
<TextView
android:id="@+id/ID_FakturyRow_Typ"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="5dp"
android:focusable="false"
android:maxLength="4"
android:maxLines="1"
android:text="Typ"
android:textAppearance="?android:attr/textAppearanceSmall"
true="" />
<TextView
android:id="@+id/ID_FakturyRow_Anulowany"
android:layout_width="46dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:maxLength="8"
android:maxLines="1"
android:text="Anulowany"
android:textAppearance="?android:attr/textAppearanceSmall"
android:focusable="false" />
<TextView
android:id="@+id/ID_FakturyRow_Kontrahent"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="10dp"
android:maxLength="14"
android:maxLines="1"
android:text="Kontrahent"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/ID_FakturyRow_DataSprzed"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="10dp"
android:text="Data"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/ID_FakturyRow_Suma"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="0dp"
android:maxLines="1"
android:text="Suma"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/ID_FakturyRow_Miejscowosc"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="0dp"
android:text="Miejscowosc"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/ID_FakturyRow_Termin"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginRight="25dp"
android:maxLength="18"
android:maxLines="1"
android:text="Termin"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/ID_FakturyRow_Wyeksp"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginRight="0dp"
android:maxLength="16"
android:maxLines="1"
android:text="Wyeksp"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/ID_FakturyRow_Uwagi"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="0dp"
android:text="Termin"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall" />
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:fillViewport="true" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ID_FakturyListaButtons_HeaderLayoutWrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:orientation="vertical" >
....... // Text Views as Listview header
</LinearLayout>
<RelativeLayout
android:id="@+id/ID_FakturyListaButtons_TabListaLayout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</RelativeLayout>
</LinearLayout>
</HorizontalScrollView>
if (enabledItems.contains(eventTime)) {
viewHolder.getCheckBox().setChecked(true);
} else {
viewHolder.getCheckBox().setChecked(false);
}
public class ButtonCursorAdapter extends SimpleCursorAdapter {
private Cursor c;
private Context context;
private Activity activity;
public static int[] chkState;
private int layout;
public ButtonCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
this.context = context;
this.activity = (Activity) context;
this.layout = layout;
// create an array to hold checked state, use zero for un-checked and 1 for checked
chkState = new int[c.getCount()]; // Set array size to # of elements in cursor
for(int i=1; i<c.getCount(); i++){ // loop through array
chkState[i] = 0; // set initial state to 0 - not checked
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = View.inflate(context, layout, null);
final int pos = position;
View row = convertView;
c.moveToPosition(position);
// do your data assignement stuff here
// set the state of your checkbox
if (chkState[position] == 0) {
// set the checkbox to checked
} else {
// set the checkbox to unchecked
}
return (row);
}
}