gpt4 book ai didi

android - 当我单击 ListView 中的小部件时,如何使用 ResourceCursorAdapter 获取 View 项

转载 作者:行者123 更新时间:2023-11-30 04:23:06 27 4
gpt4 key购买 nike

我有几天以来一直困扰着我的这个特殊问题。我使用了搜索但找不到任何对我的问题有帮助的文章。我想这也是我搜索的问题。然而,回到我的问题。

我使用 ResourceCursorAdapter 来填充我的 ListView。
我的 ListView 项目看起来像..

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/title_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/description_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/download_cb"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/some_bt"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/pdf_ib"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/download_pb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="10dip" />
</RelativeLayout>

我的适配器:

private final class ListAdapter extends ResourceCursorAdapter implements
DownloadCallback, OnItemClickListener, OnClickListener {

public ListAdapter(Context context, int layout, Cursor c) {
super(context, layout, c);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
final ItemHolder holder = (ItemHolder) view.getTag();

holder.url = cursor.getString(..);

holder.titleTv.setText(cursor.getString(..));
holder.descriptionTv.setText(cursor.getString(..));
...

if(cursor.getInt(..) == 1) {
holder.progress.setVisible(View.GONE);
}

if(cursor.getInt(...) == 1) {
holder.downloadCb.setEnabled(isConnectedToNet);
}
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = super.newView(context, cursor, parent);
ItemHolder holder = new ItemHolder();

view.setOnClickListener(this);

holder.titleTv = (TextView) view.findViewById(R.id.title_tv);
holder.descriptionTv = (TextView) view.findViewById(R.id.description_tv);
...

holder.downloadCb.setEnabled(isConnectedToNet);
holder.downloadCb.setOnClickListener(this);
holder.downloadCb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
prepare2Download();

DownItem di = new DownItem();
di.url = holder.url; /// <========= to do this

// start async downloader and notify progressbar "holder.downPb"
Download serverTaskObject = new Download();
serverTaskObject.execute(currDownholder.copy());

holder.downPb.setVisibility(View.GONE); // <======== and this.
}
});

holder.pdfIb = (ImageButton) view.findViewById(R.id.pdf_ib);
holder.pdfIb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// start new activity and load website.
openUrl(holder.url); // <============ or this.
}
});

holder.downPb = (ProgressBar) view.findViewById(R.id.pb_law_download);
holder.downPb.setVisibility(View.GONE);

view.setTag(cachedItem);
return view;
}

...

private static class LawItemCache {
TextView titleTv;
TextView descriptionTv;
...

CheckBox downloadCb;
ImageButton pdfIb;

ProgressBar downPb;

String url;
IProgressListener listener;
}
}

我的问题是..当我单击 ListView 中的 Button 或 Checkbox 时,我想在特定的 View holder 中进行一些更改,例如使 Progressbar 可见或消失。我还不知道如何在我的适配器中获取 View holder 以进行更改。

我希望我解释得足够好,能够切入我的问题所在。

最佳答案

我为此苦苦挣扎,终于成功了!我的行中有一个复选框,并且正在使用 ResourceCursorAdapter。诀窍是意识到复选框单击处理程序需要在 getView() 或 newView() 中绑定(bind),而 holder 需要在 bindView() 中关联。这样,ListView 中当前显示的行始终具有正确的持有人信息。这是我的 bindView 和 getView 方法:

 public void bindView(View view, Context context, Cursor cursor) {
Log.i(CN,"bindView");
TextView todoTxt = (TextView)view.findViewById(R.id.todoItemText);
Log.i(CN,"bindView set txt");
TextView todoTitle = (TextView)view.findViewById(R.id.todoItemTitle);
Log.i(CN,"bindView set title");
CheckBox cBox = (CheckBox)view.findViewById(R.id.todoChecked);
Log.i(CN,"bindView set check");

IOweRowTag tag=(IOweRowTag)cBox.getTag();
int pos=cursor.getInt(cursor.getColumnIndex(Todos._ID));
Log.i(CN,"getView: no tag on "+pos);
tag=new IOweRowTag();
tag.id=pos;
cBox.setTag(tag);

// IOweRowTag irt=(IOweRowTag)view.getTag();
// if(irt!=null)
// Log.i(CN,"Got irt id="+irt.id);
// else
// Log.i(CN,"No id");
//
Log.i(CN,"got title="+cursor.getString(cursor.getColumnIndex(Todos.TITLE)));
Log.i(CN,"got txt="+cursor.getString(cursor.getColumnIndex(Todos.TEXT)));

todoTitle.setText(cursor.getString(cursor.getColumnIndex(Todos.TITLE)));
todoTxt.setText(cursor.getString(cursor.getColumnIndex(Todos.TEXT)));
cBox.setChecked(true);

}




public View getView (int position, View convertView, ViewGroup parent) {
View tmpView=super.getView(position, convertView, parent);
Log.i(CN,"getView:"+position);
final CheckBox cBox = (CheckBox) tmpView.findViewById(R.id.todoChecked);
IOweRowTag tag=(IOweRowTag)cBox.getTag();
cBox.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
IOweRowTag tag=(IOweRowTag)v.getTag();
if(tag==null)
Log.i(CN,"checkbox clicked no tag");
else
Log.i(CN,"checkbox clicked tag="+tag.id);
if(cBox.isChecked()) {
Log.i(CN," Checked!");
// do some operations here
}
else{
Log.i(CN,"NOT Checked!");
// do some operations here
}
}
});


return tmpView;
}

希望对你有帮助!

关于android - 当我单击 ListView 中的小部件时,如何使用 ResourceCursorAdapter 获取 View 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8972821/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com