Issue
I am using this layout to render a dialog
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
style="ThemeDialog"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<CheckBox
android:id="@+id/AddToDictionary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:checked="false"
android:text="Add" />
<Button
android:id="@+id/declineButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confirm" />
<Button
android:id="@+id/eraseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Erase" />
</LinearLayout>
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="10dp"
android:background="@drawable/border" />
</LinearLayout>
Here is the code in my method to display list of items in an ordinary activity that works very well, but I try to display the same list in a list added to the dialog and this doesn't work although the debugger shows that the reference dialog_suggestions_list_view isn't null neither the array (my arrayAdapter), I need your help to point out where is the problem here, why setAdapter doesn't work with lists in dialogs?
dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.dialog, null);
dialog_suggestions_list_view=(ListView) view.findViewById(R.id.listView2) ;
if(dialog_suggestions_list_view!=null){
dialog_suggestions_list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView < ? > parent, View view, int position, long id) {
String selectedFromList = (String)(dialog_suggestions_list_view.getItemAtPosition(position));
AppCompatEditText fContent = (AppCompatEditText) findViewById(R.id.et_content);
fContent.append(selectedFromList);
fContent.append(" ");
}
});
}else{
Log.i("test","my list not found");
}
ArrayList<String> array=new ArrayList<String>();
array.add("Tic");
array.add("Tac");
array.add("Toe");
ArrayAdapter < String > arrayAdapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, array);
//display listView in ordianr activity
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(arrayAdapter);
//display listView in dialog
if(dialog_suggestions_list_view!=null){
dialog_suggestions_list_view.setAdapter(arrayAdapter);
}else{
Log.i("test","my list not found");
}
Solution
I'm quite confused with your code because it's missing some parts, but i did this see if helps.
dialog = new Dialog(MainActivity.this);
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog), null);
dialog.setContentView(view);
dialog_suggestions_list_view = (ListView) view.findViewById(R.id.listView2);
dialog_suggestions_list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView < ? > parent, View view, int position, long id) {
try {
String selectedFromList = (String)(dialog_suggestions_list_view.getItemAtPosition(position));
AppCompatEditText fContent = (AppCompatEditText) findViewById(R.id.et_content);
fContent.append(selectedFromList);
fContent.append(" ");
} catch (Exception e) {
Log.i("test","my list not found");
}
}
});
}
public ArrayList<String> getList(){
ArrayList<String> array = new ArrayList<String>();
array.add("Tic");
array.add("Tac");
array.add("Toe");
return array;
}
public void setInitialContentValue(){
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getList());
//display listView in ordianr activity
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(arrayAdapter);
//display listView in dialog
try {
dialog_suggestions_list_view.setAdapter(arrayAdapter);
} catch (Exception e) {
Log.i("test","my list not found");
}
}
Answered By - Dam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.