2011-11-22 49 views
0

我想在我的列表视图中添加一个按钮。但是按钮没有显示出来。我正在使用json解析从我的服务器获取数据。我正在使用hashmap和listviews。此外,我不能在列表视图中添加按钮。这里是我的代码:android listview button

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> 

的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> 
+1

你应该行XML文件中添加按钮。和android:focusable = false。 –

+0

@ Samir nope ...这是没有帮助! – Chandeep

+0

是只有按钮不显示或列表? textviews呢? – Rakhita

回答

8
如果你想

按钮,把一个按钮,在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! 
} 
}