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

Android 视图在ListView/Adapter中不可见,但存在调试对象,android,listview,adapter,Android,Listview,Adapter,我正在努力解决这个问题,在StackOverflow中浏览网页和许多其他类似帖子时没有找到任何解决方案 编译时无错误,在应用程序中执行搜索后,ListView中没有可见的视图(浏览ListView和CustomAdapter对象,结果项视图存在,但不显示在应用程序的ListView中) 代码如下: 显示 CardSearchListLoader package com.lightelements.cardroid; 导入android.content.ContentResolver; 导入and

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

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

代码如下:

显示 CardSearchListLoader
package com.lightelements.cardroid;
导入android.content.ContentResolver;
导入android.content.Context;
导入android.database.Cursor;
导入android.net.Uri;
导入android.provider.BaseColumns;
导入android.support.v4.content.AsyncTaskLoader;
导入android.util.Log;
导入java.util.ArrayList;
导入java.util.List;
/**
*用户于2015年9月6日创建。
*/
公共类CardsSearchListLoader扩展了AsyncTaskLoader{
私有静态最终字符串LOG_TAG=CardsSearchListLoader.class.getSimpleName();
私有列表mCards;
私有ContentResolver-mContentResolver;
私有游标mCursor;
私有字符串mFilterText;
public CardsSearchListLoader(上下文上下文、Uri、ContentResolver ContentResolver、String filterText){
超级(上下文);
mContentResolver=contentResolver;
mFilterText=过滤器文本;
}
@凌驾
公共列表加载背景(){
String[]projection={BaseColumns.\u ID,
CardContract.CardColumns.CARD\u名称,
CardContract.CardColumns.CARD\u集合,
CardContract.CardColumns.SET_代码,
CardContract.CardColumns.MULTIVERSE_ID,
CardContract.CardColumns.MKM_ID
};
列表项=新的ArrayList();
字符串选择=CardContract.CardColumns.CARD_NAME+“类似“%”“+mFilterText+“%””;
mCursor=mContentResolver.query(CardContract.URI_TABLE,projection,selection,null,null);
if(mCursor!=null){
if(mCursor.moveToFirst()){
做{
int_id=mCursor.getInt(mCursor.getColumnIndex(BaseColumns.u 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));
卡片=新卡片(卡片id、卡片名称、卡片集合、集合代码、多重宇宙id、mkm id);
条目。添加(卡片);
}while(mCursor.moveToNext());
}
}
返回条目;
}
@凌驾
公共无效交付结果(列表卡){
if(isReset()){
如果(卡片!=null){
mCursor.close();
}
}
Log.d(Log_标签,“deliverResult”);
列表oldCardList=mCards;
if(mCards==null | | mCards.size()==0){
Log.d(Log_标记,“+Ddata未返回”);
}
mCards=卡片;
如果(isStarted()){
超级传送结果(卡片);
}
if(oldCardList!=null | | oldCardList!=cards){
mCursor.close();
}
}
@凌驾
开始加载时受保护的void(){
如果(mCards!=null){
交付结果(mCards);
}
if(takeContentChanged()| | mCards==null){
力载荷();
}
}
@凌驾
受保护的无效onStopLoading(){
取消加载();
}
@凌驾
受保护的void onReset(){
onStopLoading();
if(mCursor!=null){
mCursor.close();
}
mCards=null;
}
@凌驾
已取消的公共作废(列表卡){
超级取消(卡片);
if(mCursor!=null){
mCursor.close();
}
}
@凌驾
公共无效强制加载(){
super.forceLoad();
}
}
卡德斯库姆适配器
package com.lightelements.cardroid;
导入android.content.Context;
导入android.support.v4.app.FragmentManager;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.TextView;
导入java.util.List;
/**
*用户于2015年9月11日创建。
*/
公共类CardsCustomAdapter扩展了ArrayAdapter{
私人停车场平面布置;
私有静态碎片管理器sframpentmanager;
公共CardsCustomAdapter(上下文上下文、碎片管理器碎片管理器){
super(context,android.R.layout.simple\u list\u item\u 2);
mLayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
SFFragmentManager=碎片管理器;
}
公共CardsCustomAdapter(上下文){
super(context,android.R.layout.simple\u list\u item\u 2);
mLayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
最终视图;
if(convertView==null){
视图=MLAyoutFlater.充气(R.layout.custom_卡,父级,假);
}否则{
视图=转换视图;
}
最终卡片=获取项目(位置);
final int_id=card.getId();
最后一个字符串名称=card.getCard_name();
((TextView)view.findviewbyd(R.id.card_name)).setText(name);
返回视图;
}
公共无效设置数据(列表卡){
清除();
如果(卡片!=null){
用于(卡片:卡片){
添加(卡);
}
}
}
}

您的搜索布局高度应倒置:

<?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>


您应该发布一条……请尝试手动为列表视图指定一些高度,如200dp,以检查列表视图是否存在高度问题谢谢您的建议;)这是我没有检查的少数几件事之一:(仍然有一些小问题,但现在有些事情出现了,非常感谢
<?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>
<?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>
<?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>
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);
}
}
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));
    }
}
}
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();
}
}
package com.lightelements.carddroid;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by User on 11/09/2015.
 */

public class CardsCustomAdapter extends ArrayAdapter<Card> {
private LayoutInflater mLayoutInflater;
private static FragmentManager sFragmentManager;

    public CardsCustomAdapter(Context context, FragmentManager fragmentManager){
    super(context, android.R.layout.simple_list_item_2);
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    sFragmentManager = fragmentManager;
}

public CardsCustomAdapter(Context context){
    super(context, android.R.layout.simple_list_item_2);
    mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View view;
    if (convertView == null) {
        view = mLayoutInflater.inflate(R.layout.custom_card, parent, false);
    } else {
        view = convertView;
    }

    final Card card = getItem(position);
    final int _id = card.getId();
    final String name = card.getCard_name();

    ((TextView) view.findViewById(R.id.card_name)).setText(name);

    return view;
}

public void setData(List<Card> cards){
    clear();
    if (cards != null) {
        for (Card card : cards) {
            add(card);
        }
    }
}
}
<?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>