Android:只显示ListView中的最后一项,实现BaseAdapter

时间:2015-09-28 12:04:30

标签: java android listview arraylist baseadapter

首先是一些背景信息:

在我的Android应用程序中,我在SQLite数据库中有一些与物理键相关的信息。此信息包括发出密钥的“服务提供商”的名称,以及密钥所针对的门和该门的ID的描述。我试图做的是显示SELECT id AS vendorid FROM vendor where subser... 元素行中每个'键'的所有信息,并在行的末尾有一个删除按钮,可用于从数据库中删除该信息。

现在的问题是,当我尝试这个时,活动中显示的唯一条目是最后一个条目,其他条目将是空白区域。请参见此处的屏幕截图:(http://i.stack.imgur.com/8C15B.png

我用红色勾勒出的空间是前3个条目所在的空白区域。我已经看了这个问题,所有对这类问题的回答都是他们将数据输入arraylist的问题,但我已经测试过了,并且arraylist正确填充了所有4个条目。我只是不知道为什么他们没有显示,我相对新的android所以任何帮助将不胜感激!

以下是相关文件的代码: MainActivity.java :(完成我的调试打印语句。这些打印语句都表明,在addKeyItems()方法的do while循环中添加它们后,ArrayList中有4个不同的条目。)

An error has occurred. DotNetNuke.Services.Exceptions.ModuleLoadException: The underlying system threw an exception. 
---> DotNetNuke.Services.FileSystem.FolderProviderException: The underlying system threw an exception.
---> System.NullReferenceException: Object reference not set to an instance of an object. 
at DotNetNuke.Services.FileSystem.FileManager.GetUrl(IFileInfo file) 
--- End of inner exception stack trace
--- at DotNetNuke.Services.FileSystem.FileManager.GetUrl(IFileInfo file) at DotNetNuke.UI.Skins.Controls.Logo.OnLoad(EventArgs e) 
--- End of inner exception stack trace --- 

activity_main.xml中:

ListView

key_list_entry.xml:

public class MainActivity extends AppCompatActivity {

private ListView mKeyListView;
//for the nav drawer
private ListView mDrawerList;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private String mActivityTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(SaveSharedPreference.getUserName(MainActivity.this).length()==0){
        //log in
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }else{
        setContentView(R.layout.activity_main);

        TextView textView = (TextView) findViewById(R.id.display_username);
        String name = SaveSharedPreference.getUserName(this);
        textView.setText("Logged in: " + name);

        //navigation drawer
        mDrawerList = (ListView)findViewById(R.id.navList);
        System.out.println(mDrawerList);
        addDrawerItems();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mActivityTitle = getTitle().toString();
        setupDrawer();

        //main content
        mKeyListView = (ListView) findViewById(R.id.keyList);
        addKeyItems();

    }
}
private void addKeyItems(){
    DbAccess dba = new DbAccess();
    Cursor cursor = dba.getKeys(getApplicationContext());
    if(cursor.moveToFirst()) {

        List<KeyModel> myKeyModelList = new ArrayList<KeyModel>();
        int position = 0;
        do {
            KeyModel key = new KeyModel();
            key.setReaderId(cursor.getLong(cursor.getColumnIndexOrThrow(KeyReaderContract.KeyEntry._ID)));
            key.setSpName(cursor.getString(cursor.getColumnIndexOrThrow(KeyReaderContract.KeyEntry.SP_NAME)));
            key.setReaderDesc(cursor.getString(cursor.getColumnIndexOrThrow(KeyReaderContract.KeyEntry.READER_DESCRIPTION)));
            myKeyModelList.add(key);
            System.out.println(position+ " "+ key.getSpName()+ " "+ key.getReaderDesc());
            position++;
        } while (cursor.moveToNext());

        for(int i = 0; i < myKeyModelList.size(); i++){
            KeyModel test = myKeyModelList.get(i);
            System.out.println(test.spName+ " "+ test.readerDesc);
        }

        //create adapter with the arraylist of keys
        MyListAdapter mKeyAdapter = new MyListAdapter(getLayoutInflater().inflate(R.layout.key_list_entry, (ViewGroup)findViewById(R.id.main_linear_layout)));
        mKeyAdapter.setModel(myKeyModelList);

        //set the adapter to the ListView
        mKeyListView.setAdapter(mKeyAdapter);

    } else{
        System.out.println("No keys");
    }

}

private class KeyModel{

    long readerId;
    String spName;
    String readerDesc;

    public long getReaderId() {
        return readerId;
    }

    public void setReaderId(long readerId) {
        this.readerId = readerId;
    }

    public String getSpName() {
        return spName;
    }

    public void setSpName(String spName) {
        this.spName = spName;
    }

    public String getReaderDesc() {
        return readerDesc;
    }

    public void setReaderDesc(String readerDesc) {
        this.readerDesc = readerDesc;
    }

    View.OnClickListener listener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, spName, Toast.LENGTH_SHORT).show();
            //do other things to delete this once all items are displaying correctly
        }
    };
}

private class MyListAdapter extends BaseAdapter{
    View renderer;
    List<KeyModel> keys;
    // call this one and pass it layoutInflater.inflate(R.layout.key_list_entry)
    public MyListAdapter(View renderer) {
        this.renderer = renderer;
    }

    // whenever you need to set the list of keys just use this method.
    // call it when you have the data ready and want to display it
    public void setModel(List<KeyModel> keys){
        this.keys = keys;
        System.out.println(keys.size());
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(keys!=null){
            return keys.size();
        }
        return 0;
    }
    @Override
    public Object getItem(int position) {
        if(keys!=null){
            return keys.get(position);
        }
        return null;
    }
    @Override
    public long getItemId(int position) {

        if(keys!=null){
            System.out.println("item id "+ position);
            return position;
        }
        System.out.println(-1);
        return -1;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = renderer;
        }
        KeyModel key = keys.get(position);
        System.out.println(position+ " "+ key.getSpName()+ " "+ key.getReaderDesc());

        TextView rIdView = (TextView)convertView.findViewById(R.id.key_id);
        rIdView.setText(Long.toString(key.readerId));

        TextView spNameView = (TextView)convertView.findViewById(R.id.sp_name);
        spNameView.setText(key.spName);

        TextView readerDescView = (TextView)convertView.findViewById(R.id.reader_desc);
        readerDescView.setText(key.readerDesc);

        Button button = (Button)convertView.findViewById(R.id.delete_keyButton);
        button.setOnClickListener(key.listener);
        convertView.setVisibility(View.VISIBLE);
        return convertView;
    }
}

在MyListAdapter的GetView方法的print语句中:<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The first child in the layout is for the main Activity UI--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/main_list_layout"> <TextView android:id="@+id/display_username" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/keyList" android:orientation="vertical" > </ListView> </LinearLayout> <!-- Side navigation drawer UI --> <ListView android:id="@+id/navList" android:layout_width="200dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#424242"/> 它会在调用该方法时正确打印列表中的4个元素:

这是输出:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/main_linear_layout">

<RelativeLayout
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_weight="0.05">
    <TextView
        android:id="@+id/key_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

<RelativeLayout
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_weight="0.35">
    <TextView
        android:id="@+id/sp_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

<RelativeLayout
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_weight="0.35">
    <TextView
        android:id="@+id/reader_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>
<RelativeLayout
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_weight="0.25">
    <Button
        android:id="@+id/delete_keyButton" style="?android:textAppearanceSmall"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="@string/delete" android:textStyle="bold"  />
</RelativeLayout>

再次感谢任何帮助!这是我的第一篇文章,所以如果我做错了或者您需要更多信息,请告诉我。我认为问题可能出在我的BaseAdapter实现中,因为列表本身包含了所有正确的信息。

1 个答案:

答案 0 :(得分:0)

所有列表项中不得使用单个视图。列表中显示的所有行必须是唯一的视图,根据传递的数据进行回收。

所以改变..

clang: error: linker command failed with exit code 1 (use -v to see invocation)

if(convertView==null){
        convertView = renderer;
 }