gpt4 book ai didi

Android:使用动态子布局(ViewHolder 和 SubViewHolder)绑定(bind)布局

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:37:20 25 4
gpt4 key购买 nike

我正在解析从服务器到 Android 的 JSON 并将数据存储在 SQLite 中,其中我有两个表 newsnews_attachment

我已经完成了在列表中显示 newsnews_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.

新闻适配器.java

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);
}
}
}

当前输出:

Output

我想要像这样的动态输出(临时 4 附件):

enter image description here

我认为动态 View 对此很有用,但我不知道如何使用 onClick 监听器为所有附件添加带格式的动态 View 。

这怎么可能?


解决上述问题后更新问题:

现在我有以下更新的适配器:

NewsNewAdapter.java

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);
}
}
}

frag_new_row.xml

<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.

通过下面的图片你会更清楚这一点。

滚动前输出:

Before Scrolling

滚动后输出:

After Scrolling

难道我的适配器代码有误?我在适配器中犯了什么错误?

我们将不胜感激。

感谢和问候,

实践

最佳答案

实现目标的最简单方法是以编程方式为每个附件添加 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/

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