简体   繁体   中英

Android: How to make a ListActivity with BaseAdapter

I'm trying to make a LIstActivity with a BaseAdapter, that displays just six EditText in vertical order, but stuck at displaying items. Can anybody give me an advice for this? Below is my code and layout.

Code:

public class FollowMe extends ListActivity {
private FollowMeAdapter2 mFMA;
private EditText[] et_list = new EditText[6];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    for(int i=0; i<et_list.length; i++){
        et_list[i] = new EditText(this);
    }

    mFMA = new FollowMeAdapter2(this, et_list);
    setListAdapter(mFMA);
}

public class FollowMeAdapter2 extends BaseAdapter {
    private EditText[] et;
    private Context mContext;

    public FollowMeAdapter2(Context context, EditText[] et){
        this.mContext = context;
        this.et = et;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder viewHolder;

        if (convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater)this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.row_follow_me, null);

            viewHolder = new ViewHolder();
            viewHolder.et = (EditText) view.findViewById(R.id.editText1);

            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }

        return view;
    }

    public class ViewHolder{
        public EditText et;
    }
}

}

Layout: row_follow_me.xml for getView method


    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
        <EditText android:text="EditText" android:id="@+id/editText1"     android:layout_width="wrap_content" android:layout_height="wrap_content">
    </LinearLayout>

just replace this code and check

   public int getCount() {
        // TODO Auto-generated method stub
        return et.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM