使用单选按钮显示alertDialog并将选定的一个添加到listView - Android

时间:2016-04-15 10:18:06

标签: android listview android-alertdialog

我想开发一个简单的android页面,其代码如下所示

>>> lableArray=['l1','l2','l3']
>>> dataArray=['d1','d2','d3']
>>> rdd2=sc.parallelize(lableArray)
>>> rdd1=sc.parallelize(dataArray)
>>> rdd1.collect()
['d1', 'd2', 'd3']
>>> rdd2.collect()
['l1', 'l2', 'l3']
>>> rdd=rdd2.zipWithIndex().map(lambda x:(x[1],x[0])).join(rdd1.zipWithIndex().map(lambda y:(y[1],y[0]))).map(lambda x:x[1])
>>> rdd.collect()
[('l1', 'd1'), ('l2', 'd2'), ('l3', 'd3')]

我的java代码如下。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <RelativeLayout
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="3"
        android:background="#2196F3">
    </RelativeLayout>
    <LinearLayout
        android:id="@+id/ll3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">


        <ListView
            android:id="@+id/item_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ffffffff">

        </ListView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:orientation="horizontal"
            android:gravity="center_horizontal"
            android:background="#2196F3">

            <Button
                style="?android:attr/buttonStyleSmall"
                android:id ="@+id/addItem"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:text="Add Item"
                android:layout_marginTop="10dp"
                android:background="@layout/button_rounded"
                android:textColor="#ffffffff"/>

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

当我点击添加项目按钮时,我想要一个单选按钮的警告对话框出现。单选按钮列表将从import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener { final Context context = this; private ListView lv; private Button b; ArrayList<String> items_list = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b = (Button)findViewById(R.id.addItem); b.setOnClickListener(this); lv = (ListView)findViewById(R.id.item_list); lv.setAdapter(new VersionAdapter(this)); } @Override public void onClick(View v) { // TODO Auto-generated method stub String[] items = {"Pen","Book","File","Note"}; if (v.getId() == R.id.addItem) { // } } class VersionAdapter extends BaseAdapter { private LayoutInflater layoutInflater; public VersionAdapter(MainActivity activity) { layoutInflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return items_list.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View listItem = convertView; int pos = position; TextView tv = (TextView) listItem.findViewById(R.id.item_element); if(!items_list.isEmpty()) { tv.setText(items_list.get(pos)); } return listItem; } } } 数组中填充。当我选择一个项目并点击items时,它应该会添加到屏幕上显示的列表视图ok(id - lv)。

2 个答案:

答案 0 :(得分:4)

显示对话框的代码

private void showAlertWithRadioButtons(){
AlertDialog.Builder builder = new AlertDialog.Builder(ShowDialog.this);//ERROR ShowDialog cannot be resolved to a type
        builder.setTitle("Alert Dialog with ListView and Radio button");
        builder.setSingleChoiceItems(items, -1,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                         // Toast.makeText(getApplicationContext(), items[item],
                                Toast.LENGTH_SHORT).show();
                    }
                });
    }
 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {

        ListView lw = ((AlertDialog)dialog).getListView();
        Object checkedItem = lw.getAdapter().getItem(lw.getCheckedItemPosition());
        itemsList.add(String.valueOf(checkedItem));
        adapter.notifyDataSetChanged();

        }
    });}

使用listview将适配器设置为:

List<String> itemsList = Arrays.asList(items);
// create ArrayAdapter with this exact parameters, not unique because of your app
ArrayAdapter<String> adapter = new VersionAdapter(this, itemsList);
lv.setAdapter(adapter);

经过一些修改后,这是你的适配器

class VersionAdapter extends ArrayAdapter<String> {

private List<String> listItems;
private LayoutInflater layoutInflater;

public VersionAdapter(Activity activity, List<String> listItems) {
    super(activity, 0, listItems);
    layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.listItems = listItems;
}

@Override
public int getCount() {
    return listItems.size();
}

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    String item = listItems.get(position);
    final ViewHolder holder;
    if (convertView == null)
    {
        convertView = layoutInflater.inflate(R.layout.list_item, parent, false);
        holder = new ViewHolder();

        holder.itemElement = (TextView) convertView.findViewById(R.id.item_element);
        convertView.setTag(holder);

    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
        ((ViewHolder) convertView.getTag()).itemElement.setTag(item);
    }

    if(!item.isEmpty()) {
        holder.itemElement.setText(item);

    }
    return convertView;
}

static class ViewHolder {
    TextView itemElement;
}

}

答案 1 :(得分:1)

try this,

set adapter with List;

VersionAdapter versionAdapter = new VersionAdapter(this),items_list
lv.setAdapter(versionAdapter);

Add this Adapter:

class VersionAdapter extends BaseAdapter {
        private LayoutInflater layoutInflater;
        private  ArrayList<String> items_list
        public VersionAdapter(MainActivity activity,ArrayList<String> items_list) {
            layoutInflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.items_list=items_list;
        }

        @Override
        public int getCount() {
            return items_list.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ViewHolder viewHolder = null;
    if(convertView==null){
            viewHolder = new ViewHolder();
            viewHolder.tv = (TextView) listItem.findViewById(R.id.item_element);
            convertView.setTag(viewHolder);
            }else{
           viewHolder = (ViewHolder) convertView.getTag();
            }
            if(!items_list.get(position).isEmpty()) {
                viewHolder.tv.setText(items_list.get(pos));

            }
            return convertView;
        }

      public void addItem(String text){
         items_list.add(text);
         notifyDataSetChanged() 
        }

    }
}

and On Click Call Add Method Of Adapter:

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        String[] items = {"Pen","Book","File","Note"};
        if (v.getId() == R.id.addItem) {

            versionAdapter.addItem(radioBtnText);
        }
    }