如何将Listview链接到AlertDialog [英] How can I link a Listview to an AlertDialog

查看:76
本文介绍了如何将Listview链接到AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个单独的ListView类,该类将显示公司名称.现在,我想将此列表视图传递给Main Activity上的AlertDialog.我创建了一个列表布局和一个文本布局来显示列表项.现在,我为Alertdialog创建了另一个布局.我想在第一页的按钮按下时显示此对话框.如何在此Alerdialog中传递listView.这是我的主要活动代码

I have created a seperate ListView Class which will show the name of the company.Now I want to pass this listview to the AlertDialog on Main Activity. I have created a list layout and a text layout to show the item of the list. Now I have created another layout for Alertdialog. I would like to show this dialog on button press on the first page. How can I pass the listView inside this Alerdialog. Here is my code for Main Activity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.option_menu_laout);

    Button button=(Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showDialog();
        }
    });
}

public void showDialog(){

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.diallog_layout, null);
    builder.setView(dialogView);
    builder.setCancelable(true);
    builder.setTitle("Contact");

    ListViewActivity listViewActivity=new ListViewActivity();

    //how can I show li list item here?
  }
}

我的ListViewActivityClass是

My ListViewActivityClass is

public class ListViewActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout);
    populateListView();

}
private void populateListView() {
    //Create list of items
    String[] company= getResources().getStringArray(R.array.company_name);
    //Build Adapter
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(
            this, //Context for the Activity
            R.layout.first_alertlist_textstyle,
            android.R.id.text1,//Layout to use
            company); //Items to be displayed

    //Configure the list view
    ListView companyList =(ListView) findViewById(R.id.list_view);
    TextView alertTitle = (TextView) findViewById(R.id.title);
    companyList.setAdapter(adapter);

  }
}

我的列表布局是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    />
</LinearLayout>

文本布局以显示列表项

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:gravity="center"
    />
</LinearLayout>

对话框布局为

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Contact"
    android:gravity="center_horizontal"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    android:layout_gravity="center"
    />

  </LinearLayout>

修改代码

public class MainActivity extends AppCompatActivity {

ArrayList<Bean> bean=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.option_menu_laout);

    Button button=(Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Alert();
            Toast.makeText(MainActivity.this, "Dialog", Toast.LENGTH_SHORT).show();
        }
    });
}

private void Alert(){
    ArrayList<Bean> bean=new ArrayList<>();
    View view = getLayoutInflater().inflate(R.layout.rating_dialog_layout, null);
    ListView details = (ListView) view.findViewById(R.id.ratingListViewId);
    Adapter adapter = new Adapter(bean,getApplicationContext());
    details.setAdapter(adapter);

    final AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Select");
    builder.setView(view);

    builder.setCancelable(false);
    builder.setNegativeButton("close", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });
    details.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(final AdapterView<?> adapterView, View view, final int i, long l) {

            ;
        }
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

}

推荐答案

在您的主要活动中写:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.option_menu_laout);

    Button button=(Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openCompaniesActivity()
        }
    });
}

public void openCompaniesActivity()(){   
    startActivity(new Intent(this, ListViewActivity.class));
  }
}

您的ListViewActivity:

Your ListViewActivity:

public class ListViewActivity extends Activity{

String[] company;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout);
    populateListView();

}
private void populateListView() {
    //Create list of items
    company= getResources().getStringArray(R.array.company_name);
    //Build Adapter
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(
            this, //Context for the Activity
            R.layout.first_alertlist_textstyle,
            android.R.id.text1,//Layout to use
            company); //Items to be displayed

    //Configure the list view
    ListView companyList =(ListView) findViewById(R.id.list_view);
    companyList.setAdapter(adapter);
    companyList.setOnItemsClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedCompany = company[position];
                //Create you dialog, depending from selected company.
                AlertDialog.Builder builder = new AlertDialog.Builder(ListViewActivity.this);
                LayoutInflater inflater = ListViewActivity.this.getLayoutInflater();
                View dialogView = inflater.inflate(R.layout.diallog_layout, null);
                builder.setView(dialogView);
                builder.setCancelable(true);
                builder.setTitle("Contact");
                builde.show(); 
            }
        })

  }
}

别忘了添加所有必需的活动来体现.

Do not forget to add all needed activities to manifest.

这篇关于如何将Listview链接到AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆