Android Studio 适配器视图中出错,OnItemClickListener

wdebmtf2  于 2023-02-24  发布在  Android
关注(0)|答案(3)|浏览(142)

为什么我说mis在这里这个数组列表代码我尝试了很多我没有找到解决方案请帮助

*public class* MainActivity extends AppCompatActivity {

**@Override**
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ArrayList<Listitem> Items = new ArrayList<Listitem>();
    Items.add(new Listitem("Noor", "Derna"));
    Items.add(new Listitem("MERO", "Derna"));
    Items.add(new Listitem("Noor", "Derna"));
    Items.add(new Listitem("Noor", "Derna"));

    **mycustomadapter myadabter = new mycustomadapter(Items);**

    **ListView Ls = (ListView) findViewById(R.id.listView); 
    Ls.setAdapter(myadabter);
Ls.setOnClickListener(new AdapterView.OnItemClickListener() {**

        **@Override**
        *public void onItemClick (AdapterView<?> parent, View view, int position, long id) {*

            TextView txtname = (TextView) view.findViewById(R.id.txt_name);
            TextView txtdisc = (TextView) view.findViewById(R.id.txt_disc);
            Toast.makeText(getApplicationContext(), txtname.getText(), Toast.LENGTH_LONG).show();
        }
    });
}



class  mycustomadapter extends BaseAdapter
{
    ArrayList<Listitem> Items=new ArrayList<Listitem>(); array list
    mycustomadapter(ArrayList<Listitem> Items){
    this.Items=Items;
    }
    @Override
    public int getCount() {
        return Items.size();
    }

    @Override
    public Object getItem(int position) {
        return Items.get(position).Name;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int i, View View, ViewGroup viewGroup) {
        LayoutInflater Linflater=getLayoutInflater(); 
        View view1 = Linflater.inflate(R.layout.raw_view,null); الview1يمثل الraw_item
        TextView txtname=(TextView)view1.findViewById(R.id.txt_name);
        TextView txtdisc=(TextView)view1.findViewById(R.id.txt_disc);
        txtname.setText(Items.get(i).Name);
        txtdisc.setText(Items.get(i).Desc);

        array list
        return view1;

    }
}

}'列表视图Ls =(列表视图)查找视图ById(R.id.列表视图);//用户名://用户名://用户名:Ls.setOnClickListener(新的适配器视图.OnItemClickListener(){

@Override
        public void onItemClick (AdapterView<?> parent, View view, int position, long id) {

            TextView txtname = (TextView) view.findViewById(R.id.txt_name);
            TextView txtdisc = (TextView) view.findViewById(R.id.txt_disc);
            Toast.makeText(getApplicationContext(), txtname.getText(), Toast.LENGTH_LONG).show();
        }
    });
}`
4ktjp1zp

4ktjp1zp1#

你为什么要在列表视图中使用On-clickListner??..不管你在做什么...

Ls.setOnClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick (AdapterView<?> parent, View view, int position, long id) {

            TextView txtname = (TextView) view.findViewById(R.id.txt_name);
            TextView txtdisc = (TextView) view.findViewById(R.id.txt_disc);
            Toast.makeText(getApplicationContext(), txtname.getText(), Toast.LENGTH_LONG).show();
        }
    });
}`

这是完全错误的。。如果你想点击列表视图项目,并从您点击的项目中获取文本,然后这样做,

Ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                Toast.makeText(MainActivity.this, Items.get(position).getName() + " " + Items.get(position).getDesc(), Toast.LENGTH_SHORT).show();

            }
        });

为了清除您的困惑这里是完整的代码,

public class MainActivity extends AppCompatActivity {

    ListView Ls;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Ls = (ListView) findViewById(R.id.listView);

        final ArrayList<Listitem> Items = new ArrayList<Listitem>();
        Items.add(new Listitem("Noor", "Derna"));
        Items.add(new Listitem("MERO", "Derna"));
        Items.add(new Listitem("Noor", "Derna"));
        Items.add(new Listitem("Noor", "Derna"));

        final mycustomadapter myadabter = new mycustomadapter(Items);
        Ls.setAdapter(myadabter);

        Ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                Toast.makeText(MainActivity.this, Items.get(position).getName() + " " + Items.get(position).getDesc(), Toast.LENGTH_SHORT).show();

            }
        });

    }

    class mycustomadapter extends BaseAdapter {
        ArrayList<Listitem> Items = new ArrayList<Listitem>();
        public TextView txtname, txtdisc;
        LayoutInflater Linflater;

        mycustomadapter(ArrayList<Listitem> Items) {
            this.Items = Items;
        }

        @Override
        public int getCount() {
            return Items.size();
        }

        @Override
        public Object getItem(int position) {
            return Items.get(position).name;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int i, View View, ViewGroup viewGroup) {
            Linflater = getLayoutInflater();
            View view1 = Linflater.inflate(R.layout.raw_view, null);
            txtname = (TextView) view1.findViewById(R.id.txt_name);
            txtdisc = (TextView) view1.findViewById(R.id.txt_disc);
            txtname.setText(Items.get(i).name);
            txtdisc.setText(Items.get(i).desc);

            return view1;

        }
    }
}
e3bfsja2

e3bfsja22#

用于使数组列表最终化,因为您已经在onCreate()中声明了它,并且在onItemClick侦听器中使用它时,您必须使它最终化。
如果你不想让它成为final,你可以声明array-list为全局变量,并在onCreate()中初始化它,那么你就不需要让它成为final。
是的,你可以从适配器初始化中删除final,它不会影响代码。

ma8fv8wu

ma8fv8wu3#

我的密码错误请告诉我。
setOnClickListener(新的适配器视图.OnItemClickListener()){ @覆盖公共void onItemClick(适配器视图,视图视图,整数位置,长l){字符串项=适配器视图.getItemAtPosition(位置).toString();吐司.makeText(主活动2.this,“项目”+项目,Toast.LENGTH_SHORT).show();} });

相关问题