替换ListView的“列”中的数据

时间:2013-12-02 13:56:47

标签: java android listview android-contentprovider android-cursoradapter

我的应用中有以下ListView版面:

              < data_label_1 >                            < data_label_2 >
item 1_1_name       10                      item 1_1_name       20   
item_2_2_name       20          ==>         item_2_2_name       30 
item_3_2_name       30                      item_3_2_name       40

它由CustomCursorAdapter填充(其中data_label_1data_label_2只是表格列中的数据)。

> <是我在应用的用户界面中的切换,应该是这样的:用户点击其中一个,列中的数据被其他列中的数据替换。可以在Android上实现这样的东西吗?

2 个答案:

答案 0 :(得分:0)

你的问题有点难以理解。

要更改ListView中的数据,您只需更改与其关联的适配器即可。 定义两个CustomCursorAdapters,并在单击标签时交换分配给ListView的那个。

答案 1 :(得分:0)

要与Cursor适配器一起使用,您可以使用自定义视图绑定器。我在https://github.com/pbirdsall/jasonisframed有一个示例应用。以下是上例中自定义视图绑定器的示例。希望这可以帮助。如果您有任何问题,请告诉我。

public class AllFragment extends ListFragment implements
    LoaderManager.LoaderCallbacks<Cursor> {
AllFragmentSelectedListener mCallback;
private static String str_where = JSONDBProvider.KEY_VALID + " = 'Y'";

public interface AllFragmentSelectedListener {
    public void onAllItemClickSelected();
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    try {
        mCallback = (AllFragmentSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
    }
}

SimpleCursorAdapter adapter;
Activity activity;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    String[] columns = new String[] { JSONDBProvider.KEY_DATA_ID,
            JSONDBProvider.KEY_TEXT, JSONDBProvider.KEY_IMAGE };
    int[] viewfields = new int[] { R.id.fuzz_id, R.id.fuzz_text,
            R.id.fuzz_graphic };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(),
            R.layout.allrow, null, columns, viewfields);

    adapter.setViewBinder(new CustomViewBinder());



    setListAdapter(adapter);

    getListView().setDivider(new ColorDrawable(R.color.black));
    getListView().setDividerHeight(1);

    getListView().setOnItemClickListener(myOnItemClickListener);
    getLoaderManager().initLoader(0, null, this);

}

private OnItemClickListener myOnItemClickListener = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        mCallback.onAllItemClickSelected();

    }
};

private class CustomViewBinder implements ViewBinder {

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

        if (columnIndex == cursor
                .getColumnIndex(JSONDBProvider.KEY_IMAGE)
                && (cursor.getString(cursor
                        .getColumnIndex(JSONDBProvider.KEY_TYPE)))
                        .equals("image")) {
            int is_text = cursor.getColumnIndex(JSONDBProvider.KEY_IMAGE);
            if (is_text != 0) {
                // Set left side icon
                ImageView fuzz_graphic = (ImageView) view
                        .findViewById(R.id.fuzz_graphic);
                if (cursor.getString(cursor
                        .getColumnIndex(JSONDBProvider.KEY_IMAGE)) != null) {
                    WebImage retImage = new WebImage();
                    Bitmap bitmp = null;

                    try {
                        bitmp = retImage
                                .execute(
                                        cursor.getString(cursor
                                                .getColumnIndex(JSONDBProvider.KEY_IMAGE)))
                                .get();
                    } catch (Exception e) {
                        Log.e("ImageLoader", e.getMessage());
                    }
                    fuzz_graphic.setImageBitmap(bitmp);
                } else {
                    fuzz_graphic.setImageResource(R.drawable.no_icon);
                }

                fuzz_graphic.setFocusable(false);

                view.setVisibility(View.VISIBLE);
                fuzz_graphic.setFocusable(false);
                fuzz_graphic.setFocusableInTouchMode(false);
                return true;
            }
        } else {
            return false;
        }
        return false;
    }
}

Handler handler = new Handler();

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] projection = new String[] { JSONDBProvider.KEY_ID,
            JSONDBProvider.KEY_DATA_ID, JSONDBProvider.KEY_TYPE,
            JSONDBProvider.KEY_TEXT, JSONDBProvider.KEY_IMAGE };

    CursorLoader loader = new CursorLoader(getActivity(),
            JSONDBProvider.CONTENT_URI, projection, str_where, null, null);

    return loader;

}

public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    ((SimpleCursorAdapter) this.getListAdapter()).swapCursor(cursor);

}

public void onLoaderReset(Loader<Cursor> loader) {
    ((SimpleCursorAdapter) this.getListAdapter()).swapCursor(null);
}