动态listview侦听器(OnItemClickListener)

时间:2016-10-17 09:39:18

标签: android listview android-fragments onitemclicklistener

我想将OnItemClickListener变为动态列表视图。列表视图不应具有固定大小。相反,它是数据库中行的大小。但是我如何创建听众,我不能使用当前的方法,因为我不知道列表视图的大小。

如果(位置== 0){...}

,问题就在内部类别中
public class VisaUtgiftFragment extends Fragment {
    private MainActivity ui;
    private TextView tvDatabase;
    private MyDBHandler dbHandler;
    private ListView lvUtgift;



    public VisaUtgiftFragment() {
        // Required empty public constructor
    }

    public void setActivity(MainActivity ui){
        this.ui = ui;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_visa_utgift, container, false);
        tvDatabase = (TextView)view.findViewById(R.id.tvDatabaseTest);
        dbHandler = new MyDBHandler(getActivity(), null, null, 1);
        tvDatabase.setText(dbHandler.databaseToString());
        lvUtgift = (ListView)view.findViewById(R.id.lvUtgift);
//        lvUtgift.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, [the rows in the database]));
        lvUtgift.setAdapter(new SimpleCursorAdapter(getActivity(),
                android.R.layout.simple_list_item_1,
                dbHandler.getAllItemsInCursor(),
                new String[]{"_titel"},
                new int[]{android.R.id.text1},
                0));
        lvUtgift.setOnItemClickListener(new ListViewClicked());

        return view;
    }

    private class ListViewClicked implements android.widget.AdapterView.OnItemClickListener {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if(position==0) { //Should be the clicked listview and execute the code
                Cursor cursor = (Cursor) lvUtgift.getItemAtPosition(position);
                String string = cursor.getString(cursor.getColumnIndexOrThrow("_titel"));
                Toast.makeText(getActivity().getApplicationContext(), string, Toast.LENGTH_SHORT).show();
                ui.dVisaInkomst();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

private class ListViewClicked implements android.widget.AdapterView.OnItemClickListener { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Cursor cursor = (Cursor)lvUtgift.getItemAtPosition(position); if(cursor != null){ String string = cursor.getString(cursor.getColumnIndexOrThrow("_titel")); Toast.makeText(getActivity().getApplicationContext(), string, Toast.LENGTH_SHORT).show(); ui.dVisaInkomst(); } } } 删除if条件。

func (o *MyController) Get() {
    uri := o.Ctx.Input.URI()
    beego.Trace(uri)
}