ListView - Удалить, Редактировать, Добавить - PullRequest
0 голосов
/ 02 ноября 2019

Я вызываю просмотр списка, вызывая одну из кнопок. Я хочу иметь следующие функции в этом списке. «Редактировать, Удалить, Добавить».

Все данные, доступные в базе данных, которая хранится в виде сохраненных данных.

Макет списка просмотра полностью разбросан, что требует выравнивания вида сетки. Я пытался, но так и не получилось. Пожалуйста, порекомендуйте.

public class import2 extends ListActivity {


  TextView lbl;
  DBController controller = new DBController(this);
  Button btnimport;
  ListView lv;
  final Context context = this;
  ListAdapter adapter;

  ArrayList < HashMap < String, String >> myList;
  public static final int requestcode = 1;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display1);
    controller = new DBController(this);
    lbl = (TextView) findViewById(R.id.txtresulttext);
    btnimport = (Button) findViewById(R.id.btnupload);
    lv = getListView();
    btnimport.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        Intent fileintent = new Intent(Intent.ACTION_GET_CONTENT);
        fileintent.setType("*/*");
        try {
          startActivityForResult(fileintent, requestcode);
        } catch (ActivityNotFoundException e) {
          lbl.setText("No activity can handle picking a file. Showing alternatives.");
        }
      }
    });

    myList = controller.getAllProducts();
    if (myList.size() != 0) {
      ListView lv = getListView();
      ListAdapter adapter = new SimpleAdapter(import2.this, myList,
        R.layout.display2, new String[] {
          "id",
          "outletname",
          "number",
          "sunday",
          "monday",
          "tuesday",
          "wednesday",
          "thursday",
          "saturday",
          "calling",
          "closed",
          "week"
        }, new int[] {
          R.id.txtproductcompany, R.id.txtproductname, R.id.txtproductprice, R.id.txtproductprice1, R.id.txtproductprice2, R.id.txtproductprice3, R.id.txtproductprice4, R.id.txtproductprice5, R.id.txtproductprice6, R.id.txtproductprice7, R.id.txtproductprice8, R.id.txtproductprice9
        });
      setListAdapter(adapter);
      lbl.setText("");

    }
  }



  protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (data == null)

      return;
    switch (requestCode) {

      case requestcode:

        String filepath = data.getData().getPath();
        controller = new DBController(getApplicationContext());
        SQLiteDatabase db = controller.getWritableDatabase();
        String tableName = "labels2";
        db.execSQL("delete from " + tableName);

        try {

          if (resultCode == RESULT_OK) {

            try {
              FileReader file = new FileReader(filepath);
              BufferedReader buffer = new BufferedReader(file);
              ContentValues contentValues = new ContentValues();
              String line = "";
              db.beginTransaction();

              while ((line = buffer.readLine()) != null) {


                String[] str = line.split(",", 3); // defining 3 columns with null or blank field //values acceptance

                //Id, Company,Name,Price

                String sno = str[0].toString();
                String id = str[1].toString();
                String name = str[2].toString();
                String sunday = str[3].toString();
                contentValues.put("id", sno);
                contentValues.put("number", id);
                contentValues.put("sunday", sunday);
                db.insert(tableName, null, contentValues);

                lbl.setText("Successfully Updated Database.");

              }
              db.setTransactionSuccessful();

              db.endTransaction();

            } catch (SQLException e) {
              Log.e("Error", e.getMessage().toString());
            } catch (IOException e) {

              if (db.inTransaction())
                db.endTransaction();
              Dialog d = new Dialog(this);
              d.setTitle(e.getMessage().toString() + "first");
              d.show();
              // db.endTransaction();
            }
          } else {

            if (db.inTransaction())

              db.endTransaction();

            Dialog d = new Dialog(this);

            d.setTitle("Only CSV files allowed");

            d.show();
          }
        } catch (Exception ex) {

          if (db.inTransaction())

            db.endTransaction();


          Dialog d = new Dialog(this);

          d.setTitle(ex.getMessage().toString() + "second");

          d.show();

          // db.endTransaction();

        }

    }

    myList = controller.getAllProducts();

    if (myList.size() != 0) {

      ListView lv = getListView();

      ListAdapter adapter = new SimpleAdapter(import2.this, myList,

        R.layout.display2, new String[] {
          "id",
          "outletname",
          "number",
          "sunday",
          "saturday",
          "monday",
          "tuesday",
          "wednesday",
          "thursday",
          "closed",
          "calling",
          "week"
        }, new int[] {
          R.id.txtproductcompany, R.id.txtproductname, R.id.txtproductprice, R.id.txtproductprice1, R.id.txtproductprice2, R.id.txtproductprice3, R.id.txtproductprice4, R.id.txtproductprice5, R.id.txtproductprice6, R.id.txtproductprice7, R.id.txtproductprice8, R.id.txtproductprice9
        });


      setListAdapter(adapter);

      lbl.setText("Data Imported");

    }
  }
}

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


    <LinearLayout
        android:id="@+id/lvh"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffe6e6e6"
        android:orientation="horizontal"
        android:scrollbars="horizontal"
        android:weightSum="4">

        <TextView
            android:id="@+id/txtproductcompany"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="No"
            android:textColor="#000000"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/txtproductname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:padding="3dp"
            android:text="Name"
            android:textColor="#000000"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/txtproductprice"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.68"
            android:gravity="left"
            android:padding="3dp"
            android:text="Number"
            android:textColor="#000000"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/txtproductprice1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:padding="3dp"
            android:text="Sunday"
            android:textColor="#000000"
            android:textSize="10sp" />
        android:textSize="10sp" />
        <TextView
            android:id="@+id/txtproductprice6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:padding="3dp"
            android:text="Sat"
            android:textColor="#000000"
            android:textSize="10sp" />
        android:textSize="10sp" />
        <TextView
            android:id="@+id/txtproductprice2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:padding="3dp"
            android:text="Monday"
            android:textColor="#000000"
            android:textSize="10sp" />
        android:textSize="10sp" />
        <TextView
            android:id="@+id/txtproductprice3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.59"
            android:gravity="left"
            android:padding="3dp"
            android:text="Tues"
            android:textColor="#000000"
            android:textSize="10sp" />
        android:textSize="10sp" />
        <TextView
            android:id="@+id/txtproductprice4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.59"
            android:gravity="left"
            android:padding="3dp"
            android:text="Wed"
            android:textColor="#000000"
            android:textSize="10sp" />

        android:textSize="10sp" />
        <TextView
            android:id="@+id/txtproductprice5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.59"
            android:gravity="left"
            android:padding="3dp"
            android:text="Thurs"
            android:textColor="#000000"
            android:textSize="10sp" />
        android:textSize="10sp" />

        <TextView
            android:id="@+id/txtproductprice7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.39"
            android:gravity="left"
            android:padding="3dp"
            android:text="closed"
            android:textColor="#000000"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/txtproductprice8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.39"
            android:gravity="left"
            android:padding="3dp"
            android:text="calling"
            android:textColor="#000000"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/txtproductprice9"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.73"
            android:gravity="left"
            android:padding="3dp"
            android:text="week"
            android:textColor="#000000"
            android:textSize="10sp" />

    </LinearLayout>
</LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...