Android:首次运行时只显示一些行

时间:2015-09-08 19:47:52

标签: android sqlite android-listview

Listview显示的是一些行而不是所有项目(有时候,它什么也没显示),我无法理解为什么会这样。它发生在第一次运行。之后,它运行良好,屏幕上填充了数据库中的所有项目。我将垂直方向放在我的线性布局xml文件和wrap_content高度中。但问题没有解决。

这是我的主要活动:

public class MainActivity extends Activity {

public static final String PREFS_NAME = "MyPrefsFile1";

private TextView mTextView;
private ListView mListView;


ArrayList<WordDefinition> allWordDefinitions=new ArrayList<WordDefinition>();

DictionaryDatabase DictionaryDatabase;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.main);
    mTextView = (TextView) findViewById(R.id.text);
    mListView = (ListView) findViewById(R.id.list);
    DictionaryDatabase=new DictionaryDatabase(this); 
    allWordDefinitions=DictionaryDatabase.getAllWords(); 

    Collections.sort(allWordDefinitions, new CustomComparator());    
        mListView.setAdapter(new BaseAdapter() {

        @Override
        public View getView(int position, View view, ViewGroup arg2) {
            if (view==null) {
                view=getLayoutInflater().inflate(R.layout.list_item, arg2, false);
            }
            TextView textView=(TextView) view.findViewById(R.id.listItemTextView);
            textView.setText(allWordDefinitions.get(position).word);

            return view;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return allWordDefinitions.size();
        }
    });

    mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long arg3) {
            Intent intent =new Intent(MainActivity.this, WordDefinitionDetailActivity.class);
            intent.putExtra("word", allWordDefinitions.get(position).word);
            intent.putExtra("definition", allWordDefinitions.get(position).definition);


            startActivity(intent);
        }
    });


    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        Intent wordIntent = new Intent(this, WordActivity.class);
        this.finish();
        wordIntent.setData(intent.getData());
        startActivity(wordIntent);
    } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        showResults(query);

    }
}

private void showResults(String query) {

    Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null,
                            new String[] {query}, null);

    if (cursor == null) {
        mTextView.setText(getString(R.string.no_results, new Object[] {query}));
    } else {
        int count = cursor.getCount();
        String countString = getResources().getQuantityString(R.plurals.search_results,
                                count, new Object[] {count, query});
        mTextView.setText(countString);

        String[] from = new String[] { DictionaryDatabase.KEY_WORD,
                                       DictionaryDatabase.KEY_DEFINITION };

        int[] to = new int[] { R.id.word,
                               R.id.definition };

        SimpleCursorAdapter words = new SimpleCursorAdapter(this,
                                      R.layout.result, cursor, from, to);
        mListView.setAdapter(words);

        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class);
                Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
                                                String.valueOf(id));
                wordIntent.setData(data);
                startActivity(wordIntent);

            }
        });
    }
}


public class CustomComparator implements Comparator<WordDefinition> {          

   @Override
       public int compare(WordDefinition p1, WordDefinition p2) {
          return p1.word.compareTo(p2.word);
       }
    }

}

WorDefinition(Array List):

public class WordDefinition {
String word,definition;

public WordDefinition(String word,ArrayList<String> alldefinition) {
    this.word=word;

    StringBuilder stringBuilder=new StringBuilder();
    for (String string : alldefinition) {
        stringBuilder.append(string);
    }       
    this.word=stringBuilder.toString();

}

public WordDefinition(String word,String alldefinition) {
    this.word=word;     
    this.definition=alldefinition;

   } 


 }

DictionaryDatabase(摘录)

public ArrayList<WordDefinition> getAllWords() {
ArrayList<WordDefinition> arrayList=new ArrayList<WordDefinition>();
SQLiteDatabase database=mDatabaseOpenHelper.getReadableDatabase();

String selectAllQueryString="SELECT * FROM "+FTS_VIRTUAL_TABLE;
Cursor cursor=database.rawQuery(selectAllQueryString, null);

if (cursor.moveToFirst()) {
    do {            
        WordDefinition wordDefinition=new WordDefinition(cursor.getString(cursor.getColumnIndex(KEY_WORD)), cursor.getString(cursor.getColumnIndex(KEY_DEFINITION)));
        arrayList.add(wordDefinition);              
    } while (cursor.moveToNext());          
}   
return arrayList;
}

主XML

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/fundoeua"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >

    <LinearLayout
    android:id="@+id/Line1"        
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text"
        android:textColor="#FFFFFF"
        android:gravity="right"
        android:textAlignment="gravity"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingRight="15dp"
        android:textStyle="bold"
        android:textSize="20sp"
        android:text="@string/chamada"
        android:background="@color/vermelho"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />



<ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:divider="#ff0000"
            android:dividerHeight="4px" 
         />

</LinearLayout>





</RelativeLayout>

list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/seletor2"
    android:orientation="vertical" >

    <TextView
    android:id="@+id/listItemTextView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dp"        
    android:textColor="@color/preto"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

   </LinearLayout>

0 个答案:

没有答案