Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击获取listview项目内的视图值_Android_Listview - Fatal编程技术网

Android 单击获取listview项目内的视图值

Android 单击获取listview项目内的视图值,android,listview,Android,Listview,我有一个包含不同xml布局的listview,每个布局都有不同的视图。我想知道如何从用户单击的项目中的视图中,在onItemClick方法中获取数据 我的listview由多个布局组成,因此我无法确定在调用onItemClick方法时按下的是哪个布局。例如,listview中布局中的一个项目是: // list_item.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" androi

我有一个包含不同xml布局的listview,每个布局都有不同的视图。我想知道如何从用户单击的项目中的视图中,在
onItemClick
方法中获取数据

我的listview由多个布局组成,因此我无法确定在调用onItemClick方法时按下的是哪个布局。例如,listview中布局中的一个项目是:

// list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <TextView
        android:id="@+id/lblSub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:padding="6dp"
        android:layout_marginRight="4dp"
        android:text=""
        android:textColor="#FFFFFF"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/lblMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:padding="6dp"
        android:layout_marginLeft="4dp"
        android:text=""
        android:textColor="#FFFFFF"
        android:textSize="18sp" />

</RelativeLayout>
我正在寻找一种方法来提取项目中所有视图的值。但是,被点击的项目可能是任何布局中的一个,这让我很困惑。有什么想法吗

编辑:

下面是我如何在代码中使用此适配器的示例:

SeparatedListAdapter adapter;
Customer cust;
ListView lvDetails;

public Map<String,?> createDoubleItem(String title, String caption) {  
        Map<String,String> item = new HashMap<String,String>();  
        item.put(ITEM_VALUE, title);  
        item.put(ITEM_CAPTION, caption);  
        return item;  
    }

    public Map<String,?> createSingleItem(String name) {  
        Map<String,String> item = new HashMap<String,String>();  
        item.put(ITEM_VALUE, name);
        return item;
    } 

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_customer);

        Intent intent = getIntent();
        cust = Db.Functions.getCustomer(intent.getStringExtra("uuid"));
        lvDetails = (ListView) findViewById(R.id.custdetails);
        adapter = new SeparatedListAdapter(this);
        lvDetails.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
                Gen.popup("Item clicked");
            }
        });

        // Name
        List<Map<String,?>> name = new LinkedList<Map<String,?>>();
        if (cust.firstName == null && cust.lastName == null)
            name.add(createSingleItem("Unnamed Customer"));
        else
            name.add(createSingleItem(
                    ((cust.title != null) ? cust.title + " " : "") +
                    ((cust.firstName != null) ? cust.firstName + " " : "") +
                    ((cust.lastName != null) ? cust.lastName : "")));
        adapter.addSection("Name", new SimpleAdapter(this, name, R.layout.list_item_name,   
                new String[] { ITEM_VALUE },
                new int[] { R.id.lblName }));
        // Phones
        if ((cust.mobileNumber != null || cust.homeNumber != null || cust.workNumber != null) && SHOW_PHONES)
        {
            List<Map<String,?>> phone = new LinkedList<Map<String,?>>();
            if (cust.mobileNumber != null)
                phone.add(createDoubleItem(cust.mobileNumber, "Mobile"));
            if (cust.homeNumber != null)
                phone.add(createDoubleItem(cust.homeNumber, "Home"));
            if (cust.workNumber != null)
                phone.add(createDoubleItem(cust.workNumber, "Work"));
            adapter.addSection("Phone", new SimpleAdapter(this, phone, R.layout.list_item,   
                    new String[] { ITEM_VALUE, ITEM_CAPTION },
                    new int[] { R.id.lblSub, R.id.lblMain }));
        }  
        lvDetails.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
}
分离列表适配器;
客户客户;
列表视图详细信息;
公共地图createDoubleItem(字符串标题、字符串标题){
Map item=newhashmap();
项目。投入(项目价值、标题);
项目.放置(项目标题,标题);
退货项目;
}
公共映射createSingleItem(字符串名称){
Map item=newhashmap();
item.put(item_值、名称);
退货项目;
} 
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u view\u customer);
Intent=getIntent();
cust=Db.Functions.getCustomer(intent.getStringExtra(“uuid”);
lvDetails=(ListView)findViewById(R.id.custdetails);
适配器=新的分离列表适配器(此);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView列表视图、视图视图、整型位置、长id){
Gen.popup(“点击的项目”);
}
});
//名字
列表名称=新建LinkedList();
if(cust.firstName==null&&cust.lastName==null)
name.add(createSingleItem(“未命名客户”);
其他的
name.add(createSingleItem)(
((cust.title!=null)?cust.title+“”:“”)+
((cust.firstName!=null)?cust.firstName+“”:“”)+
((cust.lastName!=null)?cust.lastName:);
adapter.addSection(“名称”,新的SimpleAdapter(此,名称,R.layout.list\u项\u名称,
新字符串[]{ITEM_VALUE},
新的int[]{R.id.lblName});
//电话
如果((cust.mobileNumber!=null | | cust.homeNumber!=null | | | cust.workNumber!=null)&&SHOW_电话)
{
列表电话=新建链接列表();
if(cust.mobileNumber!=null)
添加(createDoubleItem(cust.mobileNumber,“Mobile”);
如果(cust.homeNumber!=null)
添加(createDoubleItem(cust.homeNumber,“Home”);
if(cust.workNumber!=null)
phone.add(createDoubleItem(cust.workNumber,“Work”));
adapter.addSection(“电话”),新的SimpleAdapter(此,电话,R.layout.list_项,
新字符串[]{ITEM_VALUE,ITEM_CAPTION},
新的int[]{R.id.lblSub,R.id.lblMain});
}  
lvDetails.setAdapter(适配器);
adapter.notifyDataSetChanged();
}
}

首先,您为该适配器提供的链接看起来非常旧(2008年,5年前),我会看看第三方库,如

但这不是重点

适配器的设计非常复杂,因为其中有多个适配器,所以如果不启动我自己的测试项目,我无法确定这将如何工作

现在,当您调用onItemClick时,您希望添加以下内容

int viewType = adapter.getItemViewType(position);
if (viewType == SeparatedListAdapter.TYPE_SECTION_HEADER) {
    //header clicked handle that here maybe do nothing
} else {
    Map<String,?> clickedItem = (Map<String,?>) adapter.getItem(position);
    String value = (String) clickedItem.Get(ITEM_VALUE);
    String caption = (String) clickedItem.Get(ITEM_CAPTION);
    //here you can do things with the clicked item
    //also because you know the view type you can find other views on it
}
int-viewType=adapter.getItemViewType(位置);
if(viewType==SeparatedListAdapter.TYPE\u SECTION\u HEADER){
//标题点击处理这里可能什么都不做
}否则{
Map clickedItem=(Map)adapter.getItem(位置);
字符串值=(字符串)单击editem.Get(项值);
字符串标题=(字符串)单击editem.Get(项标题);
//在这里,您可以对单击的项目执行操作
//另外,因为您知道视图类型,所以可以在其上找到其他视图
}

编辑:根据您提供的内容调整onclick的外观

您在适配器中存储了什么样的数据?只需将getItem()强制转换为您的数据类型,并使用方法访问字段。恐怕我没有跟上您的脚步。我修改了我的问题以使它更清楚。我真的很抱歉,但我不是很理解你。我不确定这里的
MyClass
代表了什么,或者我应该在里面放什么。我已经编辑了我的问题,以包括我如何在代码中使用此适配器的示例。
public class SeparatedListAdapter extends BaseAdapter {  

    public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>();  
    public final ArrayAdapter<String> headers;  
    public final static int TYPE_SECTION_HEADER = 0;  

    public SeparatedListAdapter(Context context) {  
        headers = new ArrayAdapter<String>(context, R.layout.list_header);  
    }  

    public void addSection(String section, Adapter adapter) {  
        this.headers.add(section);  
        this.sections.put(section, adapter);  
    }
    public void addSectionNoHeader(String section, Adapter adapter) {
        this.sections.put(section, adapter);
    }

    public Object getItem(int position) {  
        for(Object section : this.sections.keySet()) {  
            Adapter adapter = sections.get(section);  
            int size = adapter.getCount() + 1;  

            // check if position inside this section   
            if(position == 0) return section;  
            if(position < size) return adapter.getItem(position - 1);  

            // otherwise jump into next section  
            position -= size;  
        }  
        return null;  
    }  

    public int getCount() {  
        // total together all sections, plus one for each section header  
        int total = 0;  
        for(Adapter adapter : this.sections.values())  
            total += adapter.getCount() + 1;  
        return total;  
    }  

    public int getViewTypeCount() {  
        // assume that headers count as one, then total all sections  
        int total = 1;  
        for(Adapter adapter : this.sections.values())  
            total += adapter.getViewTypeCount();  
        return total;  
    }  

    public int getItemViewType(int position) {  
        int type = 1;  
        for(Object section : this.sections.keySet()) {  
            Adapter adapter = sections.get(section);  
            int size = adapter.getCount() + 1;  

            // check if position inside this section   
            if(position == 0) return TYPE_SECTION_HEADER;  
            if(position < size) return type + adapter.getItemViewType(position - 1);  

            // otherwise jump into next section  
            position -= size;  
            type += adapter.getViewTypeCount();  
        }  
        return -1;  
    }  

    public boolean areAllItemsSelectable() {  
        return false;  
    }  

    public boolean isEnabled(int position) {  
        return (getItemViewType(position) != TYPE_SECTION_HEADER);  
    }  

    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        int sectionnum = 0;  
        for(Object section : this.sections.keySet()) {  
            Adapter adapter = sections.get(section);  
            int size = adapter.getCount() + 1;  

            // check if position inside this section   
            if(position == 0) return headers.getView(sectionnum, convertView, parent);  
            if(position < size) return adapter.getView(position - 1, convertView, parent);  

            // otherwise jump into next section  
            position -= size;  
            sectionnum++;  
        }  
        return null;  
    }  

    @Override  
    public long getItemId(int position) {  
        return position;  
    }  

}  
SeparatedListAdapter adapter;
Customer cust;
ListView lvDetails;

public Map<String,?> createDoubleItem(String title, String caption) {  
        Map<String,String> item = new HashMap<String,String>();  
        item.put(ITEM_VALUE, title);  
        item.put(ITEM_CAPTION, caption);  
        return item;  
    }

    public Map<String,?> createSingleItem(String name) {  
        Map<String,String> item = new HashMap<String,String>();  
        item.put(ITEM_VALUE, name);
        return item;
    } 

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_customer);

        Intent intent = getIntent();
        cust = Db.Functions.getCustomer(intent.getStringExtra("uuid"));
        lvDetails = (ListView) findViewById(R.id.custdetails);
        adapter = new SeparatedListAdapter(this);
        lvDetails.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
                Gen.popup("Item clicked");
            }
        });

        // Name
        List<Map<String,?>> name = new LinkedList<Map<String,?>>();
        if (cust.firstName == null && cust.lastName == null)
            name.add(createSingleItem("Unnamed Customer"));
        else
            name.add(createSingleItem(
                    ((cust.title != null) ? cust.title + " " : "") +
                    ((cust.firstName != null) ? cust.firstName + " " : "") +
                    ((cust.lastName != null) ? cust.lastName : "")));
        adapter.addSection("Name", new SimpleAdapter(this, name, R.layout.list_item_name,   
                new String[] { ITEM_VALUE },
                new int[] { R.id.lblName }));
        // Phones
        if ((cust.mobileNumber != null || cust.homeNumber != null || cust.workNumber != null) && SHOW_PHONES)
        {
            List<Map<String,?>> phone = new LinkedList<Map<String,?>>();
            if (cust.mobileNumber != null)
                phone.add(createDoubleItem(cust.mobileNumber, "Mobile"));
            if (cust.homeNumber != null)
                phone.add(createDoubleItem(cust.homeNumber, "Home"));
            if (cust.workNumber != null)
                phone.add(createDoubleItem(cust.workNumber, "Work"));
            adapter.addSection("Phone", new SimpleAdapter(this, phone, R.layout.list_item,   
                    new String[] { ITEM_VALUE, ITEM_CAPTION },
                    new int[] { R.id.lblSub, R.id.lblMain }));
        }  
        lvDetails.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
}
int viewType = adapter.getItemViewType(position);
if (viewType == SeparatedListAdapter.TYPE_SECTION_HEADER) {
    //header clicked handle that here maybe do nothing
} else {
    Map<String,?> clickedItem = (Map<String,?>) adapter.getItem(position);
    String value = (String) clickedItem.Get(ITEM_VALUE);
    String caption = (String) clickedItem.Get(ITEM_CAPTION);
    //here you can do things with the clicked item
    //also because you know the view type you can find other views on it
}