0

下面你可以看到代码,我为我的列表视图实现了一个简单的适配器。但我无法进入 onListItemClick。任何人都可以有建议吗?

实际上它正常显示列表,但我无法获取 onitemclick 事件。提前致谢。

            public class MyListActivity extends ListActivity {


        @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                ArrayList<Frame> results = WebOperations
                        .loadList_Frame();
                myAdapter = new MyListAdapter(MyListActivity.this);
                myAdapter.internalList = results;

                setListAdapter(myAdapter);
                myAdapter.notifyDataSetChanged();

        };

        @Override
            protected void onListItemClick(ListView l, View v, int position, long id) {
                String item = (String) getListAdapter().getItem(position);
                Toast.makeText(this, item + " please show toast!!!!!!!!!", Toast.LENGTH_LONG).show();
            }


        public static class MyListAdapter extends BaseAdapter {

                public ArrayList<Frame> internalList;
                public LayoutInflater mInflater;
                public int pageCount = 0;
                public MyListAdapter(Context context) {
                    mInflater = LayoutInflater.from(context);
                }
                public int getCount() {
                    if (internalList == null)
                        return 0;
                    return internalList.size();
                }
                public Object getItem(int position) {
                    if (internalList == null || internalList.size() < position)
                        return null;
                    return internalList.get(position);
                }
                public long getItemId(int position) {
                    if (internalList == null || internalList.size() < position)
                        return 0;
                    return internalList.get(position).getId();
                }
                public View getView(int position, View arg1, ViewGroup parent) {
                    View v = arg1;
                    if ((v == null) || (v.getTag() == null)) {
                        v = mInflater.inflate(R.layout.entryrow, null);
                        try {
                            String gunlukText = String.format(" %s ",
                                    internalList.get(position).getKeyText().toString());
                            TextView entry = (TextView) v
                                    .findViewById(R.id.textViewEntryText);
                            entry.setText((CharSequence) gunlukText);
                        } catch (Exception e) {
                            Log.d("aaaaaaaaaaaaaaaaaaa", "errorrrrrrrrrrr");
                        }
                    }
                    return v;
                }

            }
            }

编辑 1:我在下面添加 entry_row 布局 xml 文件。

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textViewEntryText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine"
                android:padding="5dp"
                android:paddingLeft="10dp"
                android:text="@string/Ana_Sayfa"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </LinearLayout>
4

4 回答 4

2

您应该考虑将您的侦听器添加到您的 listview :

getListView().setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       String item = (String) getListAdapter().getItem(position);
       Toast.makeText(this, item + " please show toast!!!!!!!!!", Toast.LENGTH_LONG).show();
    }
});
于 2012-02-29T08:54:29.167 回答
1

Didi 您尝试在您的布局/条目行上添加 addStatesFromChildrenattribute 并将其设置为 true ?

http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:addStatesFromChildren

于 2012-02-29T09:14:36.743 回答
1

在返回视图之前将 onclicklistener 添加到 getView 方法中。

v.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Log.w("position", position + "");
                }
            });

检查它是否有帮助..

于 2012-02-29T09:31:17.263 回答
0

请参考http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List13.html的自定义 BaseAdapter 的 API 演示中给出的一个很好的示例,只需覆盖onListItemCLicked并检查. 它工作得很好。尝试相应地修改您的代码。

于 2012-02-29T09:20:51.767 回答