如何使与嵌套的可点击按钮自定义列表项ListActivity [英] How to make a ListActivity with custom List Item with nested clickable Button

查看:318
本文介绍了如何使与嵌套的可点击按钮自定义列表项ListActivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个活动,显示用户名列表。确定它的工作原理。但是,我需要的是显示与左侧的用户名和一个按钮在右边的自定义列表视图项(此按钮会尽到一个编程给出电话号码的呼叫)。

now i have a activity, that shows a list of usernames. OK it works. But that i need is to show a custom listview item with the username at the left and a button at the right (this button will do a call to a programatically given phone number).

我知道我必须做一个自定义的适配器,但我的技能是太低了。而且我不知道如何使用它时,我已经做了。

I know that i have to do a custom adapter, but my skills are too low. And also i dont know how to use it when i have done.

有人可以帮助我做一个简单的自定义适配器来管理我的列表项与一个TextView的和一个按钮,让我有code使用该自定义适配器?

Can someone help me making a easy custom adapter to manage my listitem with one textview and one button and giving me the code to use that custom adapter?

适配器应当能够使用此新list_item2.xml:

the adapter should be able to use this new list_item2.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
        <TextView 
        android:id="@+id/friendName"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true"
        android:padding="10dp"
        android:textSize="16sp"/>
        <Button
        android:id="@+id/callButton"
        android:layout_alignBaseline="@id/friendName"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Call"
        android:width="100px"
        android:layout_alignParentRight="true"/>
</RelativeLayout> 

这是我现在使用code:

this is the code that i use now:

list_item.xml:

list_item.xml:

xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

这是listactivity类:

this is the listactivity class:

public class AllActivity extends ListActivity {

    RemoteConnection con; //conexion remota
    //private MyDbAdapter mDbHelper;

    private List<Friend> friends; //lista de amigos
    private List<String> usernames; //lista de usernames de amigos, para rellenar el listview
    //private List<Permission> permissions;

    //para almacenar la config local de mi app, mostrarme o no en el mapa...
    static SharedPreferences settings;
    static SharedPreferences.Editor configEditor;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        settings=PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
        configEditor = settings.edit();

        friends = new ArrayList<Friend>();
        usernames = new ArrayList<String>();

        //mDbHelper=MyApplication.getDatabaseAdapter();
        con = new RemoteConnection();

        actualizar();

    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 1) {
            setResult(1);
            finish();                   
        }
    }
    public void onResume() {
        super.onResume();
        actualizar();       
   }

    public void actualizar()
    {
        //friends = con.RetrieveFriends(settings.getString("login",""));
        friends = MyApplication.getDatabaseAdapter().retrieveAllFriends();
        usernames.clear();

        for (int i=0;i<friends.size();i++)
        //for (int i=0;i<permissions.size();i++)
        {
            usernames.add(i,friends.get(i).getFullName());
            //if (friends.get(i).getLastPosition()!=null)
            //  usernames.add(i,friends.get(i).getLastPosition().getpositiontimeFormated());    
        }
        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item3, usernames));
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {


              Bundle bundle = new Bundle(); //bundle is like the letter
              bundle.putString ("user", friends.get(position).getFullName()); //arg1 is the keyword of the txt, arg2 is the txt
              bundle.putString ("email", friends.get(position).getEmail());
              bundle.putString ("permission", friends.get(position).getPermission());

              Intent i=null;
              if (friends.get(position).getPermission().equals("total"))
                  i = new Intent (AllActivity.this, Locate.class);
              else if (friends.get(position).getPermission().equals("perhours"))
                  i = new Intent (AllActivity.this, LocatePerHours.class);
              else
                  i = new Intent (AllActivity.this, LocatePerDays.class);

              i.putExtras(bundle);
              startActivity(i);
              //startActivityForResult(i, 0);
          }
        });
    }

}

推荐答案

我编辑我的code是与你的兼容。

i edited my code to be compatible with yours

我写你的code。与修改来实现你想要的...我叫InitialActivity哟YHE ListActivity和一个字符串数组填充列表(填充它们是不是你的问题),所以,也许其他的东西会帮助你......

i write your code with modification to achieve what you want... i call InitialActivity yo yhe ListActivity and populate the list from a string array (populate them is not your problem) so, maybe the other things will help you...

package com.franco.test2;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class InitialActivity extends ListActivity {


//  private List<Friend> friends; //lista de amigos
    private ArrayList<String> usernames; //lista de usernames de amigos, para rellenar el listview
    private String[] usernames_array;
    private ActivityList listAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
        usernames_array = getResources().getStringArray(R.array.usernames);

        actualizar();

    }

    public void actualizar()
    {
        //friends = con.RetrieveFriends(settings.getString("login",""));
//        friends = MyApplication.getDatabaseAdapter().retrieveAllFriends();
//        usernames.clear();

//        for (int i=0;i<usernames.size();i++)
        //for (int i=0;i<permissions.size();i++)
//        {
//            usernames.add(i,friends.get(i).getFullName());
            //if (friends.get(i).getLastPosition()!=null)
            //  usernames.add(i,friends.get(i).getLastPosition().getpositiontimeFormated());    
//        }

        usernames = new ArrayList<String>();
        for(int i = 0 ; i < usernames_array.length ; i++){
            usernames.add(usernames_array[i]);
        }
        listAdapter = new ActivityList(InitialActivity.this, android.R.id.text1, usernames);


//        setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item3, usernames));
        setListAdapter(listAdapter);
        ListView lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {



              //startActivityForResult(i, 0);
          }
        });
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
         Bundle bundle = new Bundle(); //bundle is like the letter
         bundle.putString ("user", listAdapter.getItem(position)); //arg1 is the keyword of the txt, arg2 is the txt
//         bundle.putString ("user", friends.get(position).getFullName()); //arg1 is the keyword of the txt, arg2 is the txt
//         bundle.putString ("email", friends.get(position).getEmail());
//         bundle.putString ("permission", friends.get(position).getPermission());

//         Intent i=null;
//         if (friends.get(position).getPermission().equals("total"))
//             i = new Intent (AllActivity.this, Locate.class);
//         else if (friends.get(position).getPermission().equals("perhours"))
//             i = new Intent (AllActivity.this, LocatePerHours.class);
//         else
//             i = new Intent (AllActivity.this, LocatePerDays.class);
//
//         i.putExtras(bundle);
//         startActivity(i);
    }

    private OnClickListener callBtnListener = new OnClickListener(){

        public void onClick(View v) {
            Log.i("Info","Username: " + v.getTag());
        }};

    class ActivityList extends ArrayAdapter<String>{

        public ActivityList(Context context, int textViewResourceId, ArrayList<String> objects) {
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView == null){
                LayoutInflater inflater = LayoutInflater.from(InitialActivity.this);
                convertView = inflater.inflate(R.layout.new_list_item2, parent, false);
                holder = new ViewHolder();

                holder.text = (TextView)convertView.findViewById(R.id.friendName);
                holder.button = (Button)convertView.findViewById(R.id.callButton);

                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.text.setText(getItem(position));
            holder.button.setOnClickListener(callBtnListener);
            holder.button.setTag( getItem(position) );
            return convertView;
        }

    }

    static class ViewHolder{
        TextView text;
        Button button;
    }

}

我也删除了最后一行(的onClick),并在活动列表添加onClickListener。

i also deleted the last line (onClick) and add an onClickListener on the list activity.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
        <TextView 
            android:id="@+id/friendName"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_alignParentLeft="true"
            android:padding="10dp"
            android:textSize="16sp"/>
        <Button
            android:id="@+id/callButton"
            android:layout_alignBaseline="@id/friendName"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Call"
            android:width="100px"
            android:layout_alignParentRight="true"/>
</RelativeLayout>

我在设备和工程测试,我删除之前在DataHolder类。

i tested in my device and works, i delete the DataHolder class.

其他的东西,如捆绑地图,意图,因为我觉得是不是你的问题是评论说。

The other things like the Bundle map, Intent, is commented because i think is not your problem.

欢呼声。

这篇关于如何使与嵌套的可点击按钮自定义列表项ListActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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