gpt4 book ai didi

android - 无法更新 RecyclerView 中的 TextView 字段

转载 作者:太空宇宙 更新时间:2023-11-03 10:18:37 33 4
gpt4 key购买 nike

我正在从 ListView 迁移到 RecyclerView,最后我遇到的问题是更新其中一个字段。我的应用程序有一个简单的列表,其中包含一些在互联网上找到的图片。当您选择一个时,图像会下载到手机上供以后离线查看。适配器从 SQLite 数据库获取数据,因此当您点击某些图像时,数据库会更新(文本从 “点击此处下载” 变为 “已下载”) 和 RecyclerView 应该跟在后面。

我在使用 ListView 时遇到了同样的问题,但每次 App 更新数据库时,我只是调用了 populateList();。我知道这不是理想的解决方案,但它确实有效。现在我想用 notifyDataSetChanged() 或更好的 notifyItemChanged(position) 来正确完成它,但我无法让它工作。

无论如何,这是代码,抱歉有点乱。这只是一个测试代码(RecyclerView 代码来自示例):

public class RecyclerFragment extends Fragment {

private imgAdapter mAdapter;
private DatabaseHandler db;
private ProgressDialog mProgressDialog;


public RecyclerFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {



db = new DatabaseHandler(getActivity());
List<Images> listdb = db.getImages();
mAdapter = new ImgAdapter(getActivity(), listdb);
db.close();

View view = inflater.inflate(R.layout.fragment_main, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(android.R.id.list);
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(
getActivity(), getResources().getInteger(R.integer.list_columns)));
recyclerView.setAdapter(mAdapter);
return view;
}


private class ImgAdapter extends RecyclerView.Adapter<ViewHolder>
implements ItemClickListener {

public List<Images> mList;
private Context mContext;

public ImgAdapter(Context context, List<Images> listdb) {
super();
mContext = context;
mList = listdb;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.list_row1, parent, false);
return new ViewHolder(view, this);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Images image = mList.get(position);
holder.mTitle.setText(image.getTitle());
holder.mDownloadStatus.setText(image.getDownloadStatus());
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public int getItemCount() {
return mList == null ? 0 : mList.size();
}


@Override
public void onItemClick(View view, int position) {
switch (position) {
case 0:
Log.v("POSITION 0:", " " + position);
break;
case 1:
/*
some logic nvm
*/
String imgurl = "http:/...";
String imagename = "Second image";
new GetImages(imgurl, imagename).execute();

/*
I've tried mAdapter.notitfy... , no luck aswell
This does nothing:
*/
notifyItemChanged(position);
notifyDataSetChanged();
break;
//....

}
}
}

private static class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {

TextView mTitle;
TextView mDownloadStatus;
ItemClickListener mItemClickListener;

public ViewHolder(View view, ItemClickListener itemClickListener) {
super(view);
mTitle = (TextView) view.findViewById(R.id.text1);
mDownloadStatus = (TextView) view.findViewById(R.id.text2);
mItemClickListener = itemClickListener;
view.setOnClickListener(this);
}

@Override
public void onClick(View v) {
mItemClickListener.onItemClick(v, getPosition());
}
}

interface ItemClickListener {
void onItemClick(View view, int position);
}

/*
This is my AsyncTask class used for downloading image and updating db
I removed some code just to to make it cleaner
*/
private class GetImages extends AsyncTask<Object, Object, Object> {
private final String requestUrl;
private final String imagename_;
private String imagename;
public int numberofbits;


private GetImages(String requestUrl, String _imagename_) {
this.requestUrl = requestUrl;
this.imagename_ = _imagename_;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
//pDialog...
}

@Override
protected Object doInBackground(Object... objects) {
try {
URL url = new URL(requestUrl);
URLConnection conn = url.openConnection();
bitmap = BitmapFactory.decodeStream(conn.getInputStream());
numberofbits = bitmap.getByteCount();
} catch (Exception ignored) {
}
return null;
}


@Override
protected void onPostExecute(Object o) {
if (!ImageStorage.checkifImageExists(imagename)) {
ImageStorage.saveToSdCard(bitmap, imagename_);
}

if (numberofbits > 50) {
db = new DatabaseHandler(getActivity());
db.updateImages(new Images(dbid, "Downloaded", imagename_));
db.close();
//populateList(); -> THIS I USED FOR A LISTVIEW
Toast.makeText(getActivity(), "Downloaded!", Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();

//If image gets downloaded open it in Viewer class
Intent intent = new Intent(getActivity(), DisplayImage.class);
intent.putExtra("imagePath", path);
startActivity(intent);

} else {
//Toast.makeText(getActivity(), "Unable to download", Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();

}
}
}
}

这是图像类

public class Images {

//private variables
private int _id;
private String _title;
private String _downloadstatus;

// Empty constructor
public Images () {

}

// constructor
public Images(int id, String title, String downloadstatus) {
this._id = id;
this._title = title;
this._downloadstatus = downloadstatus;
}


public int getID() {
return this._id;
}

public void setID(int id) {
this._id = id;
}

public String getTitle() {
return this._title;
}

public void setTitle(String title) {
this._title = title;
}


public String getDownloadStatus() {
return this._downloadstatus;
}

public void setDownloadStatus(String downloadstatus) {
this._downloadstatus = downloadstatus;
}
}

这是一个 XML(我正在尝试更新“text2”):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:foreground="?attr/selectableItemBackground"
android:layout_height="wrap_content"
android:padding="8dp">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
>


<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/imagename"
android:textStyle="bold"
android:paddingStart="4sp"
android:paddingEnd="40sp" />

<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:textSize="@dimen/downloaded"
android:paddingStart="4sp"
android:paddingEnd="1sp"
android:textColor="@color/graydownloaded" />


</RelativeLayout>

</FrameLayout>

最佳答案

好吧,我终于让它工作了,其实很简单......

//....     
if (numberofbits > 50) {
db = new DatabaseHandler(getActivity());
db.updateImages(new Images(dbid, "Downloaded", imagename_));
//This is what I added:
List<Images> listdb = db.getImages();
mAdapter = new ImgAdapter(getActivity(), listdb);
recyclerView.setAdapter(mAdapter);
db.close();
//populateList(); -> THIS I USED FOR A LISTVIEW
Toast.makeText(getActivity(), "Downloaded!", Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();

//If image gets downloaded open it in Viewer class
Intent intent = new Intent(getActivity(), DisplayImage.class);
intent.putExtra("imagePath", path);
startActivity(intent);

} else {
//...

关于android - 无法更新 RecyclerView 中的 TextView 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30156995/

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