如何在android的ListView中添加ImageView、TextView和按钮
在我的应用程序中,我试图创建一个包含ImageView、TextView和按钮的ListView。我创建了一个单独的XML文件,并在该XML中拖动了上述所有元素,在我的主java文件中创建了BaseAdapter对象,在getView()方法中声明了这些元素,但在运行应用程序时,我看不到列表。我没有使用BaseAdapter,因此我缺少一些查看列表的代码。我也想在按钮上应用一些操作,所以请让我知道代码也 my main.xml文件的代码:如何在android的ListView中添加ImageView、TextView和按钮,android,android-listview,baseadapter,Android,Android Listview,Baseadapter,在我的应用程序中,我试图创建一个包含ImageView、TextView和按钮的ListView。我创建了一个单独的XML文件,并在该XML中拖动了上述所有元素,在我的主java文件中创建了BaseAdapter对象,在getView()方法中声明了这些元素,但在运行应用程序时,我看不到列表。我没有使用BaseAdapter,因此我缺少一些查看列表的代码。我也想在按钮上应用一些操作,所以请让我知道代码也 my main.xml文件的代码: <LinearLayout xmlns:andro
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
提前感谢….您肯定缺少
lv.setAdapter(bs)代码>正常
public class App_Adapter extends BaseAdapter implements OnClickListener{
private Activity mActivity;
private List<App_List> mList;
private static LayoutInflater inflater=null;
private PackageManager pm;
private String appclass;
private ApplicationTask mApplicationTask;
private String link=null;
public App_Adapter (FavouriteApp favouriteApp,List<App_List> mAppList, String appclass) {
// TODO Auto-generated constructor stub
this.mActivity= favouriteApp;
this.mList= mAppList;
this.appclass = appclass;
inflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pm=mActivity.getPackageManager();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View mView=convertView;
if (convertView == null)
mView = inflater.inflate(R.layout.app_adapter, parent,false);
App_List mAppList= mList.get(position);
**here i am setting two textview and one button**
((TextView) mView.findViewById(R.id.textView_appName)).setText(mAppList.getApp_Name());
((TextView) mView.findViewById(R.id.textView_appDescription)).setText(mAppList.getApp_Description());
boolean status = isAppInstalled(mAppList.getApp_Pkg());
Button btn = (Button) mView.findViewById(R.id.button_appStatus);
**// register the button for clicklistener**
btn.setOnClickListener(this);
return mView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
公共类App\u适配器扩展BaseAdapter实现OnClickListener{
私人活动能力;
私人名单;
专用静态充气机=空;
私人包装经理;
私有字符串类;
私有应用程序任务MapApplicationTask;
私有字符串链接=null;
公共应用程序适配器(FavoriteApp FavoriteApp、列表映射列表、字符串应用程序类){
//TODO自动生成的构造函数存根
this.mActivity=favoriteApp;
this.mList=mAppList;
this.appclass=appclass;
充气器=(LayoutInflater)mActivity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
pm=mActivity.getPackageManager();
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回mList.size();
}
@凌驾
公共对象getItem(int arg0){
//TODO自动生成的方法存根
返回arg0;
}
@凌驾
公共长getItemId(int arg0){
//TODO自动生成的方法存根
返回arg0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视图mView=转换视图;
if(convertView==null)
mView=充气机。充气(R.layout.app_适配器,父级,false);
App_List mAppList=mList.get(位置);
**这里我设置了两个文本视图和一个按钮**
((TextView)mView.findviewbyd(R.id.TextView_appName)).setText(mAppList.getApp_Name());
((TextView)mView.findviewbyd(R.id.TextView_appDescription)).setText(mAppList.getApp_Description());
boolean status=isAppInstalled(mAppList.getApp_Pkg());
按钮btn=(按钮)mView.findviewbyd(R.id.Button\u appStatus);
**//注册clicklistener的按钮**
btn.setOnClickListener(此);
返回mView;
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
}
并从您的活动中调用此适配器类。编写上述代码会产生一个java.lang.NullPointerException..我正在编写lv.setAdapter(bs);其中baseAdapter结束。即在}之后;低压设置适配器(bs);请纠正我。。谢谢..请看;在类似的情况下帮了我很多。你能再告诉我一件事吗?如何在代码中应用onTextChangedListener()?as onTextChangedListener()需要ArrayAdapter,但上面的代码没有任何ArrayAdapter。。非常感谢你的帮助。。谢谢
public class CustomListActivity extends Activity {
/** Called when the activity is first created. */
ListView lv;
LayoutInflater inflator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView)findViewById(R.id.listView1);
BaseAdapter bs = new BaseAdapter() {
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vw = inflator.inflate(R.layout.list_items, null);
ImageView img = (ImageView)findViewById(R.id.imageView1);
TextView tv = (TextView)findViewById(R.id.textView1);
Button btn = (Button)findViewById(R.id.button1);
return vw;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
};
}
}
public class App_Adapter extends BaseAdapter implements OnClickListener{
private Activity mActivity;
private List<App_List> mList;
private static LayoutInflater inflater=null;
private PackageManager pm;
private String appclass;
private ApplicationTask mApplicationTask;
private String link=null;
public App_Adapter (FavouriteApp favouriteApp,List<App_List> mAppList, String appclass) {
// TODO Auto-generated constructor stub
this.mActivity= favouriteApp;
this.mList= mAppList;
this.appclass = appclass;
inflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pm=mActivity.getPackageManager();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View mView=convertView;
if (convertView == null)
mView = inflater.inflate(R.layout.app_adapter, parent,false);
App_List mAppList= mList.get(position);
**here i am setting two textview and one button**
((TextView) mView.findViewById(R.id.textView_appName)).setText(mAppList.getApp_Name());
((TextView) mView.findViewById(R.id.textView_appDescription)).setText(mAppList.getApp_Description());
boolean status = isAppInstalled(mAppList.getApp_Pkg());
Button btn = (Button) mView.findViewById(R.id.button_appStatus);
**// register the button for clicklistener**
btn.setOnClickListener(this);
return mView;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}