简体   繁体   English

谁能告诉我为什么单击按钮后什么都没有发生?

[英]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 我有两个Java文件MainActivity.java和CustomAdapter.java

MainActivity.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 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. 我不知道为什么在单击按钮时不执行onclick功能。 I checked the code many times and its correct. 我检查了很多次代码及其正确性。

You can check output at: http://postimg.org/image/5fxs20wgp/ Any suggestions would be remarkable.. :) 您可以在以下位置检查输出: http : //postimg.org/image/5fxs20wgp/任何建议都将很明显.. :)

Please use ViewHolder pattern to improve your performance, http://developer.android.com/training/improving-layouts/smooth-scrolling.html 请使用ViewHolder模式来提高性能, 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. 我认为onClick可以使用,但是看不到它,因为在删除项目时必须通知列表适配器。

CustomAdapter.this.notifyDatasetChanged();

To see that, onClick really works please add Log.e("1","1"); 要知道,onClick确实有效,请添加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. 在onClick内,我m sure you are going to see the log, so actually onClick get现在m sure you are going to see the log, so actually onClick get调用了m sure you are going to see the log, so actually onClick get ,您忘记了刷新适配器。

  @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 1)通知适配器
2)use view holder design pattern 2)使用视图持有人的设计模式
3)since the button is inside list view the focus goes to list view, implement view holder and it will start working. 3)由于按钮位于列表视图内部,因此焦点将移至列表视图,因此实现了视图持有器,它将开始工作。

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 在MainActivity.java中的这一行之后

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

lv.setAdapter(obj); lv.setAdapter(OBJ);

You can write this 你可以写这个

obj.notifyDataSetChanged(); 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 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;
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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