android 列表视图按钮

2024-04-14

我正在尝试在列表视图中添加一个按钮。但按钮没有显示。我正在使用 json 解析从我的服务器获取数据。我正在使用哈希图和列表视图。此外,我也无法在列表视图中添加按钮。这是我的代码:

package acb.xiynove;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.res.Resources;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ImgJsonActivity extends ListActivity
{
    TestServiceActivity obj = new TestServiceActivity();
    String newString;
    int len;
    Button btn,btn1;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn=(Button)findViewById(R.id.btn);
        btn1=(Button)findViewById(R.id.btn1);

        newString=obj.readTwitterFeed();
        // define the list which holds the information of the list
        List<Map<String, Object>> resourceNames =
            new ArrayList<Map<String, Object>>();

        // define the map which will hold the information for each row
        Map<String, Object> data;

        // hard coded numbers retrieved from
        // http://code.google.com/android/reference/android/R.drawable.html
        try {

           JSONArray rootArray = new JSONArray(newString);
           len = rootArray.length();
//         List<String> listContents = new ArrayList<String>(len);

          for(int i=0;i<len;i++)
          {
             data = new HashMap<String, Object>();
            String x="";

            JSONObject post = rootArray.getJSONObject(i);
            x+= " " + post.getString("u")+ " " + post.getString("s") + " " + post.getString("e") + " on " + post.getString("pd") + " with " + post.getInt("a") + " ";                      
        data.put("line1", x);
            data.put("img", R.drawable.boy);

            data.put("btn", btn);
            data.put("btn1", btn1);
            resourceNames.add(data);
            //   listContents.add(x);

          }
//        myListView.setAdapter(new ArrayAdapter<String>(this, R.layout.listviewfont, listContents));     

           }
           catch (Exception e)
           {
           }

          SimpleAdapter notes = new SimpleAdapter(
                    this,
                    resourceNames,
                    R.layout.row,
                    new String[] { "line1", "img", "btn", "btn1" },
                    new int[] { R.id.text1, R.id.img, R.id.btn, R.id.btn1} );

                setListAdapter(notes);

    }
}

这是 row.xml

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

    <ImageView android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView android:id="@+id/text1"
            android:textSize="12sp"
            android:textStyle="bold"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>



        <Button android:id="@+id/btn"
        android:text="Show Interest"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content" />

        <Button android:id="@+id/btn1"
        android:text="Ask Query"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content" />
</LinearLayout>

主要.xml:

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

    <ListView
    android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
    android:drawSelectorOnTop="false"/>

    <TextView
        android:id="@id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="No data"/>

</LinearLayout>

如果您想要在列表项中添加按钮,请在 list_item.xml 中放置一个按钮。

制作一个自定义适配器:

class CustomAdapter extends BaseAdapter implements OnClickListener {
 .
 .
 .
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
       convertView = inflater.inflate(R.layout.list_item,
                    parent, false);
       holder = new ViewHolder();
       holder.button = (Button) convertView.findViewById(R.id.listButton);
       //find and assign other views here...
       convertView.setTag(holder);
   }else{
       holder = (ViewHolder) convertView.getTag();
   }
  // assign values to all view elements..
  holder.button.setTag(position);
  holder.button.setOnClickListener(this);
  }  //end of getView

public void onClick(View v) {
    int postition=(Integer)v.getTag(); // now you know wich button in list was clicked.
//do whatever you want. cheers!
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android 列表视图按钮 的相关文章

随机推荐

  • iPad 上的 HTML5 视频元素不会触发 onclick 或 touchstart 事件?

    我正在尝试将一些事件附加到 iPad Web 应用程序内的 HTML5 视频元素 但它们似乎没有触发 我已经在设备和模拟器中对此进行了测试 并得到了相同的结果 然而 这些事件 至少对于 onclick 在桌面 Safari 中运行良好 我也
  • Powershell 等待 dotnet run 在某个端口上启动应用程序

    我正在编写一个脚本 该脚本应该依次运行两个 dotnet 应用程序 根据其 launchSettings json 第一个位于端口 5000 第二个位于端口 5001 到目前为止 这是运行应用程序的脚本 app1ProjectFolder
  • 将事实收集到事实缓存的最快方法

    我正在尝试让 Ansible 与 limit 一起工作 为此我需要有关其他主机的事实 我正在使用fact caching进行缓存 我应该运行什么命令 以便它只收集所有主机上的所有事实并缓存它们 而不运行任何任务 像这样的东西设置模块 htt
  • 如何使用 Visual Studio 2015 模拟 iOS 后台获取

    我搜索了一段时间 但找不到如何使用 Visual Studio 2015 模拟 iOS 后台获取 似乎有一种方法可以在 XCode 中执行此操作 但我找不到 Visual Studio 2015 的任何内容 有人可以帮忙吗 打开项目选项并转
  • Git 从新克隆的存储库签出到分支会导致该分支中的未暂存更改

    我有一个奇怪的 git 问题 在检查了一个分支 远程跟踪的 后 我立即获得该分支中已修改和未暂存的更改 我再次克隆了存储库以验证问题是否仍然存在 我使用 Windows 和 gitblit 作为 Git 服务器 您知道为什么会发生这种情况吗
  • 如何使用 javascript 访问 Chrome 中的另一个选项卡?

    基本上 当我在 Chrome 控制台中运行此脚本时 我想获取当前 url 打开一个新选项卡 并将其设置为文本框的值 javascript function var url location href var newtab window op
  • 通过 JPA 关系进行 Bean 验证

    我想使用 Bean Validation 来注释实体中的约束 现在的问题是 实体上的关系也会得到验证吗 例如 假设我有以下实体 Entity Table name css empresa public class Empresa exten
  • 如何在 PostgreSQL 8.4 中从 string_to_array() 返回一个元素?

    我想解析具有以下类型值的字段 DAVE EBERT CONSTRUCTION 139 LENNOX STREET SANTA CRUZ CA 95060 公司电话号码 831 818 3170 我想做一个这样的查询 Update mytab
  • tkinter 错误的屏幕距离“10 0”

    My goal is to pady this section marked with a red circle slighty down Snippet of my code supposed to pady the last label
  • C# 和 C++ 中 ++i 运算符的区别

    我用 C 和 C 编写了以下代码 int i 0 i 11 此 C 编译器出现错误后 The left hand side of an assignment must be a variable property or indexer 但是
  • 如何禁用 Identityserver4 的 SSL

    为了测试目的 我需要在 DotNet Core 2 中禁用 IdentityServer4 的 SSL TSL 我看过这个链接 禁用 Identityserver3 的 SSL https stackoverflow com questio
  • ActiveRecord 迁移未填充 Postgres 物化视图

    我有一个MATERIALIZED VIEW这是通过迁移创建的 class MyView lt ActiveRecord Migration def up ActiveRecord Base connection execute lt lt
  • 如何在 mongoose js 中嵌套相同的模式

    我正在尝试使用 mongoose js 嵌套模式 特别是sameschema 创建树状结构 在此配置中 一个文档只能有 1 个父文档 但同一文档可以是多个子文档的父文档 以下是我最初的处理方式 var mongoose require mo
  • 读写模式Python [重复]

    这个问题在这里已经有答案了 可能的重复 python open内置函数 模式a a w w 和r 之间的区别 https stackoverflow com questions 1466000 python open built in fu
  • CSS 图像遮罩叠加

    我正在尝试获取一个透明的 png 框架图像 将其悬停在 img 标签上 以在其上创建框架的外观 我尝试了多种不同的方法 但似乎都不起作用 我使用的最新方法是http www cssbakery com 2009 06 background
  • 如何在 Struts 2 验证框架中验证布尔值

    简单 我必须确保选中表单上的隐私复选框 我尝试使用表达式验证器 和字段表达式验证器 但它不起作用 有谁能够帮助我 您能提供更多信息吗 您是如何使用表达式验证器的 为什么它不起作用 看来这种方法应该很好用 需要考虑一些事情 您的布尔值是否同时
  • 硒,是否存在多种元素之一?

    以答案为基础如何使用 Selenium for Python 等待页面加载 https stackoverflow com questions 26566799 how to wait until the page is loaded wi
  • 如何获取 Mac(Cocoa 或 C)的电池电量(以 mWh(而非百分比)为单位)

    标题几乎解释了一切 我正在创建一个 Mac 应用程序 我需要电池电量具体单位为毫瓦时 不是百分比 最好用 C 或 Objective C 来做 Thanks 抱歉 但我认为硬件没有办法报告这一点 随着电池进入其生命周期 它会随着时间的推移而
  • 以 json 形式返回时排除某些字段

    我有一个 ASP NET Web API 应用程序 现在假设应用程序由 User 实体和 Post 实体组成 帖子是由用户编写的 因此每个帖子实体都包含对用户实体的引用 class Post public int Id get set pu
  • android 列表视图按钮

    我正在尝试在列表视图中添加一个按钮 但按钮没有显示 我正在使用 json 解析从我的服务器获取数据 我正在使用哈希图和列表视图 此外 我也无法在列表视图中添加按钮 这是我的代码 package acb xiynove import java