为什么我在这里说错误这个数组列表代码 我尝试了很多我没有找到解决方案 请帮忙
*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;
}
}
}`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();
}
});
}`
答案 0 :(得分:1)
你为什么要在Listview中使用on-clickList???无论你在做什么,这都是......
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();
}
});
}`
这是完全错误的..如果您想点击listview项目并从您点击的项目中获取文本,那么就这样做,
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;
}
}
}
答案 1 :(得分:0)
为了制作array-list final,因为你已经在onCreate()中声明了它,并且在onItemClick监听器中使用它时你必须使它成为最终的。
如果你不想让它成为最终版,你可以将array-list声明为全局,并在onCreate()中初始化它,然后你不需要使它成为最终的。
是的,您可以从适配器初始化中删除final。它不会影响代码。