当列表中没有项目时,不会显示空视图。有什么问题?
Main_Activity
package com.example.android.customadapter;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Players> objects = new ArrayList<>();
objects.add(new Players("Shikhar Dhawan",R.drawable.shikhar));
objects.add(new Players("Virat Kohli",R.drawable.kohli));
}
}
我的模特课
Players.java
package com.example.android.customadapter;
public class Players {
private String mName;
private int image;
Players(String name,int res)
{
mName = name;
image = res;
}
public String getName()
{
return mName;
}
public int getImage()
{
return image;
}
}
CustomAdapter类
package com.example.android.customadapter;
我的自定义数组列表适配器
public class CustomAdapter extends ArrayAdapter<Players> {
public CustomAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<Players> objects) {
super(context, resource, objects);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View v = convertView;
if(v==null)
{
v = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
}
Players p = getItem(position);
TextView t = (TextView)v.findViewById(R.id.miwok_text_view);
t.setText(p.getName());
ImageView i = (ImageView)v.findViewById(R.id.image);
i.setImageResource(p.getImage());
return v;
}
}
答案 0 :(得分:0)
从您的activity_main.xml获取listView。他们为该列表设置了适配器。有关详细信息:http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial
答案 1 :(得分:0)
您收到错误是因为您没有从课程中获取该值。 这是我使用的代码
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view =convertView;
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
if (convertView==null){
view=layoutInflater.inflate(R.layout.customplaylist,parent,false);
}
Playlistfileinfo playlistfileinfo =(Playlistfileinfo)getItem(position);
TextView textView =(TextView)view.findViewById(R.id.textView7);
textView.setTypeface(typeface);
textView.setText(playlistfileinfo.getName());
return view;
}
你可以看到Playlistfileinfo playlistfileinfo =(Playlistfileinfo)getItem(position);
希望这对你有帮助。
答案 2 :(得分:0)
我已经在main_activity的ArrayList末尾解决了它 在里面。 OnCreate方法中的Listview并将数组适配器设置为listView
CustomAdapter adapter = new CustomAdapter(this,words);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);