Android列表视图不会出现

时间:2015-04-19 06:02:26

标签: android android-listview

我是Android开发人员的新手,这是我第一天触及Android工作室。只是一个愚蠢的问题:我试图在MainActivity中实现Listview,但它没有出现,请帮忙。

以下是我的文件

MainActivity.java

package com.example.mina.mynote;

import android.app.ListActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.sunny.mynote.com.example.sunny.mynote.data.NoteDataSource;
import com.example.sunny.mynote.com.example.sunny.mynote.data.NoteItem;

import java.util.List;


public class MainActivity extends ActionBarActivity {

    private NoteDataSource datasource;
    List<NoteItem> notesList;


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

        datasource = new NoteDataSource(this);

        refreshDisplay();
    }

    private void refreshDisplay() {
        notesList = datasource.findAll();
        ArrayAdapter<NoteItem> adapter =
                new ArrayAdapter<NoteItem>(this, R.layout.list_item_layout, notesList);
        ListView lv = (ListView) findViewById(R.id.list);
        lv.setAdapter(adapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xaml

<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"
    tools:context="com.example.mina.mynote.MainActivity">

    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

</RelativeLayout>

NoteItem.Java

package com.example.sunny.mynote.com.example.sunny.mynote.data;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Sunny on 19/04/2015.
 */
public class NoteItem {
    private String key;
    private String text;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public  static NoteItem getNew()
    {
        Locale locale = new Locale("en_US");
        Locale.setDefault(locale);

        String pattern;
        pattern = "yyyy-MM-dd HH:mm:ss Z";

        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat(pattern);
        String key = formatter.format(new Date());

        NoteItem note = new NoteItem();
        note.setKey(key);
        note.setText("");
        return note;
    }

    @Override
    public String toString() {
        return this.getText();
    }
}

NoteDataSource.Java

package com.example.sunny.mynote.com.example.sunny.mynote.data;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Created by Sunny on 19/04/2015.
 */
public class NoteDataSource {

    private static final  String PREFKEY = "notes";
    private SharedPreferences notePrefs;

    public NoteDataSource(Context context)
    {
        notePrefs = context.getSharedPreferences(PREFKEY, Context.MODE_PRIVATE);
    }


    public List<NoteItem> findAll()
    {
        Map<String, ?> notesMap = notePrefs.getAll();

        SortedSet<String> keys = new TreeSet<String>(notesMap.keySet());



        List<NoteItem> noteList = new ArrayList<NoteItem>();
        for (String key : keys)
        {
            NoteItem note = new NoteItem();
            note.setKey(key);
            note.setText((String) notesMap.get(key));
            noteList.add(note);
        }
        return noteList;
    }

    public boolean update(NoteItem note)
    {
        SharedPreferences.Editor editor = notePrefs.edit();
        editor.putString(note.getKey(), note.getText());
        editor.commit();

        return true;
    }

    public boolean remove(NoteItem note)
    {
        if (notePrefs.contains(note.getKey()))
        {
            SharedPreferences.Editor editor = notePrefs.edit();
            editor.remove(note.getKey());
            editor.commit();
        }

        return true;
    }

}

3 个答案:

答案 0 :(得分:0)

  1. 检查您的notesList是否有任何数据。
  2. 我建议您使用SimpleAdapter或覆盖BaseAdapter

答案 1 :(得分:0)

解决问题的一个简单方法是在refreshDisplay函数中添加此代码

notesList = datasource.findAll();
ArrayList<String> notesListString =  new ArrayList<String>();
for(int i=0;i<notesList.size();i++)
{
    notesListString.add(notesList.get(i).toString());
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, notesListString);
ListView lv = (ListView) findViewById(R.id.listView1);

lv.setAdapter(adapter);

答案 2 :(得分:0)

我测试了你的代码并且它有效:)

首先在refreshDisplay的MainActivity中:

ArrayAdapter<NoteItem> adapter =
            new ArrayAdapter<NoteItem>(this, android.R.layout.simple_list_item_1, notesList);

使用android.R.layout.simple_list_item_1而不是R.layout.list_item_layout

  1. Edited NoteItem getNew()

    note.setText(key)代替note.setText("")

  2. 我添加了

    datasource = new NoteDataSource(this); datasource.update(NoteItem.getNew());

  3. 我运行你的应用4次:) 结果:http://www81.zippyshare.com/v/DPnpaT5B/file.html 你可能用很多datasource.update(NoteItem.getNew()运行你的应用程序一次,这就是你的问题:D 在findAll方法中,您使用Set删除了重复项:) 如果您想在ArrayAdapter中使用自定义布局,请在我的答案处查看Add a Button to a ListView for each Element:)