简体   繁体   English

从列表中选择项目时如何使用按钮打开弹出窗口?-Android应用程序中

[英]How can I open a popup with a button when i select an item from a list?-in an android application

I have a problem with my app. 我的应用程序有问题。 I have a list of items and when I select one item i want to showcase a popup with some text and with a button that will take me to another activity. 我有一个项目列表,当我选择一个项目时,我想展示一个弹出窗口,其中包含一些文本和一个带我进入另一项活动的按钮。 With the code that i have so far i have the list but is not functional. 到目前为止,我所拥有的代码已经列出了,但没有起作用。 I need to add the popup and the transition to another activity.Can someone help me with my problem? 我需要添加弹出窗口和过渡到另一个活动。有人可以帮助我解决我的问题吗? This is what i have so far..: 这是我到目前为止所拥有的..:

MainActivity: 主要活动:

package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

public class Lesson extends Activity {

public static int [] prgmImages=       {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
                                 R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
                                 R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
                                 R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,};
public static String[] prgmNameList = {"Ex1","Ex2","Ex3","Ex4","Ex5","Ex6","Ex7","Ex8","Ex9","Ex10","Ex11","Ex12",};
public static String[] prgmDescription = {"asdafasdsad","asdsafasfas","aaaaaa","lkjhggfdds",
                                            "asdafasdsad","asdsafasfas","aaaaaa","lkjhggfdds",
                                            "asdafasdsad","asdsafasfas","aaaaaa","lkjhggfdds"};

Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lesson);

    context = this;

    ListView lv = (ListView) findViewById(R.id.listView1);
    lv.setAdapter(new CustomAdapter(this, prgmNameList, prgmImages, prgmDescription));
  }
}

CustomAdapter.java : CustomAdapter.java:

package com.example.test;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {

String [] description;
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;

public CustomAdapter(Lesson lesson, String[] prgmNameList, int[] prgmImages, String[]                  prgmDescription){
    // TODO Auto-generated constructor stub
    result=prgmNameList;
    description=prgmDescription;
    context=lesson;
    imageId=prgmImages;
    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

  @Override
public int getCount() {
    // TODO Auto-generated method stub
      return result.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView tv;
    TextView tv2;
    ImageView img;
}

@Override
 public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;        
         rowView = inflater.inflate(R.layout.list_field, null);
         holder.tv2=(TextView) rowView.findViewById(R.id.textView2);
         holder.tv=(TextView) rowView.findViewById(R.id.textView1);
         holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

     holder.tv2.setText(description[position]);
     holder.tv.setText(result[position]);
     holder.img.setImageResource(imageId[position]);

     rowView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Toast.makeText(context, "You Clicked "+result[position], 2000).show();

        }
        });
    return rowView;
  }
}

activity_lessons.xml: activity_lessons.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Lesson" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

</RelativeLayout>

list_field: list_field:

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="200dp"
    android:layout_height="30dp"
    android:layout_alignTop="@+id/imageView1"
    android:layout_toRightOf="@+id/imageView1"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="200dp"
    android:layout_height="18dp"
    android:layout_below="@+id/textView1"
    android:layout_toRightOf="@+id/imageView1"
    android:text="TextView" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

</RelativeLayout>

You can open alert on item click like this : 您可以像这样打开关于项目单击的警报:

rowView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Toast.makeText(context, "You Clicked "+result[position], 2000).show();

             AlertDialog.Builder builder = new AlertDialog.Builder(context);
             builder.setTitle("Title");

             builder.setMessage("Message");
             builder.setPositiveButton("OK",
                     new DialogInterface.OnClickListener()
                     {

                         public void onClick(DialogInterface dialog, int id)
                         {
                             dialog.cancel();
                              Intent intent = new Intent(context, Activity2.class);
                              context.startActivity(intent);
                         }
                     });
             AlertDialog alert = builder.create();
             alert.show();

        }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我单击列表视图(android studio)中的项目时,如何创建要显示的弹出菜单? - How can I create a popup menu to be shown when I click on an item in listview(android studio)? 如何通过按钮触发列表项的onItemClick? - How can I trigger on onItemClick of a list item from a button? 我想从组合框中选择一个项目,然后单击按钮时根据所选项目打开另一个框架 - i want select an item from the combo box and open another frame according to the selected item when I click a button 如何使用弹出窗口 window 中的按钮从 recyclerview 项目复制文本? - How can i copy a text from the recyclerview item with a button in popup window? 当我在应用浏览器中的android webview中打开链接时,如何在左上方放置关闭按钮 - when i open a link in android webview in app browser how can i put close button on top left 在Netbeans Java应用程序上,如何通过单击按钮来打开新页面(而不是弹出窗口)? - On a Netbeans Java application, how do I open a new page (not a popup) by clicking a button? 单击一个按钮,将启动一个弹出菜单,然后单击一个弹出菜单项,然后所选项目将显示在 android 工作室的按钮文本上 - click on a button, a popup menu will start, and then I click on a popup menu item then selected item will show on buttontext in android studio 如何通过右键单击并同时打开上下文菜单来 select 列表/树/表项? - How can I select a list/tree/table item via right-click and open a context menu at the same time? 我不能 select 列表视图中的项目 - I can't select an item in list view 我无法在 selenium 中选择列表项 - I can not select list item in selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM