Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:13:10+00:00 2026-06-17T21:13:10+00:00

I’ve added header in listview. and trying to swap listview items. but it’s not

  • 0

I’ve added header in listview. and trying to swap listview items. but it’s not working properly. but when i’m not using header , the operations are working properly.
The code is as below….please help me…

public class Main extends ListActivity {

private static final int O_CLEAR = 3;
private static final int O_ADD = 2;
private static final int ACT_GRP = 1;

private static final int C_DOWN = 14;
private static final int C_UP = 13;
private static final int C_RMV = 12;
private static final int CO_GRP = 11;

ListView listView;
ArrayList<String> Cities;
ArrayAdapter<String> cityAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    initialize();

}

private void initialize() {
    listView = getListView();

    Cities = new ArrayList<String>();

    cityAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, Cities);

    TextView t = new TextView(this);
    t.setText("Add Your Cities in the list From Menu ");
    t.setTextColor(Color.RED);
    listView.addHeaderView(t, "", false);

    listView.setAdapter(cityAdapter);

    registerForContextMenu(listView);

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {

    MenuItem item;

    menu.clear();
    menu.setHeaderIcon(android.R.drawable.star_big_on);
    menu.setHeaderTitle("ListView_Menu");

    item = menu.add(CO_GRP, C_RMV, 0, "Remove");
    item.setIcon(R.drawable.ic_launcher);

    item = menu.add(CO_GRP, C_UP, 1, "MoveUp");

    item = menu.add(CO_GRP, C_DOWN, 2, "Movedown");

}

@Override
public boolean onContextItemSelected(MenuItem item) {

    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();

    int position = info.position;

    TextView view = (TextView) info.targetView;

    switch (item.getItemId()) {
    case C_RMV:

        Cities.remove(view.getText().toString());

        cityAdapter.notifyDataSetChanged();

        break;

    case C_UP:

        if (position == 1) {
            mt("First Item");
        }



        else {


            String pre = (String) listView.getItemAtPosition(position-1);

             System.out.println("amrut"+pre);

            String current = (String) listView.getItemAtPosition(position);
            System.out.println("amrut"+current);

            Cities.set(position, pre);
            Cities.set(position - 1, current);

            //listView.invalidateViews();


            //listView.destroyDrawingCache();
            //listView.setVisibility(ListView.INVISIBLE);
            //listView.setVisibility(ListView.VISIBLE);
            cityAdapter.notifyDataSetChanged();


        }

        break;

    case C_DOWN:

        if (position == Cities.size() - 1) {
            mt("last Item");
        }

        else {

            String next = cityAdapter.getItem(position + 1);
            String current = Cities.get(position);

            Cities.set(position, next);
            Cities.set(position + 1, current);

            cityAdapter.notifyDataSetChanged();

        }

        break;

    }

    return true;
}

@Override
protected void onListItemClick(ListView parent, View v, int position,
        long id) {
    // TODO Auto-generated method stub

    //System.out.println("pos " + position);
    View currentView = parent.getChildAt(position);

    if (isSelected(currentView)) {

        currentView.setBackgroundColor(Color.WHITE);
        currentView.setTag("unselected");
    } else {
        currentView.setTag("selected");
        currentView.setBackgroundColor(Color.BLUE);
    }

}

private boolean isSelected(View childAt) {
    // TODO Auto-generated method stub

    if (childAt.getTag() != null)
        if (childAt.getTag().toString().contentEquals("selected"))
            return true;

    return false;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    mt("onCreateOptionsMenu called...");

    MenuItem item;

    item = menu.add(ACT_GRP, O_ADD, 0, "Addcities");
    // item.setShowAsAction(1);
    item.setIcon(android.R.drawable.btn_plus);
    item.setNumericShortcut((char) 1);

    item = menu.add(ACT_GRP, O_CLEAR, 1, "ClearList");
    item.setIcon(android.R.drawable.ic_delete);

    // getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case O_ADD:

        Intent intent = new Intent(this, Act_AddCity.class);
        startActivityForResult(intent, 1);
        break;

    case O_CLEAR:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Are You Sure...?");
        builder.setCancelable(false);
        builder.setPositiveButton("Yes", new OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                // listView.
                Cities.clear();
                // cityAdapter.notifyDataSetChanged();
                cityAdapter.notifyDataSetInvalidated();
                // cityAdapter.getview

            }
        });

        builder.setNegativeButton("Cancel", new OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                arg0.cancel();
            }
        });

        builder.create().show();
        break;

    }
    return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {

        if (data.getExtras() != null) {

            Cities.addAll(data.getExtras().getStringArrayList("city_name"));
            cityAdapter.notifyDataSetChanged();
        }
    }
    return;

}

private void mt(String string) {

    Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:13:11+00:00Added an answer on June 17, 2026 at 9:13 pm

    You haven’t described what you see when “it’s not working properly”, but I assume these lines print the appropriate information:

    System.out.println("amrut"+pre);
    System.out.println("amrut"+current);
    

    Perhaps you simply need to subtract the number of header Views from position when working with Citites, since Cities doesn’t have any headers. You are using one header, so try:

    // Code for case C_UP:
    Cities.set(position - 1, pre);
    Cities.set(position - 2, current);
    
    // C_DOWN
    Cities.set(position - 1, next);
    Cities.set(position, current);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to understand how to use SyndicationItem to display feed which is
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.