я хочу добавить два элемента в этот список из mysql

0

Я боюсь из 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();
          }


});

как создать пользовательский адаптер массива, пожалуйста, помогите мне

Теги:

1 ответ

0

просто используйте адаптер пользовательского массива, как показано ниже.

это мой класс адаптера

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)

если это помогло, пожалуйста, подтвердите ответ

  • 0
    Вы можете создать собственный массив адаптер для моего класса
  • 0
    да, просто дайте мне файл макета для элементов списка и название модели, используемое для установки данных элементов
Показать ещё 3 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню