您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
可以通过继承BaseAdapter类来自定义ListView的Adapter。以下是一个简单的例子:
public class CustomAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> mDataList;
public CustomAdapter(Context context, ArrayList<String> dataList) {
this.mContext = context;
this.mDataList = dataList;
}
@Override
public int getCount() {
return mDataList.size();
}
@Override
public Object getItem(int position) {
return mDataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.list_item, null);
}
TextView textView = view.findViewById(R.id.text_view);
textView.setText(mDataList.get(position));
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="8dp"
android:gravity="center_vertical"/>
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private ArrayList<String> mDataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = findViewById(R.id.list_view);
mDataList = new ArrayList<>();
mDataList.add("Item 1");
mDataList.add("Item 2");
mDataList.add("Item 3");
CustomAdapter adapter = new CustomAdapter(this, mDataList);
mListView.setAdapter(adapter);
}
}
通过以上步骤,我们就完成了一个简单的自定义Adapter来显示ListView中的数据。可以根据实际需求进行更复杂的定制化操作。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。