Переопределить fun getView (position: Int, convertView: View?, parent: ViewGroup?): View { var root = convertView

    if(root == null)
      root = LayoutInflater.from(activity).inflate(R.layout.list_items, null, false)
    root!!.textViewName.text = data[position].name
    root!!.textViewName.setOnClickListener{
        var i1 = Intent(root!!.context, MainActivity::class.java)
        i1.putExtra("key2", data[position].name)
        i1.putExtra("key3", data[position].image)
       root.context.startActivity(i1)
    }

}

-2
blue-star 4 Фев 2022 в 04:10

2 ответа

Класс MyAdapter (активность var: Activity, данные var: ArrayList): BaseAdapter() {

override fun getCount(): Int {
    return data.size
}

override fun getItem(position: Int): Any {
    return data[position]
}

override fun getItemId(position: Int): Long {

    return data[position].id.toLong()
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    var root = convertView

    if(root == null)
      root = LayoutInflater.from(activity).inflate(R.layout.fruit_item, null, false)
    root!!.textViewName.text = data[position].name
    
    root!!.textViewName.setOnClickListener{
        var i1 = Intent(root!!.context, MainActivity::class.java)
        i1.putExtra("key2", data[position].name)
      
       root.context.startActivity(i1)
    }


    return root
}
0
blue-star 4 Фев 2022 в 04:18
The best for you please use interface 



 public class ListAdapter extends ArrayAdapter<String> {    
        customButtonListener customListner;    
    
        public interface customButtonListener {    
            public void onButtonClickListner(int position,String value);    
        }    
        
        public void setCustomButtonListner(customButtonListener listener) {    
            this.customListner = listener;    
        }    
        
        private Context context;    
        private ArrayList<String> data = new ArrayList<String>();    
        
        public ListAdapter(Context context, ArrayList<String> dataItem) {    
            super(context, R.layout.child_listview, dataItem);    
            this.data = dataItem;    
            this.context = context;    
        }    
        
        @Override    
        public View getView(final int position, View convertView, ViewGroup parent) {    
            ViewHolder viewHolder;    
            if (convertView == null) {    
                LayoutInflater inflater = LayoutInflater.from(context);    
                convertView = inflater.inflate(R.layout.child_listview, null);    
                viewHolder = new ViewHolder();    
                viewHolder.text = (TextView) convertView    
                        .findViewById(R.id.childTextView);    
                viewHolder.button = (Button) convertView    
                        .findViewById(R.id.childButton);    
                convertView.setTag(viewHolder);    
            } else {    
                viewHolder = (ViewHolder) convertView.getTag();    
            }    
            final String temp = getItem(position);    
            viewHolder.text.setText(temp);    
            viewHolder.button.setOnClickListener(new OnClickListener() {    
        
                @Override    
                public void onClick(View v) {    
                    if (customListner != null) {    
                        customListner.onButtonClickListner(position,temp);    
                    }    
        
                }    
            });    
        
            return convertView;    
        }    
        
        public class ViewHolder {    
            TextView text;    
            Button button;    
        }    
    }   
    
    
    ***************
    import java.util.ArrayList;    
    import java.util.Arrays;    
    import java.util.List;    
        
    import com.example.articalonlistiner.ListAdapter.customButtonListener;    
        
    import android.os.Bundle;    
    import android.support.v7.app.ActionBarActivity;    
    import android.widget.ListView;    
    import android.widget.Toast;    
        
    public class MainActivity extends ActionBarActivity implements    
            customButtonListener {    
        
        private ListView listView;    
        ListAdapter adapter;    
        ArrayList<String> dataItems = new ArrayList<String>();    
        
        @Override    
        protected void onCreate(Bundle savedInstanceState) {    
            super.onCreate(savedInstanceState);    
            setContentView(R.layout.activity_main);    
            String[] dataArray = getResources().getStringArray(R.array.listdata);    
            List<String> dataTemp = Arrays.asList(dataArray);    
            dataItems.addAll(dataTemp);    
            listView = (ListView) findViewById(R.id.listView);    
            adapter = new ListAdapter(MainActivity.this, dataItems);    
            adapter.setCustomButtonListner(MainActivity.this);    
            listView.setAdapter(adapter);    
        
        }    
        
        @Override    
        public void onButtonClickListner(int position, String value) {    
            Toast.makeText(MainActivity.this, "Button click " + value,    
                    Toast.LENGTH_SHORT).show();   
    
    // call and redirect to another screen with "StartActivity" 
        
        }    
        
    }   
0
Binjal Vanani 4 Фев 2022 в 07:51