使用listview的footerview

时间:2013-04-30 11:29:52

标签: android android-listview

我有listview footerview。我为setOnClickListener添加了listview。如果我单击列表行,它将显示一个Toast消息。一切正常。但我的问题是假设我按footerview(我不需要,但有可能用户点击它)应用程序崩溃了。我不知道如何解决这个问题。我试图用视图类型控制。有点我不知道如何查找视图TextView?(我的footerviewTextView)。是否有可能解决这个问题。

我想要什么?

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

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之外,每行都有两个字符串。所以我想忽略footerview的click事件。有可能吗?

提前致谢。

1 个答案:

答案 0 :(得分: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);

Use this method to make the header or footer View not clickable