在所有活动中重用自定义列表

时间:2016-03-08 17:07:14

标签: android

在我的课程应用程序中,我列出了包含子课程的课程。课程显示在自定义列表中。一旦用户点击课程标题,特定课程活动将打开课程的详细信息。在该活动内部"课程内容"单击listener的文本视图。用户单击该文本视图将打开具有特定课程的子课程的自定义列表。我尝试在每个课程中为所有子课程使用相同的自定义列表。而不是为每个课程创建单独的自定义列表。我尝试通过Intent将图像数组和courseTitle数组从课程活动发送到自定义列表活动来实现这一目的。但是在单击课程内容文本视图时,自定义列表活动很快就停止了。这是错误

enter image description here

BigdataActivity(课程活动)

md-select

}

列出子课程的自定义列表活动

公共类BDataDialog扩展了Activity {

public class BigdataActivity extends Activity {

ImageView logo;
Button phone,mail,whatsapp;
TextView course;
@Override
protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_bigdata);

    logo=(ImageView) findViewById(R.id.imageView1);
    LayoutParams params = (LayoutParams) logo.getLayoutParams();
    params.width = 350; params.height=150; params.leftMargin=150;
    logo.setLayoutParams(params);
    course=(TextView) findViewById(R.id.course);
    phone=(Button) findViewById(R.id.button1);
    mail=(Button) findViewById(R.id.button2);
    whatsapp=(Button) findViewById(R.id.button3);
    course.setOnClickListener(new OnClickListener() {



        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //MyAlert alert=new MyAlert();
            //alert.show(getFragmentManager(), "Fill the Form");

            int activity=1;
            Resources res=getResources();
            String[] courseTitle=res.getStringArray(R.array.BigData);
            int image[] =
            {
              R.drawable.bigdatahad,
              R.drawable.hadoopadmin,R.drawable.bigdataanalytics,
              R.drawable.testing
            };
            Intent intent=new   
            Intent(BigdataActivity.this,BDataDialog.class);
            intent.putExtra("img", image);
            intent.putExtra("course", courseTitle);
            intent.putExtra("activity", activity);
            startActivity(intent);





        }


    });
    phone.setOnClickListener(new OnClickListener() {

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

            String no="+9187";
            Intent callintent =new Intent(Intent.ACTION_CALL);
            callintent.setData(Uri.parse("tel:"+no));
            startActivity(callintent);

        }
    });

    mail.setOnClickListener(new OnClickListener() {

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

            Intent mailintent= new Intent(Intent.ACTION_SEND);
            mailintent.setData(Uri.parse("mailto:"));
            String[] toadd={"info@jpasolutions.in"};
            mailintent.putExtra(Intent.EXTRA_EMAIL,toadd);
            mailintent.setType("message/rfc822");
            Intent chooser = mailintent.createChooser(mailintent, "Send 
            Email");
            startActivity(chooser);
        }
    });

    whatsapp.setOnClickListener(new OnClickListener() {

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

            String cont="7290";
            openWhatsappContact(cont);

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.bigdata, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    int id = item.getItemId();
    if(id==R.id.action_settings){

        startActivityForResult(new 
        Intent(android.provider.Settings.ACTION_SETTINGS), 0);

    }
    return super.onOptionsItemSelected(item);
}


void openWhatsappContact(String number) {


        Uri uri = Uri.parse("smsto:" + number);
        Intent i = new Intent(Intent.ACTION_SENDTO, uri);
        i.setPackage("com.whatsapp");  
        startActivity(Intent.createChooser(i, ""));

}

}

class CourseAdapter扩展了ArrayAdapter {

ImageView logoview;
ListView list;
String[] courseTitle=getIntent().getStringArrayExtra("course");
int image[]=getIntent().getIntArrayExtra("img");
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_bdata_dialog);
    //logoview=(ImageView) findViewById(R.id.imageView1);
    list=(ListView) findViewById(R.id.listView1);


   // list.setVisibility(View.INVISIBLE);
   /* Resources res=getResources();
    courseTitle=res.getStringArray(R.array.BData);*/
    CourseAdapter listAdapter =new CourseAdapter(this, courseTitle, image);
    list.setAdapter(listAdapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View arg1, int position,
                long id) {
            // TODO Auto-generated method stub
            String value = (String)adapter.getItemAtPosition(position); 
            int activity =getIntent().getIntExtra("activity", 0);

            if (value == courseTitle[0]){

                switch(activity){
                case 1:
                    Uri uriUrl = Uri.parse("http://www.jpasolutions.net/coursedetails.php?cat_id=1&crs_id=1&sub_id=1"); 
                    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);  
                    startActivity(launchBrowser);   
                    finish();
                    break;
                case 2:

                    Uri uriUrl1 = Uri.parse("http://www.jpasolutions.net/coursedetails.php?cat_id=1&crs_id=24&sub_id=1"); 
                    Intent launchBrowser1 = new Intent(Intent.ACTION_VIEW, uriUrl1);  
                    startActivity(launchBrowser1);   
                    finish();
                    break;
                }


            }
            else{

                Uri uriUrl = Uri.parse("http://www.jpasolutions.net/coursedetails.php?cat_id=1&crs_id=32&sub_id=1"); 
                Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);  
                startActivity(launchBrowser);   
                finish();
            }



        }



    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.bdata_dialog, menu);
    return true;
}

}

请帮我解决问题。

1 个答案:

答案 0 :(得分:0)

您的getIntent().getStringArrayExtra("course")正在返回null ...

尝试:

  • 处理oncreate内的此getIntent()事件。

  • 在启动活动之前,如果您在意图上添加额外内容,请进行验证 他们不是空的