2012年8月30日 星期四

自訂ListView元件 自訂Button 自已function

android 自訂Listview的資源滿多的,

但是自訂的Listview 要挷 很多 Button 怎麼做?

最近要做這一部份,

明明幾個月前有搞定,

可是就是忘了哩!


爬文也爬不到,

只好爬起自已的code。






android開發, 如何在ListView 使用 自訂 Button 自訂元件, Button 超過 二個 時, 要如何解決, 如下: mlist_view.xml

     
    


userlist_item.xml


 

   
   
             
             

         

   

    

   
   
   
 
 

ListActivity.java
public class ListActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mlist_view);
      ArrayList> list = new ArrayList>();

        int i = 0;
        
        while (i<20 hashmap="hashmap" string="string" tring="tring"> item =new HashMap();
         item.put("title", "title" + i);
         item.put("img", "img" + i);
            list.add(item);
                        
            i++;
         }
                    
         ListView listview = (ListView) findViewById(R.id.alist);
         ListApapter items = new ListApapter(A,
                list,
                R.layout.userlist_item,
                new String[] {"title","img"},   
                new int[] {R.id.textTitle,R.id.imageView}); 
              
         listview.setAdapter(items);
}

strings.xml

    
 
    music MIXER

ListApapter.java
package wolke.passingNotes;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.SimpleAdapter.ViewBinder;

public class ListApapter extends SimpleAdapter {
 private int[] mTo;
    private String[] mFrom;
    private ViewBinder mViewBinder;
    private List> mData;
    private int mResource;
    private int mDropDownResource;
    private LayoutInflater mInflater;
 public ListApapter(Context context, List> data,
   int resource, String[] from, int[] to) {
  super(context, data, resource, from, to);
  // TODO Auto-generated constructor stub
  mData = data;
     mResource = mDropDownResource = resource;
     mFrom = from;
        mTo = to;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

 }
 public View getView(int position, View convertView, ViewGroup parent) {
  
        return createViewFromResource(position, convertView, parent, mResource);
    }
 private View createViewFromResource(int position, View convertView,
   ViewGroup parent, int resource) {
  // TODO Auto-generated method stub
  View v;
        if (convertView == null) {
            v = mInflater.inflate(resource, parent, false);
 
            final int[] to = mTo;
            final int count = to.length;
            final View[] holder = new View[count];
 
            for (int i = 0; i < count; i++) {
                holder[i] = v.findViewById(to[i]);
            }
 
            v.setTag(holder);
        } else {
            v = convertView;
        }
        bindView(position, v);
 
        return v;
 }
 
 private void bindView(int position, View view) {
  // TODO Auto-generated method stub
  final Map dataSet = mData.get(position);
        if (dataSet == null) {
            return;
        }
        
        final ViewBinder binder = mViewBinder;
        final View[] holder = (View[]) view.getTag();
        final String[] from = mFrom;
        final int[] to = mTo;
        final int count = to.length;
        
        for (int i = 0; i < count; i++) {
            final View v = holder[i];       
            if (v != null) {
                final Object data = dataSet.get(from[i]);
                String text = data == null ? "" : data.toString();
                if (text == null) {
                    text = "";
                }
                boolean bound = false;
                if (binder != null) {
                    bound = binder.setViewValue(v, data, text);
                }
                if (!bound) {

                 if (v instanceof ImageButton) { 
                  Log.d(""+text,v.toString());
                  //((ImageButton) v).setTag(position);                  
                  v.setTag(R.id.tag_button_type, text);
                  v.setTag(R.id.tag_button_position, position);
                  ((ImageButton) v).setOnClickListener(new OnClickListener(){

       public void onClick(View v) {
        // TODO Auto-generated method stub
        //Log.d("text",text);
        
        //int position = (Integer)((ImageButton) v).getTag();
        int position = (Integer) v.getTag(R.id.tag_button_position);
        String text = (String)  v.getTag(R.id.tag_button_type);
        //Map dataSet = mData.get(position);
        //String tel = (String) dataSet.get("tel");
        //String img = (String) dataSet.get("img");
        
        Toast.makeText(v.getContext(), text, Toast.LENGTH_SHORT).show();
        
        
       }});
                  
                  
                 }else if (v instanceof TextView) { 
                  ((TextView) v).setText(text);   
                 }
                 
                }
                
            }
        }
        
 
 }
}

部份引用自其他source ocde, 因為 東改西改, 故難然註明部份code出處, 若有引用到請見諒。

沒有留言:

張貼留言