当使用android studio点击项目时如何在列表视图中添加弹出窗口?

时间:2017-06-14 06:07:03

标签: android listview

我正在listview中使用textview,checkbox,imageview和spinner等多个小部件实现listview。现在我想在点击列表视图的任何一行时显示弹出窗口。我该怎么办 ? 以下是我的代码,你能建议我吗?

// MainActivity.java

公共类MainActivity扩展了AppCompatActivity {

private ListView mListView;

private int[] startImages = new int[]{
        R.drawable.areaname,R.drawable.email,R.drawable.user};

private String[] mColorNames = new String[]{
        "1", "2", "3"};

private List<ListModel> listModelList = new ArrayList<>();
private String TAG = "Lokesh";

private PopupWindow popupWindow;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mListView = (ListView) findViewById(R.id.listView);
    loadListWithImages();
    final ListAdapter adapter = new ListAdapter(listModelList,this);
    mListView.setAdapter(adapter);
    Log.d(TAG, "onCreate: list"+listModelList.size());

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // String s = mListView.getItemAtPosition(position).toString();
           // String s = mListView.getSelectedItem().toString();


        }
    });
}


private void loadListWithImages() {
    for (int i = 0; i < startImages.length; i++) {
        ListModel listModel = new ListModel();
        listModel.setCount(0);
        listModel.setImage(startImages[i]);
        listModel.setName(mColorNames[i]);
        listModelList.add(listModel);
    }
}

//pop up window

private void initiatePopupWindow() {
    try {
        // We need to get the instance of the LayoutInflater
        LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.screen_popup,
                (ViewGroup) findViewById(R.id.popup_element));
        popupWindow = new PopupWindow(layout, 300, 370, true);
        popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);

     //   btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
    //    btnClosePopup.setOnClickListener(cancel_button_click_listener);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {
    public void onClick(View v) {
        popupWindow.dismiss();

    }
};

}

// ListAdapter.java

public class ListAdapter extends BaseAdapter实现了android.widget.ListAdapter

{

    private List<ListModel> listModelList;
    private Context mContext;
    private LayoutInflater inflater;
    private String TAG = "LokeshSA";
    private String[] mColorNames = new String[]{
            "1", "2", "3" };
    private String[] listitems=new String[]{"One", "Two", "Three"};

public ListAdapter(List<ListModel> listModelList, Context mContext) {
    this.listModelList = listModelList;
    this.mContext = mContext;
    inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

}

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    //  ViewHolder holder = null;
    final ViewHolder holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.single_list_items, null);
        holder = new ViewHolder();

        //checkbox

        holder.checkbox = (CheckBox) convertView.findViewById(R.id.listview_cb);
        holder.checkbox
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        ListModel element = (ListModel) holder.checkbox
                                .getTag();
                        element.setSelected(buttonView.isChecked());

                    }
                });
        convertView.setTag(holder);
        holder.checkbox.setTag(listModelList.get(position));

        //spinner

        holder.spinner = (Spinner) convertView.findViewById(R.id.listview_spinner);

        ArrayAdapter<String> startColorsAdapter = new ArrayAdapter<String>(mContext,
                android.R.layout.simple_list_item_1,mColorNames);
        holder.spinner.setAdapter(startColorsAdapter);
        holder.spinner.setSelection(listModelList.get(position).getCount());



        holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position2, long id) {
                ListModel starModel = listModelList.get(position);
                starModel.setCount(position2);
                listModelList.remove(position);
                listModelList.add(position,starModel);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

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

    // initialize image resource
    holder.iamge = (ImageView) convertView.findViewById(R.id.listview_imgs);
    // setting image
    holder.iamge.setImageResource(listModelList.get(position).getImage());

    //textview resource

     holder.textView= (TextView) convertView.findViewById(R.id.listview_tv);
     holder.textView.setText(listitems[position]);
     // holder.textView.setText(listModelList.get(position).getName());


    return convertView;
}
    private class ViewHolder{
        private Spinner spinner;
        private ImageView iamge;
        private TextView textView;
        private CheckBox checkbox;
    }



    @Override
    public int getViewTypeCount() {

    return getCount();
}

    @Override
    public int getItemViewType(int position) {

    return position;
}


}

2 个答案:

答案 0 :(得分:0)

你可以发布你的xml screen_popup ???

在MainActivity中 - 在mListView中调用 initiatePopupWindow()点击以下

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // String s = mListView.getItemAtPosition(position).toString();
           // String s = mListView.getSelectedItem().toString();

    final TextView tv_id = (TextView) view.findViewById(R.id.tv_id);
    String txt = tv_id.getText().toString();//and pass the value("txt" to use it to display on dialog) as 

            initiatePopupWindow(txt);

        }
    });

您也可以参考popupwindow_stackoverflow

答案 1 :(得分:0)

为了从点击的列表项中获取数据,请使用此代码

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, 
    long id) {
        //to get the text from selected list item
        TextView tv = (TextView) view.findViewbyId(R.id.listview_tv);
        String text = tv.getText().toString();

        initiatePopupWindow(text);

    }
});