我在Listview中挣扎,如何在此Listview中添加两个值,我正在使用SimpleAdapter但我不能添加两个值,Listview仅显示数据库中的一项,如何从中添加两个值mysql database。如何添加两个值?
        //这是我的布局文件
        //主要活动

       <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="0dp">
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="New Class :"
                android:textSize="17dp"
                android:fontFamily="serif"
                android:textColor="#000"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"/>
            <Spinner
                android:id="@+id/new_class"
                style="@style/Platform.Widget.AppCompat.Spinner"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:background="@drawable/spinner_background"
                android:popupBackground="#fff"
                android:fontFamily="serif"
                android:layout_marginRight="10dp"/>
        </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:choiceMode="multipleChoice"
            android:focusable="false"/>
    </LinearLayout>
    </LinearLayout>


//这是我用于自定义布局的行活动文件

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <TextView
        android:id="@+id/thumbImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="sample text"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <CheckedTextView
        android:id="@+id/itemCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:focusable="false"
        android:focusableInTouchMode="false"/>
</LinearLayout>
</RelativeLayout>


//这是我的代码

listview.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub


              sparseBooleanArray = listview.getCheckedItemPositions();
              String ValueHolder = "" ;
              int i = 0 ;

              while (i < sparseBooleanArray.size()) {

                  if (sparseBooleanArray.valueAt(i)) {
                      ValueHolder += data4.get(sparseBooleanArray.keyAt(i)) + ",";
                  }

                  i++ ;
              }
              ValueHolder = ValueHolder.replaceAll("(,)*$", "");
              Toast.makeText(MainActivity.this,  ValueHolder, Toast.LENGTH_LONG).show();
          }


});


如何创建自定义阵列适配器,请帮助我

最佳答案

只需使用自定义阵列适配器,如下所示

这是我的适配器类

public class ItemAdapter extends ArrayAdapter<User> {

// declaring our ArrayList of items
private ArrayList<User> objects;

/* here we must override the constructor for ArrayAdapter
* the only variable we care about now is ArrayList<User> objects,
* because it is the list of objects we want to display.
*/
public ItemAdapter(Context context, int textViewResourceId, ArrayList<User> objects) {
    super(context, textViewResourceId, objects);
    this.objects = objects;
}

/*
 * we are overriding the getView method here - this is what defines how each
 * list item will look.
 */
public View getView(int position, View convertView, ViewGroup parent){

    // assign the view we are converting to a local variable
    View v = convertView;

    // first check to see if the view is null. if so, we have to inflate it.
    // to inflate it basically means to render, or show, the view.
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_item, null);
    }

    /*
     * Recall that the variable position is sent in as an argument to this method.
     * The variable simply refers to the position of the current object in the list. (The ArrayAdapter
     * iterates through the list we sent it)
     *
     * Therefore, i refers to the current User object.
     */
    User i = objects.get(position);

    if (i != null) {

        // This is how you obtain a reference to the TextViews.
        // These TextViews are created in the XML files we defined.

        TextView tt = (TextView) v.findViewById(R.id.toptext);
        TextView ttd = (TextView) v.findViewById(R.id.toptextdata);
        TextView mt = (TextView) v.findViewById(R.id.middletext);
        TextView mtd = (TextView) v.findViewById(R.id.middletextdata);
        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
        TextView btd = (TextView) v.findViewById(R.id.desctext);

        // check to see if each individual textview is null.
        // if not, assign some text!
        if (tt != null){
            tt.setText("Name: ");
        }
        if (ttd != null){
            ttd.setText(i.getName());
        }
        if (mt != null){
            mt.setText("Price: ");
        }
        if (mtd != null){
            mtd.setText("$" + i.getPrice());
        }
        if (bt != null){
            bt.setText("Details: ");
        }
        if (btd != null){
            btd.setText(i.getDetails());
        }
    }

    // the view must be returned to our activity
    return v;

}


}

像这样在列表视图中设置自定义适配器的实例

ItemAdapter itemAdapter = new ItemAdapter(...);
listView.setAdapter(itemAdapter)


如果有帮助,请验证答案

09-27 00:12