我正在使用自定义对话框,在长按一个名为pg的按钮时调用该对话框。 我想在自定义对话框中显示ListView, ListView和自定义对话框都有相同的xml文件:custome_dialog 。 问题是:当我尝试声明ListView的适配器时,应用程序崩溃。 这是我的 Gorups.this :
的Java代码public class Gorups extends AppCompatActivity {
Button pg;
ListView pglist;
private static String[] NAMES=new String[]{
"A","B","C"
}; //Items To show in ListView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gorups);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//LongClick on PG////////////////////////////////////Start
pg=findViewById(R.id.pgbtn);
pg.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
final AlertDialog.Builder dialog = new AlertDialog.Builder(Gorups.this);
view = getLayoutInflater().inflate(R.layout.custome_dialog,null);
dialog.setView(view);
AlertDialog customDialog = dialog.create();
customDialog.setTitle("Saved Contacts");
customDialog.setMessage("Dialog is Shown");
//Declaring ListView Adapter
pglist = (ListView) findViewById(R.id.lvpg);
ArrayAdapter<String> adapter = new ArrayAdapter<>(Gorups.this, R.layout.custome_dialog, NAMES);
pglist.setAdapter(adapter);
//Calling ListView:--//////////////////////////////////////////////////
pglist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String value=(String)pglist.getItemAtPosition(position);
Toast.makeText(Gorups.this,"U pressed: "+position+" and: "+value,Toast.LENGTH_SHORT).show();
}
});
customDialog.show();
return true;
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
这是custome_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="25dp">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ListView
android:id="@+id/lvpg"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
它可能有什么问题?
答案 0 :(得分:1)
使用此
pglist = (ListView) dialog.findViewById(R.id.lvpg);
而不是
pglist = (ListView) findViewById(R.id.lvpg);
另一个问题是您为
listview
适配器设置了相同的布局 custome_dialog 检查
试试这个
ArrayAdapter<String> adapter = new ArrayAdapter<>(Gorups.this, android.R.layout.simple_list_item_1, NAMES);
或者试试这个
Dialog dialog=new Dialog(Gorups.this);
dialog.setTitle("Saved Contacts");
dialog.setContentView(R.layout.custome_dialog);
pglist = (ListView) dialog.findViewById(R.id.lvpg);;
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
Gorups.this,
android.R.layout.simple_list_item_1, NAMES );
pglist.setAdapter(arrayAdapter);
dialog.show();
修改强>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ListView
android:id="@+id/lvpg"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
答案 1 :(得分:1)
而不是这一行:
view = getLayoutInflater().inflate(R.layout.custome_dialog,null);
使用:
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(R.layout.custome_dialog,null);
也可以使用:
pglist = (ListView) dialog.findViewById(R.id.lvpg);
而不是:
pglist = (ListView) findViewById(R.id.lvpg);
并用getContext()
替换所有Groups.this并替换它:
new ArrayAdapter<>(Gorups.this, R.layout.custome_dialog, NAMES);
使用:
new ArrayAdapter<>(getContext(),android.R.layout.simple_list_item_1, NAMES);