2015-05-19 107 views
0

我對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> 

enter image description here

它完美地顯示列表,但是當我點擊任何複選框時,選中的項目會在上下滾動時重複和更改。

我不知道如何解決這個與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; 

    } 

} 
+0

會你可以解釋一下這一行的更多細節:當我點擊任何複選框時,選中的項目會在上下滾動時重複和更改。 –

+0

當我只檢查項目1時,還會定期檢查5,10,15等。當上下滾動時,我看到檢查的項目會改變它們的位置,項目1未選中,項目3被選中,項目8被選中......這是隨機檢查和未選中。 – babiro

+0

嗯得到它.....等等 –

回答

0

嘗試下面的適配器,看看它是否有幫助。

private static class MyCustomAdapter extends dataAdapter { 

    private LayoutInflater mInflater; 
    private Cursor cur; 
    private HashMap<Integer, Boolean> tracker = new HashMap<Integer, Boolean>(); 

    public MyCustomAdapter(Context context, Cursor c) { 
     super(context,c); 
     this.mInflater = LayoutInflater.from(context); 
     this.cur = c; 
     for(int i=0;i<c.getCount();i++){ 
     tracker.put(i, false); 
     } 
    } 


    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

    final ViewHolder holder = null; 

    if (convertView == null) { 
    convertView = this.mInflater.inflate(R.layout.chamber_item, null); 
       holder = new ViewHolder(); 
       holder .text = (TextView)convertView.findViewById(R.id.textsv); 
       holder .checkbox = (CheckBox)convertView.findViewById(R.id.textcb); 
       convertView.setTag(holder); 
    } 
    else { 
    holder = (ViewHolder) convertView.getTag(); 
    } 
    this.cur.moveToPosition(position); 
    holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      tracker.put(position, isChecked); 
     } 
    }); 
    holder.text.setText(this.cur.getString(this.cur.getColumnIndex("texts")));   
    holder.checkbox.setText(this.cur.getString(this.cur.getColumnIndex("istrue"))); 
    holder.checkbox.setChecked(tracker.get(position)); 

      return convertView; 
     } 
     static class ViewHolder 
     { 
      TextView text; 
      CheckBox checkbox; 
     } 


    } 
0

添加private ArrayList<Integer> selectedPosision = new ArrayList<Integer>保持選定的複選框的位置,這將有助於您在您的滾動列表視圖,因爲getview(...)方法還將這個時候打電話。在您的getView(...)參數中設置final int position,這樣可以防止在您單擊一個itme時隨機選擇您的listitem。並添加clickListener保持複選框的記錄(選中或取消選中)狀態.. viewHolder.checkbox.setChecked(selectedPosition.indexOf(position)>-1);此行確保此複選框被選中或不...在每次當列表滾動的時候....

private class MyCustomAdapter extends dataAdapter { 

    private LayoutInflater mInflater; 
    private Cursor cur; 
    private ArrayList<Integer> selectedPosision = new ArrayList<Integer>(); 

    public MyCustomAdapter(Context context, Cursor c) { 
    super(context,c); 
    this.mInflater = LayoutInflater.from(context); 
    this.cur = c; 
    } 


    @Override 
    public View getView(final 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"))); 

viewHolder.checkbox.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub     
        if (selectedPosition.indexOf(position) > -1) { 
         selectedPosition.remove(selectedPosition.indexOf(position)); 
        } else { 
         selectedPosition.add(position); 
        } 

       } 
      }); 
      viewHolder.checkbox.setChecked(selectedPosition.indexOf(position)>-1); 
      return convertView; 
     } 
     static class ViewHolder 
     { 
      TextView text; 
      CheckBox checkbox; 

     } 

    }