设置标签时不一致

时间:2016-12-29 01:10:27

标签: android listview tags

我有一个我用于所有listViews的单元适配器。不同类型的列表由参数TableType确定。

TableTypes 1和2还使用外部库,允许使用滑出功能进行保存和删除。由于此列表视图中的OnClickListenersTableType不适用于OnClickListener 1和2。

因此,我在单元适配器本身中添加了onClickListener(标签的" Cell"部分的TableType。单元适配器比使用接口参数调用必要的方法。

这里的不一致是TableType 1正确执行而没有错误。 TableType 2不会然而,因为标记返回其上方项目的位置。

即。如果TableType 2有4个项目,则项目2将具有标记1,项目3将具有标记2.我不确定为什么会发生这种情况,因为package layout; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.SharedPreferences; import android.media.Image; import android.nfc.Tag; import android.support.v4.app.FragmentTransaction; import android.support.v7.widget.RecyclerView; import android.widget.ArrayAdapter; import android.content.Context; import android.view.View; import java.util.ArrayList; import java.util.HashSet; import java.util.Set; import android.preference.PreferenceManager; import android.widget.ImageView; import android.widget.TextView; import android.view.LayoutInflater; import android.view.ViewGroup; import com.chauthai.swipereveallayout.SwipeRevealLayout; import com.tble.brgo.InfoArticle; import com.tble.brgo.R; import android.graphics.Typeface; import android.widget.Filterable; import android.widget.Filter; import org.w3c.dom.Text; /** * Created by Praveen on 8/21/16. */ public class StandardCellAdapter extends ArrayAdapter<InfoArticle> implements Filterable { public ArrayList<InfoArticle> orig; public ArrayList<InfoArticle> Teachers; public SwipeRevealLayout swipeV; public TextView deleteButton; public TextView saveButton; public int tableType; public displayInterface activity; public Context ct; public StandardCellAdapter(Context context, ArrayList<InfoArticle> titles, int tableType, displayInterface inter) { super(context, 0, titles); this.Teachers = titles; this.tableType = tableType; saveButton = new TextView(context); deleteButton = new TextView(context); activity = inter; ct = context; } @Override public View getView(int position, View convertView, ViewGroup parent) { // Get the data item for this position String title = getItem(position).title; String desc = getItem(position).description; Holder TagHolder = null; TextView Ctitle; // Check if an existing view is being reused, otherwise inflate the view if (convertView == null) { if (tableType == 0) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.nclayout, parent, false); Ctitle = (TextView) convertView.findViewById(R.id.cellTitle); } else if(tableType == 1){ convertView = LayoutInflater.from(getContext()).inflate(R.layout.sclayout, parent, false); Ctitle = (TextView) convertView.findViewById(R.id.cellTitle); TagHolder = new Holder(); swipeV = (SwipeRevealLayout)convertView.findViewById(R.id.scSwipe); saveButton = (TextView)convertView.findViewById(R.id.saveButton); TagHolder.Save = saveButton; TagHolder.Cell = Ctitle; convertView.setTag(TagHolder); } else{ convertView = LayoutInflater.from(getContext()).inflate(R.layout.tclayout, parent, false); Ctitle = (TextView) convertView.findViewById(R.id.cellTitle); TagHolder = new Holder(); swipeV = (SwipeRevealLayout)convertView.findViewById(R.id.tcSwipe); deleteButton = (TextView) convertView.findViewById(R.id.deleteButton); TagHolder.Delete = deleteButton; TagHolder.Cell = Ctitle; convertView.setTag(TagHolder); } } else { TagHolder = (Holder) convertView.getTag(); Ctitle = (TextView) convertView.findViewById(R.id.cellTitle); } if(tableType == 1) { TagHolder.Save.setTag(position); TagHolder.Save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { saveTeacher((int)v.getTag()); } }); TagHolder.Cell.setTag(position); TagHolder.Cell.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { activity.displayWebpage(getItem((int)v.getTag()).description); } }); } else if(tableType == 2) { TagHolder.Delete.setTag(position); deleteButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { deleteTeacher((int)v.getTag()); if(Teachers.size() == 0) Teachers.add(new InfoArticle("Slide to Save Teachers", "google.com")); notifyDataSetChanged(); } }); TagHolder.Cell.setTag(position); TagHolder.Cell.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { activity.displayWebpage(getItem((int)v.getTag()).description); } }); } Typeface customFont = Typeface.SERIF; Ctitle.setTypeface(customFont); Ctitle.setText(title); return convertView; } public Filter getFilter() { return new Filter() { @Override protected FilterResults performFiltering(CharSequence constraint) { final FilterResults oReturn = new FilterResults(); final ArrayList<InfoArticle> results = new ArrayList<InfoArticle>(); if (orig == null) orig = Teachers; if (constraint != null) { if (orig != null && orig.size() > 0) { for (final InfoArticle g : orig) { if (g.title.toLowerCase() .contains(constraint.toString().toLowerCase())) { results.add(g); } } } oReturn.values = results; } return oReturn; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { Teachers = (ArrayList<InfoArticle>)results.values; notifyDataSetChanged(); } }; } @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); } public ArrayList<InfoArticle> toIA(ArrayList<String> data){ ArrayList<InfoArticle> converted = new ArrayList<InfoArticle>(); for(String a: data) { converted.add(new InfoArticle(a,"")); } return converted; } @Override public int getCount() { return Teachers.size(); } @Override public InfoArticle getItem(int position) { return Teachers.get(position); } public void saveTeacher(int position){ SharedPreferences sharedPref = getContext().getSharedPreferences("MY_PREFERENCES",Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); Set<String> teach = sharedPref.getStringSet("teachPref", new HashSet<String>()); teach.add(Teachers.get(position).title); Set<String> link = sharedPref.getStringSet("linkPref", new HashSet<String>()); link.add(Teachers.get(position).description); editor.putStringSet("teachPref", teach); editor.putStringSet("linkPref", link); editor.apply(); new AlertDialog.Builder(getContext()) .setTitle("Teacher Saved") .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // continue with delete } }).setIcon(android.R.drawable.ic_dialog_alert) .show(); swipeV.close(true); } private void deleteTeacher(int position){ SharedPreferences sharedPref = getContext().getSharedPreferences("MY_PREFERENCES",Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); Set<String> teach = sharedPref.getStringSet("teachPref", new HashSet<String>()); Set<String> link = sharedPref.getStringSet("linkPref", new HashSet<String>()); teach.remove(Teachers.get(position).title); link.remove(Teachers.get(position).description); editor.putStringSet("teachPref", teach); editor.putStringSet("linkPref", link); editor.apply(); Teachers.remove(position); new AlertDialog.Builder(getContext()) .setTitle("Teacher Deleted") .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // continue with delete } }).setIcon(android.R.drawable.ic_dialog_alert) .show(); swipeV.close(false); } class Holder{ TextView Delete; TextView Save; TextView Cell; } } 1和2之间的代码相同。

CellAdapter:

/graphql

1 个答案:

答案 0 :(得分:0)

原来问题不在于标签是我保存数据的方式。 TableType 2用于显示来自2个不同Hashset的数据,然后将其组合。因为Hashsets不关心订单,所以由于缺乏订单,我的组合ArrayList会不匹配。我通过将我的ArrayList保存为JSON序列化字符串来修复此问题。