简体   繁体   中英

Can anyone tell me why nothing is happen on the click of button?

Not able to refresh the list..

I have two java file MainActivity.java and CustomAdapter.java

MainActivity.java

package com.dv.deletev;

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

public class MainActivity extends Activity {

    ListView lv;
    String name[]={"Ankit","Arora","Arun","yadav"};
    String no[]={"AnAnaAN","cccc","bbbbb","aaa"};
    static CustomAdapter obj;



    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv=(ListView)findViewById(R.id.listView1);

        obj=new CustomAdapter(MainActivity.this,name,no);

        lv.setAdapter(obj);

    }

    public static  CustomAdapter take()
    {
        return obj;
    }
}

and

CustomAdapter.java

package com.dv.deletev;

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

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.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.dv.deletev.*;

public class CustomAdapter extends ArrayAdapter<String>
{

String name[];
String no[];
    Context con;
    MainActivity a;
    public CustomAdapter(Context con,String a[],String b[])
    {
        super(con,R.layout.second,a);
        name=a;
        this.con=con;   
        no=b;
        }



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
    LayoutInflater lv=(LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView=lv.inflate(R.layout.second, null);

    TextView tv1=(TextView)convertView.findViewById(R.id.textView1);
    TextView tv2=(TextView)convertView.findViewById(R.id.textView2);
    Button  bt=(Button)convertView.findViewById(R.id.button1);      
        final List<String> arr1;
        final List<String> arr2;
        arr1=(List<String>) Arrays.asList(name);
        arr2=(List<String>) Arrays.asList(no);

        final ArrayList<String> arr=new ArrayList<String>(arr1);
        final ArrayList<String> ar=new ArrayList<String>(arr2);


    bt.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
        arr.remove(position);
        ar.remove(position);
        a.take().notifyDataSetChanged();
        }

    });
        tv1.setText(name[position]);
        tv2.setText(no[position]);

        return convertView;
    }







}

I don't know why on the click of button its not perform onclick function. I checked the code many times and its correct.

You can check output at: http://postimg.org/image/5fxs20wgp/ Any suggestions would be remarkable.. :)

Please use ViewHolder pattern to improve your performance, http://developer.android.com/training/improving-layouts/smooth-scrolling.html

I think onClick works, but you don`t see it, because you have to notify your list adapter, when removing an item.

CustomAdapter.this.notifyDatasetChanged();

To see that, onClick really works please add Log.e("1","1"); inside the onClick, I m sure you are going to see the log, so actually onClick get s called right now, you are forgetting to fresh the adapter.

  @Override
    public void onClick(View v)
    {
    Log.e("1","onClick");
    arr.remove(position);
    ar.remove(position);
    a.take().notifyDataSetChanged();
    }

ok there are a couple of things that are wrong here. 1)notify the adapter
2)use view holder design pattern
3)since the button is inside list view the focus goes to list view, implement view holder and it will start working.

public class MainActivity extends Activity {
    ListView lv;
    String name[]={"Ankit","Arora","Arun","yadav"};
    CustomAdapter obj;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv=(ListView)findViewById(R.id.listView1);
        obj=new CustomAdapter(MainActivity.this,name,no);
        lv.setAdapter(obj);
        obj.notifyDataSetChanged();
    }
}

and change your adapter to

public class CustomAdapter extends ArrayAdapter<String>
{
    String name[];
    Context con;
    public CustomAdapter(Context con,String a[])
    {
        name=a;
        this.con=con;   
    }

static class Holder{
Button buton;
}

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater lv=(LayoutInflater)       con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView=lv.inflate(R.layout.second, null);
    Holder holder = new Holder;
    if(convertView==null){
    Button  bt=(Button)convertView.findViewById(R.id.button1);
    convertView.setTag(holder);      
    }else{
      holder=convertView.getTag(holder)
    }   

    holder.button.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
             Log.d("","inside click")
             todo embed logic here 
        }

    });
        return convertView;
    }

After this line in your MainActivity.java

obj=new CustomAdapter(MainActivity.this,name,no);

lv.setAdapter(obj);

You can write this

obj.notifyDataSetChanged();

CustomAdapter.java


 TextView tv1=(TextView)convertView.findViewById(R.id.textView1);
    TextView tv2=(TextView)convertView.findViewById(R.id.textView2);
    Button  bt=(Button)convertView.findViewById(R.id.button1);      
        final List<String> arr1;
        final List<String> arr2;
        arr1=(List<String>) Arrays.asList(name);
        arr2=(List<String>) Arrays.asList(no);

        final ArrayList<String> arr=new ArrayList<String>(arr1);
        final ArrayList<String> ar=new ArrayList<String>(arr2);


    bt.setOnClickListener(new Listener()); 
        tv1.setText(name[position]);
        tv2.setText(no[position]);

        return convertView;
    }
class Listener implements OnClickListener
{
 @Override
        public void onClick(View v)
        {
        arr.remove(position);
        ar.remove(position);
        a.take().notifyDataSetChanged();
        }

}

CustomAdapter

public CustomAdapter(Context con, String a[], String b[]) {
    super(con, R.layout.second, a);
    name = a;
    this.con = con;
    no = b;
}

public static class ViewHolder {
    // TODO Auto-generated method stub
    TextView tv1, tv2;
    Button bt;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    LayoutInflater inflater;
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = new View(mContext);
        convertView = inflater.inflate(R.layout.second, null);
        holder.tv1 = (TextView) convertView.findViewById(R.id.textView1);
        holder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
        holder.bt = (Button) convertView.findViewById(R.id.button1);
        convertView.setTag(v);
        final List<String> arr1;
        final List<String> arr2;
        arr1 = (List<String>) Arrays.asList(name);
        arr2 = (List<String>) Arrays.asList(no);

        final ArrayList<String> arr = new ArrayList<String>(arr1);
        final ArrayList<String> ar = new ArrayList<String>(arr2);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    v.bt.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            arr.remove(position);
            ar.remove(position);
            notifyDataSetChanged();
        }
    });
    holder.tv1.setText(name[position]);
    holder.tv2.setText(no[position]);
    return convertView;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM