In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "what is the concept of Android adapter". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is an adapter
In common software, you can often see a list-like flow of information, such as:
If you write the information to death in xml, it is obviously inappropriate and cannot achieve the effect of pull-up and refresh.
At this point, an adapter is needed.
There are many adapters in Android. First, look at the inheritance structure of these adapters.
Among these adapters, BaseAdapter is the most widely used and the most familiar.
II. Basic concepts and inheritance relations of Adapter
III. Custom adapter instance
1. File structure
2.xml content
Main.xml:
Listview_item.xml:
3.java content
MainActivity
Package cn.edu.cdut.testadapter;import androidx.appcompat.app.AppCompatActivity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.Toast;import java.util.LinkedList;import java.util.List;public class MainActivity extends AppCompatActivity {private List mData = null; private Context mContext; private NewsAdapter mAdapter = null; private ListView listView @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); mContext = this; listView = (ListView) findViewById (R.id.listview); mData = new LinkedList (); for (int I = 0; I < 10) MData.add +) {mData.add ("I am a news headline -" + I, "I am a news content -" + I, R.mipmap.news));} mAdapter = new NewsAdapter (mData, mContext); listView.setAdapter (mAdapter) ListView.setOnItemClickListener (new AdapterView.OnItemClickListener () {@ Override public void onItemClick (AdapterView parent, View view, int position, long id) {Toast.makeText (mContext, "clicked" + position + "data", Toast.LENGTH_SHORT). Show ();}});}}
News.java
Package cn.edu.cdut.testadapter;public class News {private String title; private String content; private int aIcon; public News () {} public News (String title, String content, int aIcon) {this.title = title; this.content = content; this.aIcon = aIcon;} public String getTitle () {return title;} public void setTitle (String title) {this.title = title } public String getContent () {return content;} public void setContent (String content) {this.content = content;} public int getaIcon () {return aIcon;} public void setaIcon (int aIcon) {this.aIcon = aIcon;}}
NewsAdapter
Package cn.edu.cdut.testadapter;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import java.util.List;public class NewsAdapter extends BaseAdapter {private List mData; private Context mContext; public NewsAdapter (List mData, Context mContext) {this.mData = mData; this.mContext = mContext } @ Override public int getCount () {return mData.size ();} @ Override public Object getItem (int position) {return mData.get (position);} @ Override public long getItemId (int position) {return position } @ Override public View getView (int position, View convertView, ViewGroup parent) {convertView = LayoutInflater.from (mContext) .propagate (R.layout.listview_item, parent, false); ImageView img_icon = (ImageView) convertView.findViewById (R.id.img_icon) TextView title = (TextView) convertView.findViewById (R.id.tv_title); TextView content = (TextView) convertView.findViewById (R.id.tv_content); img_icon.setBackgroundResource (mData.get (position). GetaIcon ()); title.setText (mData.get (position). GetTitle ()); content.setText (mData.get (position). GetContent ()); return convertView This is the end of the content of "what is the concept of Android Adapter". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.