gpt4 book ai didi

java - 在 TextView 中显示列的名称

转载 作者:行者123 更新时间:2023-12-02 09:42:25 25 4
gpt4 key购买 nike

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

我尝试了设置文本、光标和很多东西,但没有成功

这是我正在上的类(class)

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"

);
}

}

我希望我的 TextView 显示列名称

最佳答案

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

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

在 View 的onCreate Activity 中调用此方法并启动databasehelper类,

DatabaseHelper databaseHelper;

调用另一个要显示详细信息的警报框对话框,并通过以下方式获取该 View 中所有保存的数据:

final MyAdapter myAdapter = new MyAdapter(MainActivity.this,ShowName(name));
recyclerView.setAdapter(myAdapter);
eventRecyclerAdapter.notifyDataSetChanged();

在 View 中设置showname方法

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

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

关于java - 在 TextView 中显示列的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56970195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com