在Android上使用带有ListAdapter的AdMob广告

时间:2011-02-20 15:50:27

标签: android admob

我目前正在开发一个应用程序,其中主屏幕没有像setContentView(R.layout.myScreen)中那样的实际视图;用来。相反,它使用XML文件,该文件仅详细说明分配给列表适配器的文本视图,并填充数据库中的数据。

以下是使用的XML文件

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:myapp="http://schemas.android.com/apk/res/com.BoardiesITSolutions.PasswordManager"
            android:id="@+id/showLogin"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="10dp"
            android:textSize="16dp">
    </TextView>

以下是将数据添加到textview列表适配器的代码

公共类ShowLogins扩展了ListActivity {

String LOGINS[] = null;
String company = null;
String longClickCompany;

String companyName = "";
String companyURL = "";
String companyUsername = "";
String companyPassword = "";
Boolean passwordEnabled;
public void onCreate(Bundle savedInstanceState)
{
    SharedPreferences settings = getSharedPreferences("prefs", 0);

    boolean enablePassword = settings.getBoolean("enablePassword", false);
    boolean loggedIn = settings.getBoolean("loggedIn", false);
    super.onCreate(savedInstanceState);
    if (loggedIn)
    {
        passwordEnabled = settings.getBoolean("enablePassword", false);
        setContentView(R.layout.show_login);

    //registerForContextMenu(getListView());


    addToArray();

    setListAdapter(new ArrayAdapter<String>(this, R.id.showLoginBox, LOGINS));

    //ListView lv = getListView();
    ListView lv = (ListView)findViewById(R.id.show_list_view);
    //View myView = findViewById(R.layout.advert);
    //lv.addView(myView);

    lv.setTextFilterEnabled(true);

    lv.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {
            longClickCompany = ((TextView) view).getText().toString();
            return false;
        }
    });

    lv.setOnItemClickListener(new OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            company = ((TextView) view).getText().toString();
            StringTokenizer st = new StringTokenizer(company, "\n");
            String companyName = st.nextToken();
            String username = st.nextToken();
            prepareLoadingWebsite(companyName, username);
        }
    });
    }
    else
    {
        Intent intent = new Intent(ShowLogins.this, SplashScreen.class);
        startActivity(intent);
    }
}

private void addToArray()
{
    int totalRows = 0;
    int added = 0;
    int arrayField = 0;
    SQLiteDatabase myDB = null;
    Cursor c = null;
    Cursor count = null;
    try
    {
        myDB = this.openOrCreateDatabase("PasswordManager", MODE_PRIVATE, null);
        count = myDB.rawQuery("SELECT COUNT(*) FROM PASSWORD", null);
        if (count.moveToNext())
        {
            totalRows = count.getInt(0);
        }
        LOGINS = new String[totalRows];

        c = myDB.rawQuery("SELECT * FROM password ORDER BY pas_company ASC", null);

        while(arrayField <=2 && c.moveToPosition(added))
        {
            LOGINS[added] = c.getString(1) + "\n" + c.getString(3);
            added++;
        }

        if (totalRows == 0)
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("No login records have been found.\n\n Press menu > New login to add a record")
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
            AlertDialog alert = builder.create();
            alert.show();
        }
        else
        {
            setListAdapter(new ArrayAdapter<String>(this, R.layout.show_login, LOGINS));

            ListView lv = getListView();
            lv.setTextFilterEnabled(true);
        }

    }
    catch (SQLiteException ex)
    {
        Log.d("Database Error", ex.toString());
    }
    finally
    {
        myDB.close();
        c.close();
        count.close();
    }
}

3 个答案:

答案 0 :(得分:4)

当您使用ListActivity时,如果您自己未指定内容布局,则会获得系统指定的内容布局。为活动创建自己的布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ListView android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />
    <!-- Ad view goes here with layout_width="match_parent" and layout_height="wrap_content" -->
</LinearLayout>

像往常一样使用setContentView进行设置。如果您使用自定义布局,ListActivity需要一个ID为@android:id/list的ListView,它可以找到它。列表的height = 0,weight = 1配置将在广告高度测量后为布局中的所有剩余空间提供。

答案 1 :(得分:0)

根据您的说法,您不希望admob广告与ListAdapter有任何关系。

您想将admob广告添加到LinearLayout的底部,但您需要告诉我们应用程序部队关闭的原因。

告诉我们,我们也许可以从那里帮助你。

答案 2 :(得分:0)

如果您使用getListView(),那么您的xml R.layout.myScreen不仅仅是TextView。它应包含<ListView>

您无需从适配器添加Admob的View。您可以将其放在ListView下方的视图中。类似的东西:

<RelativeLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <AdmobSomething android:id="@+id/admob"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"/>

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/admob" />

</RelativeLayout>