- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在解析从服务器到 Android 的 JSON 并将数据存储在 SQLite 中,其中我有两个表 news
和 news_attachment
。
我已经完成了在列表中显示 news
和 news_attachment
的代码,但现在我想为一个新闻显示多个附件。
The following code displaying last_attachment of the particular news. I want to display all attachment with Custom Layout which will be settle automatically in parent view.
public class NewsAdapter extends SimpleCursorAdapter {
DatabaseHelper dbHelper;
LayoutInflater inflater;
public NewsAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
inflater = LayoutInflater.from(context);
dbHelper = new DatabaseHelper(context);
dbHelper.open();
}
@Override
public void bindView(View convertView, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) convertView.getTag();
if(cursor.isNull(cursor.getColumnIndex(DatabaseHelper.NEWS_TITLE))) {
holder.newsTitleText.setVisibility(View.GONE);
} else {
holder.newsTitleText.setVisibility(View.VISIBLE);
holder.newsTitleText.setText(""+cursor.getString(cursor.getColumnIndex(DatabaseHelper.NEWS_TITLE)));
}
if(cursor.isNull(cursor.getColumnIndex(DatabaseHelper.NEWS_TEXT))) {
holder.newsText.setVisibility(View.GONE);
} else {
holder.newsText.setVisibility(View.VISIBLE);
String newsText = cursor.getString(cursor.getColumnIndex(DatabaseHelper.NEWS_TEXT));
newsText = Html.fromHtml(newsText).toString();
holder.newsText.setText(newsText);
}
/***************************
* Getting Child Cursor...
*/
Cursor newsAttachmentCursor;
int newsId;
/**
* Setting up values of news_Attachment Table...
*/
newsId = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.NEWS_SERVER_ID));
newsAttachmentCursor = dbHelper.getNewsAttachment(newsId);
/**
* Displaying news Attachment
*/
if(newsAttachmentCursor.getCount() > 0 && newsAttachmentCursor != null){
newsAttachmentCursor.moveToFirst();
/**
* Displaying NEWS_AT_NAME
*/
if(newsAttachmentCursor.isNull(newsAttachmentCursor.getColumnIndex(DatabaseHelper.NEWS_AT_NAME))) {
holder.newsAttachment.setVisibility(View.GONE);
} else{
holder.newsAttachment.setVisibility(View.VISIBLE);
/**
* Displaying If attachment has Images....
*/
String fileName = newsAttachmentCursor.getString(newsAttachmentCursor.getColumnIndex(DatabaseHelper.NEWS_AT_NAME));
String fileUrl = newsAttachmentCursor.getString(newsAttachmentCursor.getColumnIndex(DatabaseHelper.NEWS_AT_URL));
String tempLowerUrl = fileUrl.toLowerCase();
if(tempLowerUrl != null && (tempLowerUrl.contains("png") || tempLowerUrl.contains("jpeg") || tempLowerUrl.contains("jpg") || tempLowerUrl.contains("ttif") || tempLowerUrl.contains("gif")))
{
holder.newsAttachment.setVisibility(View.GONE);
holder.newsImage.setVisibility(View.VISIBLE);
Log.d("oopscv2", "File : "+fileUrl);
new AQuery(mContext).id(holder.newsImage).image(newsAttachmentCursor.getString(newsAttachmentCursor.getColumnIndex(DatabaseHelper.NEWS_AT_URL)), true, true, 0, R.drawable.no_image, null, AQuery.FADE_IN);
} else {
holder.newsAttachment.setVisibility(View.VISIBLE);
holder.newsImage.setVisibility(View.GONE);
holder.newsAttachment.setText(fileName);
}
}
} else {
holder.newsAttachment.setVisibility(View.GONE);
holder.newsImage.setVisibility(View.GONE);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(R.layout.frag_news_row, parent, false);
v.setTag(new ViewHolder(v));
return v;
}
private static class ViewHolder
{
TextView newsTitleText;
TextView newsText;
ImageView newsImage;
TextView newsAttachment;
public ViewHolder(View view) {
// TODO Auto-generated constructor stub
newsTitleText = (TextView) view.findViewById(R.id.newsTitleText);
newsText = (TextView) view.findViewById(R.id.newsText);
newsImage = (ImageView) view.findViewById(R.id.newsImage);
newsAttachment = (TextView) view.findViewById(R.id.newsAttachment);
}
}
}
当前输出:
我认为动态 View 对此很有用,但我不知道如何使用 onClick 监听器为所有附件添加带格式的动态 View 。
这怎么可能?
现在我有以下更新的适配器:
public class NewsAdapterNew extends SimpleCursorAdapter {
DatabaseHelper dbHelper;
LayoutInflater inflater;
ViewHolder holder;
ViewHolder.SubViewHolder subHolder;
String DIRPATH = android.os.Environment.getExternalStorageDirectory() + File.separator + "TEMP" + File.separator;
public NewsAdapterNew(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
inflater = LayoutInflater.from(context);
dbHelper = new DatabaseHelper(context);
dbHelper.open();
}
@Override
public void bindView(View convertView, final Context context, Cursor cursor) {
holder = (ViewHolder) convertView.getTag();
if(cursor.isNull(cursor.getColumnIndex(DatabaseHelper.NEWS_TITLE))) {
holder.newsTitleText.setVisibility(View.GONE);
} else {
holder.newsTitleText.setVisibility(View.VISIBLE);
holder.newsTitleText.setText(""+cursor.getString(cursor.getColumnIndex(DatabaseHelper.NEWS_TITLE)));
}
if(cursor.isNull(cursor.getColumnIndex(DatabaseHelper.NEWS_TEXT))) {
holder.newsText.setVisibility(View.GONE);
} else {
holder.newsText.setVisibility(View.VISIBLE);
String newsText = cursor.getString(cursor.getColumnIndex(DatabaseHelper.NEWS_TEXT));
newsText = Html.fromHtml(newsText).toString();
holder.newsText.setText(newsText);
}
/***************************
* Getting Child Cursor...
*/
Cursor newsAttachmentCursor;
int newsId;
/**
* Setting up values of news_Attachment Table...
*/
newsId = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.NEWS_SERVER_ID));
newsAttachmentCursor = dbHelper.getNewsAttachment(newsId);
Log.d(TAG, "newsAttachmentCursor.getCount() : " +newsAttachmentCursor.getCount());
/**
* Displaying news Attachment
*/
if(newsAttachmentCursor.getCount() > 0 && newsAttachmentCursor != null){
//newsAttachmentCursor.moveToFirst();
/**
* Displaying NEWS_AT_NAME
*/
holder.layoutAttachment.removeAllViews();
newsAttachmentCursor.moveToFirst();
do {
View viewAttachement = LayoutInflater.from(mContext).inflate(R.layout.attachment_layout, null);
subHolder = new com.salesman.adapter.NewsAdapterNew.ViewHolder.SubViewHolder(viewAttachement);
subHolder.newsImage.setTag(subHolder);
subHolder.newsImageDownload.setTag(subHolder);
subHolder.newsAttachmentFileName.setTag(subHolder);
/**
* Displaying If attachment has Images....
*/
subHolder.fileName = newsAttachmentCursor.getString(newsAttachmentCursor.getColumnIndex(DatabaseHelper.NEWS_AT_NAME));
subHolder.fileUrl = newsAttachmentCursor.isNull(newsAttachmentCursor.getColumnIndex(DatabaseHelper.NEWS_AT_URL)) ? "" : newsAttachmentCursor.getString(newsAttachmentCursor.getColumnIndex(DatabaseHelper.NEWS_AT_URL));
subHolder.newsAttachmentFileName.setText(subHolder.fileName);
String tempExt = subHolder.fileUrl.contains(".") ? subHolder.fileUrl.substring(subHolder.fileUrl.lastIndexOf(".")).toLowerCase() : "";
Log.d(TAG, "Temp Extension : "+tempExt);
if(!tempExt.isEmpty()) {
File myFile = new File(DIRPATH + subHolder.fileName);
if(myFile.exists()) {
subHolder.newsImageDownload.setVisibility(View.GONE);
} else {
subHolder.newsImageDownload.setVisibility(View.VISIBLE);
}
if(tempExt != null && tempExt.equals(".mp4") || (tempExt.equals(".jpeg") || tempExt.equals(".jpg") || tempExt.equals(".ttif") || tempExt.equals(".gif") || tempExt.equals(".png")|| tempExt.equals(".pdf")|| tempExt.equals(".docx")|| tempExt.equals(".doc")|| tempExt.equals(".xls")|| tempExt.equals(".xlsx")|| tempExt.equals(".ppt")|| tempExt.equals(".pptx")|| tempExt.equals(".txt")|| tempExt.equals(".zip")))
{
subHolder.newsImage.setVisibility(View.VISIBLE);
if(tempExt.equals(".pdf")) {
new AQuery(mContext).id(subHolder.newsImage).image(R.drawable.ic_pdf);
} else if(tempExt.equals(".doc") || tempExt.equals(".docx")) {
new AQuery(mContext).id(subHolder.newsImage).image(R.drawable.ic_word);
} else if(tempExt.equals(".xls") || tempExt.equals(".xlsx")) {
new AQuery(mContext).id(subHolder.newsImage).image(R.drawable.ic_excel);
} else if(tempExt.equals(".ppt") || tempExt.equals(".pptx")) {
new AQuery(mContext).id(subHolder.newsImage).image(R.drawable.ic_ppt);
} else if(tempExt.equals(".txt")) {
new AQuery(mContext).id(subHolder.newsImage).image(R.drawable.ic_text);
} else if(tempExt.equals(".zip")) {
new AQuery(mContext).id(subHolder.newsImage).image(R.drawable.ic_zip);
} else if(tempExt.equals(".mp4")) {
new AQuery(mContext).id(subHolder.newsImage).image(R.drawable.ic_video);
} else {
subHolder.newsImage.setVisibility(View.VISIBLE);
new AQuery(mContext).id(subHolder.newsImage).image(subHolder.fileUrl, true, true, 0, R.drawable.no_image, null, AQuery.FADE_IN);
}
} else {
subHolder.newsImage.setVisibility(View.GONE);
subHolder.newsImageDownload.setVisibility(View.GONE);
}
/***
*
*/
subHolder.newsImageDownload.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
com.salesman.adapter.NewsAdapterNew.ViewHolder.SubViewHolder tempHolder = ((com.salesman.adapter.NewsAdapterNew.ViewHolder.SubViewHolder)v.getTag());
//Toast.makeText(mContext, tempHolder.fileName.toString() + " is Downloading...", Toast.LENGTH_LONG).show();
if(InternetConnection.checkConnection(context)) {
tempHolder.newsImageDownload.setVisibility(View.GONE);
tempHolder.pBar.setVisibility(View.VISIBLE);
new DownloadFileAsync().execute(tempHolder);
} else {
tempHolder.newsImageDownload.setVisibility(View.VISIBLE);
tempHolder.pBar.setVisibility(View.GONE);
AlertDialogManager.showAlertDialog(context, "Download failed", "The download was unable to complete. Please try again later.", false);
}
}
});
subHolder.newsImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
com.salesman.adapter.NewsAdapterNew.ViewHolder.SubViewHolder tempHolder = ((com.salesman.adapter.NewsAdapterNew.ViewHolder.SubViewHolder)v.getTag());
Toast.makeText(mContext, "Downloaded : " + tempHolder.fileName.toString(), Toast.LENGTH_LONG).show();
File myFile = new File(DIRPATH + tempHolder.fileName.toString());
FileOpen.openFile(mContext, myFile);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d(TAG, "Error While Opening : "+e.getLocalizedMessage());
}
}
});
/** Adding News Attachment Layout in Parent LinearLayout **/
holder.layoutAttachment.addView(viewAttachement);
}
viewAttachement.setTag(subHolder);
} while(newsAttachmentCursor.moveToNext());
} else {
holder.layoutAttachment.setVisibility(View.GONE);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(R.layout.frag_news_row, parent, false);
v.setTag(new ViewHolder(v));
return v;
}
private static class ViewHolder
{
TextView newsTitleText;
TextView newsText;
LinearLayout layoutAttachment;
static class SubViewHolder {
ImageView newsImage,newsImageDownload;
TextView newsAttachmentFileName;
ProgressBar pBar;
String fileUrl;
String fileName;
public SubViewHolder(View subView) {
// TODO Auto-generated constructor stub
newsImage = (ImageView) subView.findViewById(R.id.newsImage);
newsImageDownload =(ImageView) subView.findViewById(R.id.newsImageDownload);
pBar = (ProgressBar) subView.findViewById(R.id.newsProgressBar);
newsAttachmentFileName = (TextView) subView.findViewById(R.id.newsAttachment);
}
}
public ViewHolder(View view) {
// TODO Auto-generated constructor stub
newsTitleText = (TextView) view.findViewById(R.id.newsTitleText);
newsText = (TextView) view.findViewById(R.id.newsText);
layoutAttachment = (LinearLayout) view.findViewById(R.id.linear_news_row_attachment);
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/newsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@drawable/bg_white_shadow"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/newsTitleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:text="News From"
android:textColor="@android:color/black"
android:textSize="@dimen/common_title_textview"
android:textStyle="bold" />
<TextView
android:id="@+id/newsText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="News Text News Text"
android:textColor="@android:color/black"
android:textSize="@dimen/common_fontsize" />
<LinearLayout
android:id="@+id/linear_news_row_attachment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
While scrolling sometimes all News Attachment per News going to blank. I think this problem occurring in adapter while binding view but i cant found.
通过下面的图片你会更清楚这一点。
难道我的适配器代码有误?我在适配器中犯了什么错误?
我们将不胜感激。
感谢和问候,
实践
最佳答案
实现目标的最简单方法是以编程方式为每个附件添加 View 。你必须这样做:例如,制作 holder.newsAttachments - linearLayout。
然后在你的 bindView 方法中:
holder.newsAttachment.removeAllViews(); //remove views if it exists
while (newsAttachmentCursor.moveToNext()) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_for_attachment, null);
//fill view
String fileName = newsAttachmentCursor.getString(newsAttachmentCursor.getColumnIndex(DatabaseHelper.NEWS_AT_NAME));
view.setTag(fileName); // add as tag something, what you need inside onCLickListener
((TextView) view.findViewById(R.id.attachment_view_name)).setText(fullName);
holder.newsAttachment.addView(view);
view.setOnClickListener(new OnClickListener() {
@Override
void onClick(View v) {
String fileName = (String) v.getTag();
//do what you want with file name, or anything what you've added to tag.
}
});
}
这只是示例,但这个想法适用于少量附件。
关于Android:使用动态子布局(ViewHolder 和 SubViewHolder)绑定(bind)布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23127485/
最近我使用 RecyclerView 并添加了一个自定义标题 View (另一种类型的项目 View )并尝试在数据发生更改时对其进行更新。奇怪的事情发生了。适配器创建一个新的 HeaderViewH
我的项目应该基于用户输入(用户发送文本、拍照或录制视频)创建多个 View ,类似于 WhatsApp 聊天 Activity ,结构几乎相同。适配器应该能够通过使用 getItemViewType(
在 ViewHolder pattern 中将 ViewHolder 设为静态对性能至关重要吗? ? A ViewHolder object stores each of the component
我只是想更好地理解我经常用来优化 ListView 的以下模式 我的阅读只指出静态内部类被视为顶级类的事实。与成员类(非静态)相比,这样的事情有什么好处? @Override public View
我有一个 RecyclerView 和一些 view/card(我们暂时称它为 View ),它们都包含相同的东西,包括我用作分隔栏的 View。 我希望能够在当前 View 上方的 view 中更改
我需要从 View Holder 访问 RecyclerView Adapter 的方法。我找不到任何解决方案。 或者是否可以从 ViewHolder 的 ViewModel 类(我已经在 MVVM
在我的 RecyclerView 适配器中只有一个 ViewHolder 类: public static class PlayerItemViewHolder extends RecyclerVie
我已经采用了一个示例代码来实现 RecyclerView,但试图将其转换为在我的应用程序的子 fragment 中工作。 代码在“创建列表 - 示例”下 Creating Lists and Card
我试图在我的主要 Activity 中而不是在我的自定义适配器中保存复选框的状态。我已检查共享首选项中是否存储了正确的数据,并且可以成功检索信息,但是当我尝试在打开应用程序时标记复选框时, View
我是 Kotlin 的新手,我正在制作一款货币兑换应用。在适配器中,我想将一些项目传递到新 Activity 中。 class AdapterC (val countryList: ArrayLis
我在我的代码中遇到了这个问题,当在 Viewholder 中的一个按钮上进行转换时,这是在 Onclicklistener 中完成的,多个转换发生在不同的行上即,如果我单击第 1 行中的按钮,则按钮向
public class ListViewAdapter extends BaseAdapter { private Context context; private LocationDetails[
我刚刚生成了一个 Master/Detail Flow 项目,我发现了一些奇怪的东西:在 DriverListActivity.java 中,名为 ViewHolder 的子类具有 final 属性。
我正在尝试配置抽屉导航以显示 3 种不同类型的“行”,但我在使用 Holder 时遇到了问题,被告知该消息: FATAL EXCEPTION: main java.lang.ClassCastExce
我理解Viewholder pattern的思路和用法,但我仍然有一个问题: 假设我们在 viewholder 中有一个 TextView,并显示 10 个项目(“item0,item1 ....”)
我有一个包含 3 种不同类型行的 ListView 。我在网上收到 ClassCastException holder = (RowViewHolder) row.getTag(); 我注意到 row
我正在尝试学习 Android 编程。我找不到这个算法的解释: public View getView(int r, View convertView, ViewGroup parent) { V
我正在创建一个 Android 应用程序,其中包含一个带有嵌套 CardView 的 RecyclerView。我需要将所有其他卡片替换为不同的颜色。我正在使用 @Override 来覆盖 onBin
我在 ListView 中使用分段时遇到问题。我需要使用 viewholder 使 listview 滚动平滑,但我不知道如何实现两个 viewholders,因为有两个单独的 View ,一个是部分
我在 ListView 上遇到了 View 持有者的充气问题。实现 Viewholder 后,充气机会出错,因此消息显示错误。 (在图 10 中,消息不是来 self ,而且每当我滚动时,错误的消息都
我是一名优秀的程序员,十分优秀!