0

我有一个listview. footerview我已经添加setOnClickListenerlistview. 如果我单击列表行,它将显示一条 toast 消息。一切正常。但我的问题是假设如果我按下footerview(我不需要,但用户有可能点击它)应用程序会崩溃。我不知道如何解决这个问题。我试图用视图类型来控制。有点我不知道如何找到一个视图是TextView或不是?(我footerview是一个TextView)。有没有可能解决这个问题。

我想要什么?

如果用户单击页脚视图,则不会发生任何事情。

ListView lv1 = (ListView) findViewById(R.id.myListView);

ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.txnlist,
                new String[] { "name", "mark"}, new int[] {
                        R.id.text1, R.id.text2 }) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.txnlist, null);
                }
                Collection<Object> str = mylist.get(position).values();
                ArrayList<Object> al1 = new ArrayList<Object>(str);
                TextView amnt = (TextView) v.findViewById(R.id.txntext2);
                if (Integer.parseInt(al1.get(1).toString()) == 1)
                    amnt.setTextColor(Color.GREEN);
                else if (Integer.parseInt(al1.get(1).toString()) == 2)
                    amnt.setTextColor(Color.RED);
                return super.getView(position, v, parent);
            }
        };
TextView MyText = new TextView(this);
MyText.setText("Finished");
lv1.addFooterView(MYText);
lv1.setAdapter(adapter);

setOnClickListener 编码

lv1.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

Collection<Object> str = mylist.get(arg2).values();
ArrayList<Object> al1 = new ArrayList<Object>(str);

Toast.makeText(getApplicationContext(),al1.get(0)+" : "+al1.get(1), Toast.LENGTH_SHORT).show();

}
});

我的 logcat 是:

04-30 17:24:34.908: E/AndroidRuntime(2619): FATAL EXCEPTION: main
04-30 17:24:34.908: E/AndroidRuntime(2619): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
04-30 17:24:34.908: E/AndroidRuntime(2619):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at java.util.ArrayList.get(ArrayList.java:304)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at com.example.sample.DailyReportActivity$9.onItemClick(DailyReportActivity.java:396)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at android.widget.AbsListView$1.run(AbsListView.java:3168)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at android.os.Handler.handleCallback(Handler.java:605)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at android.os.Looper.loop(Looper.java:137)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at java.lang.reflect.Method.invokeNative(Native Method)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at java.lang.reflect.Method.invoke(Method.java:511)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-30 17:24:34.908: E/AndroidRuntime(2619):     at dalvik.system.NativeStart.main(Native Method)

除页脚视图外,每一行都有两个字符串。所以我想忽略footerview的点击事件。可能吗?

提前致谢。

4

1 回答 1

1
Toast.makeText(getApplicationContext(),al1.get(0)+" : "+al1.get(1), Toast.LENGTH_SHORT).show();

确保

Collection<Object> str = mylist.get(arg2).values();

的大小str总是大于 1。

if(al1.size() > 1)
{
    Toast.makeText(getApplicationContext(),al1.get(0)+" : "+al1.get(1), Toast.LENGTH_SHORT).show();
}

或者

ListView#addHeaderView(View v, Object data, boolean isSelectable);

使用此方法使页眉或页脚视图不可点击

于 2013-04-30T11:46:01.650 回答