Android的列表视图按钮 [英] android Listview button

查看:94
本文介绍了Android的列表视图按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的列表视图中添加一个按钮。但该按钮没有显示出来。我使用JSON解析从我的服务器获取数据。我使用HashMap和列表视图。而且我不能在列表视图ADDA按钮了。这是我的code:

 包acb.xiynove;

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

公共类ImgJsonActivity扩展ListActivity
{
    TestServiceActivity OBJ =新TestServiceActivity();
    字符串newString;
    INT LEN;
    按钮BTN,BTN1;

    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        BTN =(按钮)findViewById(R.id.btn);
        BTN1 =(按钮)findViewById(R.id.btn1);

        newString = obj.readTwitterFeed();
        //定义其保持列表的信息的列表
        列表与LT;地图<字符串,对象>> resourceNames =
            新的ArrayList<地图<字符串,对象>>();

        //定义将保留信息的每一行地图
        地图<字符串,对象>数据;

        //从中检索硬codeD号码
        // HTTP://$c$c.google.com/android/reference/android/R.drawable.html
        尝试 {

           JSONArray rootArray =新JSONArray(newString);
           的len = rootArray.length();
//名单,其中,字符串> listContents =新的ArrayList<字符串>(LEN);

          的for(int i = 0; I< LEN;我++)
          {
             数据=新的HashMap<字符串,对象>();
            字符串x =;

            JSONObject的帖子= rootArray.getJSONObject(我);
            X + =+ post.getString(U)++ post.getString(S)++ post.getString上(E)++ post.getString(PD) +以+ post.getInt(一)+;
        data.put(一号线,X);
            data.put(IMG,R.drawable.boy);

            data.put(BTN,BTN);
            data.put(BTN1,BTN1);
            resourceNames.add(数据);
            // listContents.add(X);

          }
// myListView.setAdapter(新ArrayAdapter<字符串>(这一点,R.layout.listviewfont,listContents));

           }
           赶上(例外五)
           {
           }

          SimpleAdapter注意到=新SimpleAdapter(
                    本,
                    resourceNames,
                    R.layout.row,
                    新的String [] {一号线,IMG,BTN,BTN1},
                    新的INT [] {R.id.text1,R.id.img,R.id.btn,R.id.btn1});

                setListAdapter(票据);

    }
}
 

这里的row.xml

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / VW1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向>

    < ImageView的机器人:ID =@ + ID / IMG
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>

    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>

        < TextView的机器人:ID =@ + ID / text1中
            机器人:TEXTSIZE =12SP
            机器人:TEXTSTYLE =黑体
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT/>

    < / LinearLayout中>



        <按钮机器人:ID =@ + ID / BTN。
        机器人:文本=明送秋波
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>

        <按钮机器人:ID =@ + ID / BTN1
        机器人:文本=请查询
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
 

main.xml中:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>

    <的ListView
    机器人:ID =@ ID / Android的:清单
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_weight =1
    机器人:drawSelectorOnTop =FALSE/>

    <的TextView
        机器人:ID =@ ID /安卓:空
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:文本=无数据/>

< / LinearLayout中>
 

解决方案

如果你想里面的列表项的按钮,把一个按钮,list_item.xml。

请自定义适配器:

 类CustomAdapter扩展了BaseAdapter实现OnClickListener {
 。
 。
 。
  公共查看getView(INT位置,查看convertView,ViewGroup中父){
    如果(convertView == NULL){
       convertView = inflater.inflate(R.layout.list_item,
                    父母,假);
       持有人=新ViewHolder();
       holder.button =(按钮)convertView.findViewById(R.id.listButton);
       //发现这里分配其他的意见?
       convertView.setTag(保持器);
   }其他{
       支架=(ViewHolder)convertView.getTag();
   }
  //赋值给所有视图元素..
  holder.button.setTag(位置);
  holder.button.setOnClickListener(本);
  } // getView结束

公共无效的onClick(视图v){
    INT =的体位(整数)v.getTag(); //现在你知道在列表至极按钮被点击。
//做你想做的。干杯!
}
}
 

I am trying to add a button in my list view. But the button is not showing up. I am using json parsing to take data from my server. I am using hashmap and listviews. Moreover i cannot adda button in the list view too. Here's my code:

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);

    }
}

Here's the 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>

Main.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>

解决方案

if you want buttons inside list items, put a button in the list_item.xml .

make a custom adapter :

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!
}
}

这篇关于Android的列表视图按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆