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 に答える