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

时间:2014-03-12 16:55:21

标签: java android android-listview listadapter

我的应用有问题。我有一个项目列表,当我选择一个项目时,我想展示一个带有一些文本和一个按钮的弹出窗口,它将带我到另一个活动。到目前为止我的代码我有列表但不起作用。我需要添加弹出窗口和转换到另一个活动。有人可以帮我解决我的问题吗?  这就是我到目前为止......:

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:

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:

<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:

<?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>

1 个答案:

答案 0 :(得分:1)

您可以点击项目点击打开提醒:

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();

        }
        });