视图在ListView / Adapter中不可见,但是存在调试对象

时间:2015-09-22 12:39:36

标签: android listview adapter

我正在努力解决这个问题并没有找到任何解决方案浏览网页和StackOverflow中的许多其他类似帖子

在应用程序中搜索后没有错误编译没有在ListView中可见的视图(浏览ListView和CustomAdapter对象,结果项视图存在,但未显示在应用程序的列表视图中)

这里是代码:

清单

// C
#include <stdint.h>

// C++
#include <cstdint>

#if INTPTR_MAX == INT64_MAX
// 64-bit
#elif INTPTR_MAX == INT32_MAX
// 32-bit
#else
#error Unknown pointer size or missing size macros!
#endif

RES / XML / searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lightelements.carddroid">

<permission android:name="com.lightelements.carddroid.cards.provider.READWRITE"/>

<uses-permission android:name="com.lightelements.carddroid.cards.provider.READWRITE"/>
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchableActivity" />
    </activity>

    <activity android:name=".SearchActivity"
              android:parentActivityName=".MainActivity"
              android:label="@string/search_cards_title">
    </activity>

    <activity android:name=".SearchableActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
    </activity>

    <provider
        android:authorities="com.lightelements.carddroid.cards.provider"
        android:name="com.lightelements.carddroid.CardProvider"
        android:exported="true"
          android:readPermission="com.lightelements.carddroid.cards.provider.READWRITE"
        android:writePermission="com.lightelements.carddroid.cards.provider.READWRITE"
        />
</application>

</manifest>

search_layout

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/ 
    android"android:label="@string/app_name"
    android:hint="@string/search_hint" >
</searchable>

simple_card.xml

<?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:layout_marginTop="10dp"
          android:orientation="vertical"
          android:padding="16dp">

<TextView
    android:id="@+id/searchLayoutTestText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<ListView android:id="@+id/searchResultsList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</ListView>

</LinearLayout>

SearchableActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<TextView
    android:id="@+id/card_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/card_name"
    />

</LinearLayout>

MainActivity

package com.lightelements.carddroid;

import android.app.SearchManager;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;
/**
 * Created by User on 20/09/2015.
 */
public class SearchableActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Card>> {

private static final String LOG_TAG = SearchableActivity.class.getSimpleName();
private CardsCustomAdapter mCardsCustomAdapter;
private static int LOADER_ID = 2;
private ContentResolver mContentResolver;
private List<Card> cardsRetrieved;
private ListView listView;
private String matchText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d(LOG_TAG, "Entered Searchable Activity");

    setContentView(R.layout.search_layout);
    TextView testText = (TextView) findViewById(R.id.searchLayoutTestText);
    testText.setText("OK SEARCHLIST LAYOUT");

    Intent intent = getIntent();
    // ACTION SEARCH PER CONFERMA SELEZIONE //
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        Log.v(LOG_TAG,"query: " + query);
        mContentResolver = getContentResolver();
        mCardsCustomAdapter = new CardsCustomAdapter(SearchableActivity.this, getSupportFragmentManager());
        listView = (ListView) findViewById(R.id.searchResultsList);

        listView.setAdapter(mCardsCustomAdapter);
        matchText = query.toString();
        getSupportLoaderManager().initLoader(LOADER_ID++, null, SearchableActivity.this);
    }
}

@Override
public Loader<List<Card>> onCreateLoader(int id, Bundle args) {
    return new CardsSearchListLoader(SearchableActivity.this, CardContract.URI_TABLE, this.mContentResolver, matchText);
}

@Override
public void onLoadFinished(Loader<List<Card>> loader, List<Card> cards) {
    mCardsCustomAdapter.setData(cards);
    this.cardsRetrieved = cards;
}

@Override
public void onLoaderReset(Loader<List<Card>> loader) {
    mCardsCustomAdapter.setData(null);
}
}

CardSearchListLoader

package com.lightelements.carddroid;
import android.app.Activity;
import android.app.SearchManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks,
    BestPrice.OnFragmentInteractionListener {

private static final String LOG_TAG = MainActivity.class.getSimpleName();

private NavigationDrawerFragment mNavigationDrawerFragment;
TextView mData = null;
Fragment activeFragment = null;
LinearLayout activeLayout = null;

private CharSequence mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    // Connect to Database
    String[] projection = {BaseColumns._ID,
            CardContract.CardColumns.CARD_NAME,
            CardContract.CardColumns.CARD_SET,
            CardContract.CardColumns.SET_CODE,
            CardContract.CardColumns.MULTIVERSE_ID,
            CardContract.CardColumns.MKM_ID
    };
    ContentResolver mContentResolver = getContentResolver();
    Cursor mTestCursor = mContentResolver.query(CardContract.URI_TABLE, projection, null, null, null);
    Log.d(LOG_TAG,"Completato on Create.");

    if (mTestCursor.moveToFirst()) {
        String card_name = mTestCursor.getString(mTestCursor.getColumnIndex(CardContract.CardColumns.CARD_NAME));
        TextView testView = (TextView) findViewById(R.id.testTextView);
        testView.setText(card_name);
    }

}

@Override
public void onNavigationDrawerItemSelected(int position) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
            .commit();
}

public void onSectionAttached(int number) {
    Fragment fragment = null;
    switch (number) {
        case 1:
            mTitle = getString(R.string.title_section1);
            fragment = BestPrice.newInstance("param1", "param2");
            break;
        case 2:
            mTitle = "TEST SEZIONE 2";
            fragment = PlaceholderFragment.newInstance(number + 1);
            break;
        case 3:
            mTitle = getString(R.string.title_section3);
            fragment = PlaceholderFragment.newInstance(number + 1);
            break;
    }
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, fragment)
            .commit();
}

public void onFragmentInteraction(Uri uri){
}

    public void dataSearch(View view){
    Activity fragment = (Activity) view.getContext();
    mData   = (TextView) fragment.findViewById(R.id.best_price_data);
    activeLayout = (LinearLayout) findViewById(R.id.fragment_best_price_layout);
}

public void dataOut(String outData){
    mData.setText(outData);
}

public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        getMenuInflater().inflate(R.menu.main, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchItem = menu.findItem(R.id.searchCards);

        SearchView cardSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        cardSearchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchableActivity.class)));
        Log.d(LOG_TAG,"Searchable created");

        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}
}

CardsCustomAdapter

package com.lightelements.carddroid;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.support.v4.content.AsyncTaskLoader;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by User on 06/09/2015.
 */
public class CardsSearchListLoader extends AsyncTaskLoader<List<Card>> {
private static final String LOG_TAG = CardsSearchListLoader.class.getSimpleName();
private List<Card> mCards;
private ContentResolver mContentResolver;
private Cursor mCursor;
private String mFilterText;

public CardsSearchListLoader(Context context, Uri uri, ContentResolver contentResolver, String filterText){
    super(context);
    mContentResolver = contentResolver;
    mFilterText = filterText;
}

@Override
public List<Card> loadInBackground() {
    String[] projection = {BaseColumns._ID,
            CardContract.CardColumns.CARD_NAME,
            CardContract.CardColumns.CARD_SET,
            CardContract.CardColumns.SET_CODE,
            CardContract.CardColumns.MULTIVERSE_ID,
            CardContract.CardColumns.MKM_ID
    };
    List<Card> entries = new ArrayList<Card>();

    String selection = CardContract.CardColumns.CARD_NAME + " LIKE '%" + mFilterText + "%'";

    mCursor = mContentResolver.query(CardContract.URI_TABLE, projection, selection, null, null);
    if (mCursor!=null) {
        if (mCursor.moveToFirst()) {
            do {
                int _id = mCursor.getInt(mCursor.getColumnIndex(BaseColumns._ID));
                String card_name = mCursor.getString(mCursor.getColumnIndex(CardContract.CardColumns.CARD_NAME));
                int card_set = mCursor.getInt(mCursor.getColumnIndex(CardContract.CardColumns.CARD_SET));
                String set_code = mCursor.getString(mCursor.getColumnIndex(CardContract.CardColumns.SET_CODE));
                int multiverse_id = mCursor.getInt(mCursor.getColumnIndex(CardContract.CardColumns.MULTIVERSE_ID));
                int mkm_id = mCursor.getInt(mCursor.getColumnIndex(CardContract.CardColumns.MKM_ID));
                Card card = new Card(_id, card_name, card_set, set_code, multiverse_id, mkm_id);
                entries.add(card);
            } while (mCursor.moveToNext());
        }
    }
    return entries;
}

@Override
public void deliverResult(List<Card> cards) {
    if (isReset()){
        if (cards!=null){
            mCursor.close();
        }
    }
    Log.d(LOG_TAG,"deliverResult");
    List<Card> oldCardList = mCards;
    if (mCards == null || mCards.size() == 0) {
        Log.d(LOG_TAG, "++++++++++++++ No Ddata returned");
    }
    mCards = cards;
    if (isStarted()) {
        super.deliverResult(cards);
    }
    if (oldCardList != null || oldCardList != cards) {
        mCursor.close();
    }

}

@Override
protected void onStartLoading() {
    if (mCards != null) {
        deliverResult(mCards);
    }

    if (takeContentChanged() || mCards == null) {
        forceLoad();
    }
}

@Override
protected void onStopLoading() {
    cancelLoad();
}

@Override
protected void onReset() {
    onStopLoading();
    if (mCursor != null) {
        mCursor.close();
    }
    mCards = null;
}

@Override
public void onCanceled(List<Card> cards) {
    super.onCanceled(cards);
    if (mCursor != null) {
        mCursor.close();
    }
}

@Override
public void forceLoad() {
    super.forceLoad();
}
}

1 个答案:

答案 0 :(得分:0)

您的搜索布局高度应该反转:

<?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:layout_marginTop="10dp"
          android:orientation="vertical"
          android:padding="16dp">

<TextView
    android:id="@+id/searchLayoutTestText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<ListView android:id="@+id/searchResultsList"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>

</LinearLayout>