Question

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 :

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>
Was it helpful?

Solution

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

        }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top