2014-05-24 60 views
1

我申請這個應用程序包含列表視圖,Listview檢索本地數據庫sqlite數據,通過使用適配器,這是我的適配器類代碼:如何刷新BaseAdapter中的Listview?

public class CommandsListAdapter extends BaseAdapter { 

    public static String ID = null; 
    private List<String> data; 

    ArrayList<HashMap<String, String>> commandList = new ArrayList<HashMap<String, String>>(); 

    private ArrayAdapter<String> listAdapter; 

    Context context; 

    public CommandsListAdapter(Context con, 
      ArrayList<HashMap<String, String>> list) { 

     commandList = list; 
     context = con; 


    } 

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


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

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

    @Override 
    public View getView(final int index, View convertView, ViewGroup arg2) { 
     // TODO Auto-generated method stub 


     if (convertView == null) { 
      LayoutInflater mInflater = (LayoutInflater) context 
        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

      convertView = mInflater.inflate(R.layout.people_list, null); 
     } 




     TextView tvTime = (TextView) convertView.findViewById(R.id.timeOfBlock); 
     TextView tvName = (TextView) convertView.findViewById(R.id.name); 

     tvName.setText(commandList.get(index).get(MyDatabase.Number_Block)); 
     tvTime.setText(commandList.get(index).get(MyDatabase.Time_Of_Block)); 

     Typeface tf = Typeface.createFromAsset(context.getAssets(), 
       "segoeuil.ttf"); 
     tvTime.setTypeface(tf); 
     tvName.setTypeface(tf); 

     convertView.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       // Message.message(context, 
       // commandList.get(index).get(MyDatabase.Block_Table_Id)); 

       Intent i = new Intent(context, MessageDetail.class); 

       i.putExtra(ID, 
         commandList.get(index).get(MyDatabase.Block_Table_Id) 
           .toString()); 

       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

      // context.startActivity(i); 

      } 
     }); 

     convertView.setOnLongClickListener(new OnLongClickListener() { 

      public boolean onLongClick(View v) { 
       // TODO Auto-generated method stub 
       ID=commandList.get(index).get(MyDatabase.Block_Table_Id); 
       removeListItem(v, index); 
       return false; 
      } 
     }); 

     return convertView; 
    } 

    protected void removeListItem(View rowView, final int positon) { 

     final Animation animation = AnimationUtils.loadAnimation(
       context, android.R.anim.slide_out_right); 
     rowView.startAnimation(animation); 
     Handler handle = new Handler(); 
     handle.postDelayed(new Runnable() { 

      @Override 
      public void run() { 
       commandList.remove(positon); 



      MyDatabase db=new MyDatabase(context); 
      db.open(); 
     // db.deleteBlock(ID); 
      db.close(); 

       Message.message(context, ID); 

      } 
     }, 1000); 

    } 



} 

,這是我的活動課代碼:

public class ListOfBlock extends Activity { 


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

     ActionBar actionBar = getActionBar(); 
     // for color 
     actionBar.setBackgroundDrawable(new ColorDrawable(Color 
       .parseColor("#a20000"))); 

     if (actionBar != null) { 
      actionBar.setTitle(getResources().getString(
        R.string.title_activity_list_of_block)); 
      actionBar.setDisplayHomeAsUpEnabled(true); 
      // actionBar.setIcon(R.drawable.ic_launcher); 
     } 

     ListView lvCommands = (ListView) findViewById(R.id.ListOfBlockerListView); 

     lvCommands.setAdapter(new CommandsListAdapter(getApplicationContext(), 
       new MyDatabase(getApplicationContext()).getBlockers())); 

    } 


} 

現在我想刷新getview方法中的列表視圖,我不知道如何做到這一點,我找到了一些答案,但真的沒有幫助我,感謝任何幫助。

+0

'getView'方法自動 –

+0

添加視圖列表視圖,並刷新列表使用SQLite工作時不使用底座適配器,使用SimpleCursorAdapter相反,它會正確使用時自動刷新 – pskink

+0

哈立德塔哈如何做到這一點你能告訴我嗎? –

回答

1

我希望這是幫助你,類適配器內做到這一點: -

ArrayList<HashMap<String, String>> commandList = new ArrayList<HashMap<String, String>>(); 



private ArrayList<HashMap<String, String>> searchArrayList; 

public CommandsListAdapter(Context con, 
     ArrayList<HashMap<String, String>> list) { 

    commandList = list; 
    context = con; 

    searchArrayList=list; 
} 

,創造更新的功能你listview這個樣子,也內適配器類別: -

public void updateResults(ArrayList<HashMap<String, String>> results) { 
     searchArrayList = results; 
     //Triggers the list update 
     notifyDataSetChanged(); 
    } 

以及任何你想更新你的listview的地方打電話給這個功能,我爲你做了這個Sister,就像這樣: -

updateResults(commandList); 

有一點可以肯定的是,這些代碼都是你的代碼,但我做了一些修改,並且參數updateResults函數commandList這是你已經定義的ArrayAdapter。 我希望這是工作

+0

'OOOOOOOOMMMMMMMGGGG',sooooooooooo真棒,是啊,我想這個,謝謝你的回答soooooooooooooooo強大,謝謝你兄弟:)當然是呀 –

+0

。 – ROR

0

只需調用BaseAdapter類的notifyDataSetChanged()即可刷新列表視圖項。根據您的要求適當調用它。 乾杯。

3
YourActivity.this.recreate(); 

這將重新創建您的列表。在執行這些操作的行爲中保持一個變量n = false。然後,當您使用列表回叫活動時,只需將其作爲true即可。

在你perfom這些操作的activiy,

Intent i = new Intent(Operations.this,ListActivity.class); 
ListActivity.n=true; 
startActivity(i); 

在ListActivity中的onCreate創建一個類變量boolean n=false;

if(n){ 
ListActivity.this.recreate(); 
} 

希望它能幫助。乾杯!