Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

android - How to make a ListActivity with custom List Item with nested clickable Button

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.

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?

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> 

this is the code that i use now:

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>

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

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

i edited my code to be compatible with yours

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

}

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>

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.

cheers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...