0

我在列表视图中添加了标题。并尝试交换列表视图项目。但它不能正常工作。但是当我不使用 header 时,操作工作正常。代码如下......请帮助我......

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

}
4

1 回答 1

0

您尚未描述“无法正常工作”时看到的内容,但我假设这些行打印了适当的信息:

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

也许您只需要在使用 时减去标题视图的数量positionCitites因为Cities没有任何标题。您正在使用一个标头,因此请尝试:

// 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);
于 2013-01-23T18:58:41.973 回答