Android 合并适配器-can';t检索截面位置

Android 合并适配器-can';t检索截面位置,android,commonsware-cwac,Android,Commonsware Cwac,我希望将MergeAdapter集成到我的项目中,但在检索用户单击的部分时遇到了问题。我想设置它,这样当用户单击任何部分中的任何项目时,都会返回部分编号,以便我知道用户所在的部分。我正在使用的应用程序需要此功能 下面的代码设置了3个部分,每个部分都有一些数据 public class SectionTesting extends Activity { ListView listView; private MergeAdapter mergeAdapter = null;

我希望将MergeAdapter集成到我的项目中,但在检索用户单击的部分时遇到了问题。我想设置它,这样当用户单击任何部分中的任何项目时,都会返回部分编号,以便我知道用户所在的部分。我正在使用的应用程序需要此功能

下面的代码设置了3个部分,每个部分都有一些数据

public class SectionTesting extends Activity
{

    ListView listView;
    private MergeAdapter mergeAdapter = null;

    private static final String[] items =
    { "One", "Two", "Three" };

    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mergelayout);

        context = this;

        listView = (ListView) findViewById(R.id.mergeListView);

        mergeAdapter = new MergeAdapter();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, new ArrayList<String>(
                        Arrays.asList(items)));

        TextView sectionHeader = new TextView(this);
        sectionHeader.setText("Section One");

        mergeAdapter.addView(sectionHeader);
        mergeAdapter.addAdapter(adapter);

        TextView sectionHeaderTwo = new TextView(this);
        sectionHeaderTwo.setText("Section Two");

        mergeAdapter.addView(sectionHeaderTwo);
        mergeAdapter.addAdapter(adapter);

        TextView sectionHeaderThree = new TextView(this);
        sectionHeaderThree.setText("Section Three");

        mergeAdapter.addView(sectionHeaderThree);
        mergeAdapter.addAdapter(adapter);

        listView.setAdapter(mergeAdapter);

        listView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position,
                    long arg3)
            {
                Toast.makeText(
                        context,
                        "You clicked Section "
                                + mergeAdapter.getSectionForPosition(position),
                        Toast.LENGTH_SHORT).show();

            }

        });
    }

}
然后我在这里添加了这个方法

public int getSectionNumber(int position)
    {
        int section = 0;

        for (ListAdapter piece : getPieces())
        {
            int size = piece.getCount();

            if (position < size)
            {
                return section-=1;
            }

            position -= size;

            if(size == 1)
            {
                if(piece.getItem(0) instanceof TextView)
                {
                    TextView tv = (TextView) piece.getItem(0);
                    if(tv.getId() == 1000)
                    {
                        section++;  
                    }
                }       
            }
        }

        return (-1);
    }
public int getSectionNumber(int位置)
{
int段=0;
对于(ListAdapter片段:getPieces())
{
int size=piece.getCount();
如果(位置<尺寸)
{
返回段-=1;
}
位置-=大小;
如果(大小==1)
{
if(工件.getItem(0)文本视图实例)
{
TextView tv=(TextView)片段.getItem(0);
if(tv.getId()==1000)
{
第++;
}
}       
}
}
返回(-1);
}

代码似乎工作得很好,我觉得有点草率。如果有人能想出一个更干净的解决方案,我将不胜感激,如果没有,那么我将在我可以接受的时候添加这个作为答案,你至少有两个问题:

  • 为了使用
    getSectionForPosition()
    ,您的
    适配器必须实现
    SectionIndexer
    接口<代码>阵列适配器
    没有。所有
    MergeAdapter
    所做的就是尝试使用传递给它的任何
    SectionIndexer
    实现,这样的实现已经传递了零次

  • 您试图在
    MergeAdapter
    中多次重复使用同一
    适配器
    实例,我不知道这样做是否有效,当然不推荐这样做


  • 为了解决这些问题,请为每个部分创建单独的适配器,并让这些适配器实现
    SectionIndexer

    我正在重新使用适配器,因为我想建立一个简单的示例,让您了解我正在尝试做什么,在我的应用程序中,我没有这样做。我没有使用SectionIndexer,我没有意识到我必须在自定义适配器中实现它。我现在已经实现了SectionIndexer,它正在按预期工作。非常感谢你的帮助@Commonware,我正在努力从mergeadapter listview的onItemClick事件中找到适配器实例,public void onItemClick(AdapterView父对象,视图视图,int位置,长id){if(mergeadapter.getItem(position)instanceof FeedReferenceQuestionAdapter){LivLog.info(getClass(),“订阅源提到问题适配器已单击”);}如果(mergeadapter.getItem(position)instanceof FeedQuestionCursorAdapter){LivLog.info(getClass(),“订阅源问题适配器已单击”);}
    public int getSectionNumber(int position)
        {
            int section = 0;
    
            for (ListAdapter piece : getPieces())
            {
                int size = piece.getCount();
    
                if (position < size)
                {
                    return section-=1;
                }
    
                position -= size;
    
                if(size == 1)
                {
                    if(piece.getItem(0) instanceof TextView)
                    {
                        TextView tv = (TextView) piece.getItem(0);
                        if(tv.getId() == 1000)
                        {
                            section++;  
                        }
                    }       
                }
            }
    
            return (-1);
        }