【问题标题】:Get ImageView id inside multiple LinearLayout获取多个LinearLayout内的ImageView id
【发布时间】:2016-10-11 03:05:05
【问题描述】:

我正在使用包含图像和按钮的自定义列表视图。

我的 listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout">

    <ListView android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
/>
</LinearLayout>

我在这里处理 3 个线性布局。

我的button_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"
android:id="@+id/parent"
>

   <LinearLayout
    android:orientation="horizontal"  android:layout_width="match_parent" android:layout_height="match_parent"  android:id="@+id/child1"
    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/image"
        />
    <TextView
        android:id="@+id/listarray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"  android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"  android:id="@+id/child2">
    <ImageButton
        android:id="@+id/one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/one"
        />
    <ImageButton
        android:id="@+id/two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/two"
        />
    <ImageButton
        android:id="@+id/three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />
</LinearLayout>

适配器代码

tweetArray 包含我的列表。

ArrayAdapter<String> arrayAdapter =new ArrayAdapter<String>(this,R.layout.button_view, R.id.listarray, tweetArray);
            ListView listView = (ListView) findViewById(android.R.id.list);
            listView.setAdapter(arrayAdapter);

问题:我的问题是如何获得ImageView 的ID。

试过了:我尝试过简单的方法

 ImageView user_picture = (ImageView) findViewById(R.id.image);
 Picasso.with(getApplicationContext()).load("http://www.w3schools.com/css/trolltunga.jpg).into(user_picture);

但这显示错误目标不能为空

你有什么想法吗...?

【问题讨论】:

  • 如果该代码在您的Adapter 中,您需要在为列表项充气的View 上调用findViewById()
  • 怎么做@MikeM。
  • 您需要为您的Adapter 发布代码。
  • downvoters 也请您分享原因。这可能会有所帮助。
  • 这不是您使用自定义项目布局创建ListView 的方式。您需要实现一个自定义的Adapter 类。看看this post

标签: android android-layout listview picasso


【解决方案1】:

尝试自定义您的适配器 请检查适配器的链接

http://www.vogella.com/tutorials/AndroidListView/article.html

【讨论】:

  • 只有链接的答案是 Stack Overflow 上的 not considered good practise。问题“答案”实际上应该包含一个答案,或者至少是一个内容摘要以及它与问题的关系。
  • 结果为空
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多