Gunaseelan
Gunaseelan

Reputation: 15515

working with footerview of listview

I have a listview with footerview. I have added setOnClickListener for listview. If I click the list row it will display a toast message. everything working fine. But my problem is suppose if I press the footerview (I didn't need, But there is possibility to user click it) the application get crashed. I dont know how to resolve this problem. I am tried to control with the view type. Bit I dont know how to find a view is TextView or not?(My footerview is a TextView). Is there any possiblity to solve this Issue.

What I am want?

if user clicked on the footerview nothing should happen.

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 coding

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();

}
});

And My logcat is:

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)

Every row has two strings except footerview. So I want to ignore the click event of footerview. Is it possible?

Thanks in advance.

Upvotes: 0

Views: 125

Answers (1)

Triode
Triode

Reputation: 11359

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

Make sure that

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

The size of str is always greater that 1.

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

Or

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

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

Upvotes: 1

Related Questions