繁体   English   中英

如何在Android的ListView中添加ImageView,TextView和Button

[英]How to add ImageView, TextView and Button in ListView in android

在我的应用程序中,我试图创建一个包含ImageView,TextView和Button的ListView。 我创建了一个单独的XML文件,并将上述所有元素拖放到该XML中,并在主Java文件中创建了BaseAdapter对象,并在getView()方法中声明了这些元素,但是当我运行应用程序时,我无法查看清单。 我没有使用BaseAdapter,所以为了查看列表我缺少一些代码。 我也想对按钮进行一些操作,因此也请让我知道该代码。

我的main.xml文件的代码:

<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>

我的list_item.xml的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="15dp"
        android:text="Medium Text"
        android:textSize="15dp" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="25dp"
        android:text="Button" />

</LinearLayout>

我的main.java文件的代码:

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


}

并从您的活动中调用此适配器类。

您肯定缺少lv.setAdapter(bs);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM