我對android非常陌生,正在尋求關於從數據庫中獲取的數據的listview優化方面的幫助。「我想檢查這些框,然後按下get按鈕以在烤麪包中顯示它們。」Listview從數據庫中獲取數據的優化
與SimpleCursorAdapter:
package com.example.adapterx;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
DBAdapter myDb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openDB();
display();
insertdata();
}
private void insertdata() {
String pap = "1";
String pap1 = "2";
String pap2 = "3";
String pap3 = "4";
String pap4= "5";
String pap5 = "6";
String pap6 = "7";
String pap7 = "8";
String pap8 = "9";
String pap9 = "10";
String pap10 = "11";
String pap11 = "12";
String pap12 = "13";
String pap13= "14";
String pap14 = "15";
String pap15 = "16";
String pap16 = "17";
String pap17 = "18";
String pap18 = "19";
String pap19 = "20";
String pap20 = "21";
String pap21 = "22";
String pap22 = "23";
myDb.insertRow(pap);
myDb.insertRow(pap1);
myDb.insertRow(pap2);
myDb.insertRow(pap3);
myDb.insertRow(pap4);
myDb.insertRow(pap5);
myDb.insertRow(pap6);
myDb.insertRow(pap7);
myDb.insertRow(pap8);
myDb.insertRow(pap9);
myDb.insertRow(pap10);
myDb.insertRow(pap11);
myDb.insertRow(pap12);
myDb.insertRow(pap13);
myDb.insertRow(pap14);
myDb.insertRow(pap15);
myDb.insertRow(pap16);
myDb.insertRow(pap17);
myDb.insertRow(pap18);
myDb.insertRow(pap19);
myDb.insertRow(pap20);
myDb.insertRow(pap21);
myDb.insertRow(pap22);
Toast.makeText(this, "inserted successfully.", Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
closeDB();
}
private void openDB() {
myDb = new DBAdapter(this);
myDb.open();
}
private void closeDB() {
myDb.close();
}
private void display() {
final Cursor cursor = myDb.getAllRows();
String[] fromFieldNames = new String[]{DBAdapter.KEY_NAME};
int[] toViewIDs = new int[]{R.id.textsv};
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter= new SimpleCursorAdapter(getBaseContext(),R.layout.item_layoutt,cursor , fromFieldNames , toViewIDs ,0);
final ListView myList = (ListView) findViewById(R.id.listView1);
myList.setAdapter(myCursorAdapter);
}
activity_main.xml中
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.adapterx.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/textView1" >
</ListView>
<Button
android:id="@+id/get"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/listView1"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/listView1"
android:text="Get" />
</RelativeLayout>
item_layoutt.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="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/textcb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textsv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="TextView" />
</LinearLayout>
它完美地顯示列表,但是當我點擊任何複選框時,選中的項目會在上下滾動時重複和更改。
我不知道如何解決這個與ViewHolder。我讀了一篇文章http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/,但我未能優化列表視圖。
如何修改MainActivity類,使用MyCustomAdapter類來解決此問題。請幫助我完成整個代碼。
private class MyCustomAdapter extends dataAdapter {
private LayoutInflater mInflater;
private Cursor cur;
public MyCustomAdapter(Context context, Cursor c) {
super(context,c);
this.mInflater = LayoutInflater.from(context);
this.cur = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = this.mInflater.inflate(R.layout.chamber_item, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView)convertView.findViewById(R.id.textsv);
viewHolder.checkbox = (CheckBox)convertView.findViewById(R.id.textcb);
convertView.setTag(viewHolder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
this.cur.moveToPosition(position);
viewHolder.text.setText(this.cur.getString(this.cur.getColumnIndex("texts")));
viewHolder.checkbox.setText(this.cur.getString(this.cur.getColumnIndex("istrue")));
return convertView;
}
static class ViewHolder
{
TextView text;
CheckBox checkbox;
}
}
會你可以解釋一下這一行的更多細節:當我點擊任何複選框時,選中的項目會在上下滾動時重複和更改。 –
當我只檢查項目1時,還會定期檢查5,10,15等。當上下滾動時,我看到檢查的項目會改變它們的位置,項目1未選中,項目3被選中,項目8被選中......這是隨機檢查和未選中。 – babiro
嗯得到它.....等等 –