0

在我的应用程序中,我试图创建一个包含 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;
            }
        };
    }
}

提前致谢....

4

2 回答 2

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


}

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

于 2012-09-14T07:40:51.080 回答
0

你肯定失踪了lv.setAdapter(bs);

于 2012-09-14T07:40:03.370 回答