- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 fragment 创建一个 ListView
,其中填充了 CheckBox
es。我尽力根据其他问题找到解决方案,但我似乎无法修复它。当我不使用以下命令运行脚本时:
convertView.getTag();
我的应用程序运行良好,但列表中 Item 的位置并未保存。 View 项跳来跳去。但是,当我确实使用该方法时,出现错误。日志猫指出:
01-02 23:54:20.662: E/InputEventReceiver(1209): Exception dispatching input event.
01-02 23:54:20.672: D/AndroidRuntime(1209): Shutting down VM
01-02 23:54:20.672: W/dalvikvm(1209): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-02 23:54:20.782: E/AndroidRuntime(1209): FATAL EXCEPTION: main
01-02 23:54:20.782: E/AndroidRuntime(1209): java.lang.ClassCastException: com.MrBabilin.youdeserveadrink.Items cannot be cast to com.MrBabilin.youdeserveadrink.AlcoholList$MyCustomAdapter$ViewHolder
01-02 23:54:20.782: E/AndroidRuntime(1209): at com.MrBabilin.youdeserveadrink.AlcoholList$MyCustomAdapter.getView(AlcoholList.java:169)
01-02 23:54:20.782: E/AndroidRuntime(1209): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
01-02 23:54:20.782: E/AndroidRuntime(1209): at android.widget.ListView.makeAndAddView(ListView.java:1769)
01-02 23:54:20.782: E/AndroidRuntime(1209): at android.widget.ListView.fillDown(ListView.java:672)
01-02 23:54:20.782: E/AndroidRuntime(1209): at android.widget.ListView.fillGap(ListView.java:636)
01-02 23:54:20.782: E/AndroidRuntime(1209): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5040)
01-02 23:54:20.782: E/AndroidRuntime(1209): at
此 Activity 由 3 个类(class)组成。主要的:IngredientsActivity
public class IngredientsActivity extends FragmentActivity implements ActionBar.TabListener {
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_list);
getActionBar().setTitle("Search");
getActionBar().setIcon(R.drawable.search_1);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// For each of the sections in the app, add a tab to the action bar.
actionBar.addTab(actionBar.newTab().setText("Alcahol").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Juice").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Other").setTabListener(this));
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
if (tab.getPosition() == 0) {
AlcoholList simpleListFragment = new AlcoholList();
getSupportFragmentManager().beginTransaction().replace(R.id.containert, simpleListFragment).commit();
}
else if (tab.getPosition() == 1) {
JuiceList androidlidt = new JuiceList();
getSupportFragmentManager().beginTransaction().replace(R.id.containert, androidlidt).commit();
}
else {
OtherList androidversionlist = new OtherList();
getSupportFragmentManager().beginTransaction().replace(R.id.containert, androidversionlist).commit();
}
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public static class DummySectionFragment extends Fragment {
public DummySectionFragment() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args = getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
}
然后是 fragment :AlcoholList
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
public class AlcoholList extends ListFragment{
MyCustomAdapter dataAdapter = null;
private Set<String> recipesList = new HashSet<String>();
private ArrayList<Items> stateList = new ArrayList<Items>();
public AlcoholList() {
Items _states1 = new Items ("Gin",false);
stateList.add(_states1);
Items _states2 = new Items ("Ginger Liqueur",false);
stateList.add(_states2);
Items _states3 = new Items ("Citrus Vodka",false);
stateList.add(_states3);
Items _states4 = new Items ("Champagne",false);
stateList.add(_states4);
Items _states5 = new Items ("Coconut Rum",false);
stateList.add(_states5);
Items _states6 = new Items ("Pear Vodka",false);
stateList.add(_states6);
Items _states7 = new Items ("Rum",false);
stateList.add(_states7);
Items _states8 = new Items ("Tequila",false);
stateList.add(_states8);
Items _states9 = new Items ("Triple Sec",false);
stateList.add(_states9);
Items _states10 = new Items ("Kahlua",false);
stateList.add(_states10);
Items _states11 = new Items ("Vanilla Vodka",false);
stateList.add(_states11);
Items _states12 = new Items ("Sake",false);
stateList.add(_states12);
Items _states13 = new Items ("Bourbon",false);
stateList.add(_states13);
Items _states14 = new Items ("Vodka",false);
stateList.add(_states14);
Items _states15 = new Items ("Beer",false);
stateList.add(_states15);
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataAdapter = new MyCustomAdapter(this.getActivity(),R.layout.da_item, stateList);
setListAdapter(dataAdapter);
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getActivity());
recipesList = sharedPreferences.getStringSet("string", recipesList);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_fragment, container, false);
}
@Override
public void onListItemClick(ListView list, View v, int position, long id) {
}
private class MyCustomAdapter extends ArrayAdapter<Items>
{
private ArrayList<Items> stateList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Items> stateList)
{
super(context, textViewResourceId, stateList);
this.stateList = new ArrayList<Items>();
this.stateList.addAll(stateList);
}
private class ViewHolder
{
CheckBox name;
}
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.da_item, null);
holder = new ViewHolder();
convertView.setTag(holder);
holder.name = (CheckBox) convertView.findViewById(R.id.ingredientbox);
holder.name.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
Items _state = (Items) cb.getTag();
_state.setSelected(cb.isChecked());
}
});
}
else
{
holder = (ViewHolder) convertView.getTag(); // THIS IS THE PROBLEM
}
Items state = stateList.get(position);
holder.name.setText(state.getName());
holder.name.setChecked(state.isSelected());
holder.name.setTag(state);
return convertView;
}
}
}
然后是 Array 类:Items
public class Items {
String name ;
boolean selected;
public Items(String name, boolean selected){
super();
this.name=name;
this.selected=selected;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
我不知道这是否有帮助,但是在我尝试实现 fragment View 之前并没有出现这个问题。有效的代码是:
public class IngredientsActivity extends Activity {
MyCustomAdapter dataAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.check_list);
//Generate list View from ArrayList
displayListView();
}
private void displayListView()
{
//Array list of countries
ArrayList<Items> stateList = new ArrayList<Items>();
Items _states2 = new Items ("420" ,false);
stateList.add(_states2);
Items _states3 = new Items ("Amanda Bynes" ,false);
stateList.add(_states3);
Items _states4 = new Items ("Angelina Jolie" ,false);
stateList.add(_states4);
Items _states5 = new Items ("Anne Hathaway" ,false);
stateList.add(_states5);
Items _states6 = new Items ("Bachelorette Contestants" ,false);
stateList.add(_states6);
Items _states7 = new Items ("Beauty and the Beast" ,false);
stateList.add(_states7);
Items _states8 = new Items ("Beyonce" ,false);
stateList.add(_states8);
Items _states9 = new Items ("Big Boi" ,false);
stateList.add(_states9);
Items _states10 = new Items ("Charlie Sheen" ,false);
stateList.add(_states10);
}
//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this,R.layout.da_item, stateList);
ListView listViews = (ListView) findViewById(R.id.ingList2);
// Assign adapter to ListView
listViews.setAdapter(dataAdapter);
}
private class MyCustomAdapter extends ArrayAdapter<Items>
{
private ArrayList<Items> stateList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Items> stateList)
{
super(context, textViewResourceId, stateList);
this.stateList = new ArrayList<Items>();
this.stateList.addAll(stateList);
}
private class ViewHolder
{
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.da_item, null);
holder = new ViewHolder();
holder.name = (CheckBox) convertView.findViewById(R.id.ingredientbox);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
Items _state = (Items) cb.getTag();
_state.setSelected(cb.isChecked());
}
});
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Items state = stateList.get(position);
holder.name.setText(state.getName());
holder.name.setChecked(state.isSelected());
holder.name.setTag(state);
return convertView;
}
}
}
问题是:调用 convertView.getTag() 时如何防止我的应用程序崩溃
最佳答案
R.layout.da_item
文件只包含 R.id.ingredientbox
所以你的 covertView
和 holder.name
指的是同一个对象。
现在当你打电话的时候
convertView.setTag(holder);
holder 设置为 convertView 对象的标签,然后你调用
holder.name.setTag(state);
这会覆盖标签,因为两个对象相同。
尝试删除第 2 个调用并检查。
编辑将您的持有人类别更改为:
private class ViewHolder
{
Items state;
CheckBox name;
}
不要写holder.name.setTag(state);
。而是执行以下操作:
holder.state=state;
现在,当你想要元素时,只需说:
Items _state = ((ViewHolder) cb.getTag()).state;
关于java - convertView.getTag();创建转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20896930/
我正在尝试将一个字符串逐个字符地复制到另一个字符串中。目的不是复制整个字符串,而是复制其中的一部分(我稍后会为此做一些条件......) 但我不知道如何使用迭代器。 你能帮帮我吗? std::stri
我想将 void 指针转换为结构引用。 结构的最小示例: #include "Interface.h" class Foo { public: Foo() : mAddress((uint
这有点烦人:我有一个 div,它从窗口的左上角开始过渡,即使它位于文档的其他任何位置。我试过 usign -webkit-transform-origin 但没有成功,也许我用错了。有人可以帮助我吗?
假设,如果将 CSS3 转换/转换/动画分配给 DOM 元素,我是否可以检测到该过程的状态? 我想这样做的原因是因为我正在寻找类似过渡链的东西,例如,在前一个过渡之后运行一个过渡。 最佳答案 我在 h
最近我遇到了“不稳定”屏幕,这很可能是由 CSS 转换引起的。事实上,它只发生在 Chrome 浏览器 上(可能还有 Safari,因为一些人也报告了它)。知道如何让它看起来光滑吗?此外,您可能会注意
我正在开发一个简单的 slider ,它使用 CSS 过渡来为幻灯片设置动画。我用一些基本样式和一些 javascript 创建了一支笔 here .注意:由于 Codepen 使用 Prefixfr
我正在使用以下代码返回 IList: public IList FindCodesByCountry(string country) { var query =
如何设计像这样的操作: 计算 转化 翻译 例如:从“EUR”转换为“CNY”金额“100”。 这是 /convert?from=EUR&to=CNY&amount=100 RESTful 吗? 最佳答
我使用 jquery 组合了一个图像滚动器,如下所示 function rotateImages(whichHolder, start) { var images = $('#' +which
如何使用 CSS (-moz-transform) 更改一个如下所示的 div: 最佳答案 你可以看看Mozilla Developer Center .甚至还有例子。 但是,在我看来,您的具体示例不
我需要帮助我正在尝试在选中和未选中的汉堡菜单上实现动画。我能够为菜单设置动画,但我不知道如何在转换为 0 时为左菜单动画设置动画 &__menu { transform: translateX(
我正在为字典格式之间的转换而苦苦挣扎:我正在尝试将下面的项目数组转换为下面的结果数组。本质上是通过在项目第一个元素中查找重复项,然后仅在第一个参数不同时才将文件添加到结果集中。 var items:[
如果我有两个定义相同的结构,那么在它们之间进行转换的最佳方式是什么? struct A { int i; float f; }; struct B { int i; float f; }; void
我编写了一个 javascript 代码,可以将视口(viewport)从一个链接滑动到另一个链接。基本上一切正常,你怎么能在那里看到http://jsfiddle.net/DruwJ/8/ 我现在的
我需要将文件上传到 meteor ,对其进行一些图像处理(必要时进行图像转换,从图像生成缩略图),然后将其存储在外部图像存储服务器(s3)中。这应该尽可能快。 您对 nodejs 图像处理库有什么建议
刚开始接触KDB+,有一些问题很难从Q for Mortals中得到。 说,这里 http://code.kx.com/wiki/JB:QforMortals2/casting_and_enumera
我在这里的一个项目中使用 JSF 1.2 和 IceFaces 1.8。 我有一个页面,它基本上是一大堆浮点数字段的大编辑网格。这是通过 inputText 实现的页面上的字段指向具有原始值的值对象
ScnMatrix4 是一个 4x4 矩阵。我的问题是什么矩阵行对应于位置(ScnVector3),旋转(ScnVector4),比例(ScnVector3)。第 4 行是空的吗? 编辑: 我玩弄了
恐怕我是 Scala 新手: 我正在尝试根据一些简单的逻辑将 Map 转换为新 Map: val postVals = Map("test" -> "testing1", "test2" -> "te
输入: This is sample 1 This is sample 2 输出: ~COLOR~[Green]This is sample 1~COLOR~[Red]This is sam
我是一名优秀的程序员,十分优秀!