android studio Listview的使用

anticity_main.xml





.java
package com.example.administrator.listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private String[] names = {“京东商城”,“QQ”};
private int[] icons ={R.drawable.jd, R.drawable.qq}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.lv);
MyBaseAdapter mAdapter = new MyBaseAdapter();
mListView.setAdapter(mAdapter);
}
class MyBaseAdapter extends BaseAdapter{
@Override
public int getCount(){
return names.length;
}
@Override
public Object getItem(int position){
return names[position];
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView(int positoin, View convertView, ViewGroup parent){
View view = View.inflate(MainActivity.this,R.layout.list_item,null);
TextView mTextView = (TextView) view.findViewById(R.id.item_tv);
mTextView.setText(names[positoin]);
ImageView imageView = (ImageView) view.findViewById(R.id.item_image);
imageView.setBackgroundResource(icons[positoin]);
return view;
}
}
}
list_item.xml




你可能感兴趣的:(Android开发)