0

我有一个自定义 SimpleCursorAdapter 和一个列表视图。列表的每一行都有一个名称和一个按钮。当我按下每个名称的按钮时,会出现一个带有描述的对话框。在自定义 SimpleCursorAdapter 中,我为按钮设置了 onclick 方法。当我有一个大列表时,我的 listView 会得到一个滚动条。而且我不知道为什么,当我向下滚动时,列表的最后一行没有显示每一行的正确描述。这是我的代码:

public class listServicesCursorAdapter extends SimpleCursorAdapter{

private Context context;
private int layout;
private String[] from;
private int[] to;


public listServicesCursorAdapter (Context context, int layout, Cursor c,
        String[] from, int[] to, int flags) {

    super(context, layout, c, from, to, flags);

    this.context = context;
    this.layout = layout;
    this.from = from;
    this.to = to;

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(layout, parent, false);

    //Column of BD that we want to recover
    String column = null;

    //Index of the column of  DB
    int nameCol = 0;

    //Result of obtain the index of the column of DB 
    String nombre = null;

    //Name of the textView in the Layout where we want to show the result
    TextView name_text= null;

    String description = null;
    String nameService = null;

    //For each value of DB, we show it in the text view.
    for (int i=0; i<from.length; i++){
        column= from[i];
        nameCol = cursor.getColumnIndex(column);
        name = cursor.getString(nameCol);

        //the values to[i] equals to 0 indicates values that we need but 
                    //that we are not showing in the list directly
        //0 -> description
        if(to[i] == 0){
            description = name;
        }else{
            nameService = name; 
            name_text = (TextView) v.findViewById(to[i]);
            if (name_text != null) {
                name_text.setText(name);
            }
        }
    }


    ImageButton buttonDescription = (ImageButton)  v.findViewById(R.id.imageButtonDescription);

    //we store in a bundle the name and description of the service, so we can use it in
            // the setOnClickListener method.
    final Bundle mArguments = new Bundle();
    mArguments.putString("name", nameService);
    mArguments.putString("description", description);



    buttonDescription .setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v){

            AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

            builder.setMessage(mArguments.getString("description"))
            .setTitle(mArguments.getString("name"))
            .setCancelable(false)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            AlertDialog alert = builder.create();
            alert.show();

        }});

    return v;
}

}

这是我调用适配器的地方:

        ServiceSqliteDao serviceDao = new ServiceSqliteDao();
            //get the services for DB
    Cursor mCursorServices = serviceDao.listServices(getActivity());


    if(mCursorServices.getCount()>0){
        //indicate the fields we want to show (from) and where (to)
        String[] from = new String[] { "name", "description"};
        int[] to = new int[] { R.id.checkBoxService,0};
        ListView lvServices = (ListView) v.findViewById (R.id.listViewServices);

        ListServicesCursorAdapter notes = new ListServicesCursorAdapter (getActivity(), R.layout.activity_file_service, mCursorServices, from, to, 0);
        lvServices.setAdapter(notes);

为什么我会得到这种行为?我得到了列表中的所有名称,但是当我以水平方式按下按钮(我的意思是水平放置平板电脑)并在列表中获得滚动条时,我没有得到正确的描述。另一方面,如果我垂直使用平板电脑,我不会在我的列表中看到滚动条,我会在每个按钮中获得正确的描述。

这是我的布局:

<ListView
    android:id="@+id/listViewServices"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

解决方案: newView 应如下所示:

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(layout, parent, false);

    return v;
}

和 bindView 应该是这样的:

@Override
public View bindView(View v, Context context, Cursor cursor) {

    //Column of BD that we want to recover
    String column = null;

    //Index of the column of  DB
    int nameCol = 0;

    //Result of obtain the index of the column of DB 
    String nombre = null;

    //Name of the textView in the Layout where we want to show the result
    TextView name_text= null;

    String description = null;
    String nameService = null;

    //For each value of DB, we show it in the text view.
    for (int i=0; i<from.length; i++){
        column= from[i];
        nameCol = cursor.getColumnIndex(column);
        name = cursor.getString(nameCol);

        //the values to[i] equals to 0 indicates values that we need but 
                    //that we are not showing in the list directly
        //0 -> description
        if(to[i] == 0){
            description = name;
        }else{
            nameService = name; 
            name_text = (TextView) v.findViewById(to[i]);
            if (name_text != null) {
                name_text.setText(name);
            }
        }
    }

/********************************NEW CODE ************************************/

String uniMedition = cursor.getString(cursor.getColumnIndex("unitMedition"));
    if(uniMedition.equals("none")){
        EditText etMedida = (EditText) v.findViewById(R.id.editTextMedida);
        etMedida.setVisibility(View.INVISIBLE);

        TextView tvUniMedition = (TextView) v.findViewById(R.id.textViewUniMedition);
        tvUniMedition .setVisibility(View.INVISIBLE);


    }else{
        EditText etMedida = (EditText) v.findViewById(R.id.editTextMedida);
        etMedida.setVisibility(View.VISIBLE);

        TextView tvUniMedition = (TextView) v.findViewById(R.id.textViewUniMedition);
        tvUniMedition .setVisibility(View.VISIBLE);
        tvUniMedition .setText(uniMedition);

    }

/********************************END NEW CODE ************************************/

    ImageButton buttonDescription = (ImageButton)  v.findViewById(R.id.imageButtonDescription);

    //we store in a bundle the name and description of the service, so we can use it in
            // the setOnClickListener method.
    final Bundle mArguments = new Bundle();
    mArguments.putString("name", nameService);
    mArguments.putString("description", description);



    buttonDescription .setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v){

            AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

            builder.setMessage(mArguments.getString("description"))
            .setTitle(mArguments.getString("name"))
            .setCancelable(false)
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            AlertDialog alert = builder.create();
            alert.show();

        }});

}

}

现在一切正常!

4

1 回答 1

1

为什么我会得到这种行为?我得到了列表中的所有名称,但是当我以水平方式按下按钮(我的意思是水平放置平板电脑)并在列表中获得滚动条时,我没有得到正确的描述。

当您ListView没有空间显示所有行时,出于性能原因,它将回收行视图。问题是,在您SimpleCursorAdapter覆盖 当 ListView 没有回收视图时newView()才会调用的方法。覆盖以完成工作,因为为每一行调用该方法,在该方法中只是膨胀/构建行布局。bindView()newView()

于 2013-05-24T18:56:48.407 回答