【问题标题】:showing name of Column in text view在文本视图中显示列的名称
【发布时间】:2019-07-10 11:54:35
【问题描述】:

在我的回收视图中,当用户单击任何项​​目时,将显示一个警告对话框并显示项目名称和一些用于编辑、删除和...的按钮,我如何获取我的项目名称并将其显示在警告对话框?

我尝试了设置文本、光标和许多东西,但都不起作用

这是我正在学习的课程

package com.example.hozoor;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.textfield.TextInputEditText;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Classes extends AppCompatActivity {
    Cursor cursor;
    TextView add;
    Database database;
    private SQLiteDatabase sqLiteDatabase;
    private class_adaptor mclassadaptor;
    RecyclerView.LayoutManager layoutManager;
    String nameclass;



    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_classes );
        Toolbar toolbar = findViewById ( R.id.toolbar );
        setSupportActionBar ( toolbar );

        Database mdatabase = new Database ( this );
        sqLiteDatabase = mdatabase.getWritableDatabase ();
        RecyclerView recyclerView = findViewById ( R.id.rec_1 );
        recyclerView.setLayoutManager ( new LinearLayoutManager ( this ,LinearLayoutManager.VERTICAL,false ) );
        mclassadaptor = new class_adaptor ( this, getAllItem () );

        recyclerView.setAdapter(mclassadaptor);

        FloatingActionButton fab = findViewById ( R.id.fab );
        fab.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {


                AlertDialog.Builder builder = new AlertDialog.Builder ( Classes.this );
                View view1 = getLayoutInflater ().inflate ( R.layout.custom_dialog_add_calss, null );

                final EditText New_kelas = (TextInputEditText) view1.findViewById ( R.id.name_class );
                TextView afzodn = (TextView) view1.findViewById ( R.id.btn_afzodn_new_class );
                final TextView cancel = (TextView) view1.findViewById ( R.id.btn_cancel_new_class );



                afzodn.setOnClickListener ( new View.OnClickListener () {
                    @Override
                    public void onClick(View view) {

                        if (New_kelas.getText().toString().trim().length()==0 ) {


                        } else {
                            Toast.makeText ( Classes.this, "نام کلاس را وارد کنید", Toast.LENGTH_SHORT ).show ();
                            String name=New_kelas.getText().toString();
                            ContentValues cv=new ContentValues();
                            cv.put(Database.COLUMN_NAME,name);

                            sqLiteDatabase.insert(Database.TABLE_NAME,null,cv);

                            mclassadaptor.swapcursor(getAllItem());

                            New_kelas.getText().clear();
                        }


                    }
                } );

                cancel.setOnClickListener ( new View.OnClickListener () {
                    @Override
                    public void onClick(View view) {

                        Intent intent = new Intent ( getApplicationContext (), Classes.class );
                        startActivity ( intent );
                        finish ();
                    }
                } );

                builder.setView ( view1 );
                AlertDialog dialog = builder.create ();
                dialog.show ();


}
        } );
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager ( layoutManager );
        recyclerView.addOnItemTouchListener(new rec_listener(getApplicationContext(), recyclerView, new rec_listener.ClickListener() {
            @Override
            public void onClick(View view, int position) {
                Intent intent =new Intent(getApplicationContext(),Situation.class);
                startActivity(intent);
            }


            @SuppressLint("ResourceType")
            @Override
            public void onLongClick(View view, int position) {
                String id=cursor.getString ( cursor.getColumnIndex ( "_ID" ) );
                String name= (String) getText ( R.id.name_class );
                TextView name_kelas = findViewById ( R.id.txt_name_kelas );

                AlertDialog.Builder builder = new AlertDialog.Builder ( Classes.this );
                View view1 = getLayoutInflater ().inflate ( R.layout.custom_dialog_edit_delete_bikhial, null );
                EditText New_kelas = (TextInputEditText) view1.findViewById ( R.id.name_class );

               String Name= cursor.getColumnName ( Integer.parseInt ( id ) );
                name_kelas.setText ( Name );

                builder.setView ( view1 );
                AlertDialog dialog = builder.create ();
                dialog.show ();

            }
        }));

    }



    private Cursor getAllItem(){
        return sqLiteDatabase.query(
                Database.TABLE_NAME,
                null,
                null,
                null,
                null,
                null,
                Database.COLUMN_TIMESTAMP+" DESC"

        );
}

}

我希望我的文本视图显示列名

【问题讨论】:

    标签: java android sqlite android-studio


    【解决方案1】:

    首先制作两个警报对话框, 第一个用于添加值(名称),第二个用于显示值(名称)。 在要添加事件的活动中显示添加警报对话框,保存该事件,

    public void SaveEvent(String name) {
        databaseHelper.SaveEvent(name);
        databaseHelper.close();
        Toast.makeText(context, Name Saved!!", Toast.LENGTH_SHORT).show();
    }
    

    在视图的onCreate活动中调用该方法,同时启动databasehelper类,

    DatabaseHelper databaseHelper;
    

    调用另一个要显示详细信息的警报框对话框, 并通过

    获取该视图中所有保存的数据
    final MyAdapter myAdapter = new MyAdapter(MainActivity.this,ShowName(name));
                recyclerView.setAdapter(myAdapter);
                eventRecyclerAdapter.notifyDataSetChanged();
    

    在视图中设置 showname 方法

    public void ShowName(String name) {
    String name;
        SQLiteDatabase database = dbOpenHelper.getReadableDatabase();
        Cursor cursor = databaseHelper.ShowDetail(name);
    }
    

    希望对您有所帮助! 谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 2011-06-05
      • 2019-09-19
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多