0

这是在 android 中使用 BaseAdapter 的自定义列表视图的代码,它工作正常:

public class CustomListAdapter extends BaseAdapter {

    private ArrayList<TaskClass> _listData;
    Context _c;

    public CustomListAdapter(Context context, ArrayList<TaskClass> listData) {

        _listData = listData;
        _c = context;

    }

    @Override
    public int getCount() {
        return _listData.size();
    }

    @Override
    public Object getItem(int position) {
        return _listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position,View convertView,ViewGroup parent) {
        View v = convertView;
        if (v == null) 
        {
            LayoutInflater layoutInflator = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = layoutInflator.inflate(R.layout.listview_row, null);
        }
        TextView titleText = (TextView)v.findViewById(R.id.holdTitleText);
        TextView  catText = (TextView)v.findViewById(R.id.holdCatText);
        TextView  descText = (TextView)v.findViewById(R.id.holdDescText);
        TextView  dateText = (TextView)v.findViewById(R.id.holdDateText);
        //CheckBox checkBoxForEachItem = (CheckBox)v.findViewById(R.id.)
        TaskClass taskClassInstance = _listData.get(position);
        titleText.setText(taskClassInstance.getTitle());
        catText.setText(taskClassInstance.getTaskCategory());
        descText.setText(taskClassInstance.getDescription());
        dateText.setText(taskClassInstance.getTaskDate());
        return v;


    }


}

并在活动中使用自定义适配器绑定列表视图:

listViewInstance.setAdapter(new CustomListAdapter(getApplicationContext(),taskClasslistInstance));

其中“taskclasslistinstance”是我的arraylist conatining data from DB 它现在工作正常我需要为listitemclick编写函数,这样当用户单击任何listitem时,我可以从数据库中获取 该listitem记录的rowid 。所以在获得rowwid后我可以从中删除记录listview 和来自 db 并且可以编辑信息

4

3 回答 3

1

好吧,我们不知道你的TaskClass,但我希望你想要这样的东西:

listViewInstance.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        TaskClass taskClass = taskClasslistInstance.get(pos);
        Log.d(TAG, "Clicked on: " + taskClass)
        // Do stuff with taskClass
    }});
于 2013-06-21T11:22:29.123 回答
0

您也可以在 getView 方法中设置,如下所示:

public View getView(int position,View convertView,ViewGroup parent) {
        View v = convertView;
        if (v == null) 
        {
            LayoutInflater layoutInflator = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = layoutInflator.inflate(R.layout.listview_row, null);
        }
        TextView titleText = (TextView)v.findViewById(R.id.holdTitleText);
        TextView  catText = (TextView)v.findViewById(R.id.holdCatText);
        TextView  descText = (TextView)v.findViewById(R.id.holdDescText);
        TextView  dateText = (TextView)v.findViewById(R.id.holdDateText);
        //CheckBox checkBoxForEachItem = (CheckBox)v.findViewById(R.id.)
        TaskClass taskClassInstance = _listData.get(position);
        titleText.setText(taskClassInstance.getTitle());
        catText.setText(taskClassInstance.getTaskCategory());
        descText.setText(taskClassInstance.getDescription());
        dateText.setText(taskClassInstance.getTaskDate());

       v.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //Perform an action when this list item is clicked
        }
    });
        return v;


    }
于 2013-06-21T11:25:38.270 回答
0

这是我项目中的一个小例子:

 list = (ListView) rootView.findViewById(R.id.list);

    adapter = new LazyAdapterAlbum(getActivity(), songs);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent i = new Intent(getActivity(), AlbumActivity.class);
            Bundle args = new Bundle();
            args.putString(AlbumActivity.ALBUM_NAME, adapter.getItem(position).getName());
            i.putExtras(args);
            startActivity(i);
        }
    });

您必须在 ListView 上设置一个 onClickListener。

于 2013-06-21T11:24:16.613 回答