在文本视图中显示列的名称

时间:2019-07-10 11:54:35

标签: java android sqlite android-studio

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

我尝试设置文本,光标和许多其他东西,但是没有用

这是我正在上的课

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"

        );
}

}

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

1 个答案:

答案 0 :(得分: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);
}

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