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

时间:2012-09-14 07:34:50

标签: android android-listview baseadapter

在我的应用程序中,我正在尝试创建一个包含ImageView,TextView和Button的ListView。我已经创建了一个单独的XML文件并在该XML中拖动了所有上述元素,在我的主java文件中我创建了一个BaseAdapter的Object,并且在getView()方法中我已经声明了这些元素但是当我运行应用程序时我不能看清单。我没有使用BaseAdapter,所以我缺少一些代码来查看List。我还想在按钮上应用一些操作,所以请让我知道该代码。

我的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;
            }
        };
    }
}

提前致谢....

2 个答案:

答案 0 :(得分:1)

好的,

    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


}

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

答案 1 :(得分:0)

你肯定错过了lv.setAdapter(bs);