-7

I want to refresh my custom ListView in Android. My application crashed by using the following code...

MyActivity.java

package com.example.customview;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity  
{

    ListView lv;
    String name[]={"Ankit","Arora","Aru","abc"};
    String phn_no[]={"123","122","121","1211"};
    Integer[] arr_img={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
    CustomAdapter obj;
Button btn[];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv=(ListView) findViewById(R.id.listView1);
    btn=new Button[name.length];

    for(int i=0;i<name.length;i++)
    {
        btn[i]=(Button) findViewById(R.id.button1);
    }

        obj=new CustomAdapter(MainActivity.this,name,phn_no,arr_img);
        lv.setAdapter(obj);
}
}

CustomAdapter.java

package com.example.customview;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.customview.*;

@SuppressLint("ShowToast")
public class CustomAdapter extends ArrayAdapter<String> 
{
String nam[];
String no[];
Integer img[];
Context con;
    public CustomAdapter(Context con, String name[],String phn_no[],Integer arr_img[])
    {

        super(con,R.layout.customisedview,name);
        this.con=con;
        nam=name;
        this.img = arr_img;

        no=phn_no;

    }
    @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

        LayoutInflater li=  (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView=li.inflate(R.layout.customisedview, null);
        final ImageView imgv= (ImageView) convertView.findViewById(R.id.imageView1);
        final TextView tv1=(TextView) convertView.findViewById(R.id.textView1);
        final TextView tv2=(TextView) convertView.findViewById(R.id.textView2);
        final Button    btn=(Button) convertView.findViewById(R.id.button1);
        final ArrayList<String> name,num;
        final List<Integer> imag;
        name=(ArrayList<String>) Arrays.asList(nam);
        num=(ArrayList<String>) Arrays.asList(no);
        imag=Arrays.asList(img);


            btn.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                name.remove(position);
                num.remove(position);
                imag.remove(position);
                //notifyDataSetChanged();
                //Toast.makeText(CustomAdapter.this,"Delete" ,5555);
                }

            }
            );


        tv1.setText(nam[position]);
        tv2.setText(no[position]); 
        imgv.setImageResource(img[position]);






            return convertView;
        }

}

Now i want a list that consist of a delete button followed by the image and two text views and when i click on that button corresponding view gets deleted. I don't want that my new class i.e CustomAdapter is in the same file as in which MyActivity.java exits. Can Anyone tell me the problem & when I replaces onclick method with this code i don't get the required result.

    public void onClick(View v)
{   if(tv1.getText().toString()==nam[0])
                    {
                    nam[0]="";
                    tv1.setText(nam[0]);
                    no[0]="";
                    tv2.setText(no[0]);
                img[0]=(int) '\0';
            imgv.setImageResource(img[0]);
                                    }
                    else if(tv1.getText().toString()==nam[1])
                    {
                        nam[1]="";  
                        tv1.setText(nam[1]);
                        no[1]="";
                        tv2.setText(no[1]);

                        img[1]=(int)'\0';
                        imgv.setImageResource(img[1]);

                    }
                    else if(tv1.getText().toString()==nam[2])
                    {
                        nam[2]="";  
                        tv1.setText(nam[2]);                        
                        no[2]="";
                        tv2.setText(no[2]);
                        img[2]=(int)'\0';
                        imgv.setImageResource(img[2]);

                    }
                    else 
                    {
                    img[3]=(int) '\0';
                    imgv.setImageResource(img[3]);

                    no[3]="";
                    tv2.setText(no[3]);

                    nam[3]="";  
                    tv1.setText(nam[3]);

                    }
                }

            });

Can anyone judge me to go in right direction??

1 Answers1

0

There is a java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList in your code. Please use following code in your getView

final List<List<Integer>> imag = new ArrayList<List<Integer>>();

instead of

final List<Integer> imag;

Please follow the link to convert image array to List

Community
  • 1
  • 1
Shoeb Siddique
  • 2,665
  • 1
  • 18
  • 38