1

I have application this application contain listview , Listview retrieve data from local database sqlite , by using adapter this is my Adapter class code :

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);

    }



}

and this is my Activity Class code :

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()));

    }


}

now I want to refresh the listview within getview method , I don't know how to do this , and I found some answer but really this is not helped me , thanks for any help .

Shanaz K
  • 678
  • 2
  • 13
  • 28

3 Answers3

2
YourActivity.this.recreate();

This will recreate your list. Keep a variable say n=false in the actvity where you perfom those operations. Then when you call back the activity with list, just pass this as true.

In the activiy where you perfom those operations,

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

In onCreate of ListActivity create a class variable boolean n=false;

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

Hope it helps. Cheers!

vishalmullur
  • 1,094
  • 4
  • 14
  • 32
1

I hope this is help you , within the class adapter do this :-

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;
}

and create a function for update your listview like this , also within Adapter Class :-

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

and any where you want to update your listview call this function , and I did this for you Sister , like this :-

updateResults(commandList); 

one thing for sure these codes above this is all your code but I made some modify , and the parameter of updateResults function commandList this is your ArrayAdapter you defined already . I hope this is work

ROR
  • 271
  • 3
  • 29
  • 1
    `OOOOOOOOMMMMMMMGGGG` , sooooooooooo awesome ,yeah , I want this , thank you your answer soooooooooooooooo powerful , thank you bro :)\ – Shanaz K May 24 '14 at 12:56
0

Simply calling notifyDataSetChanged() of BaseAdapter class refresh the list view items. Call it appropriately based upon your requirement. Cheers.

Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60