user3880757
user3880757

Reputation: 70

android listview onItemClick

Every time I run this, I get error. I don't know what is wrong ? I just want to get the string from array by index and put it on the URL and run it in anther activity.

package info.androidhive.customlistviewvolley;

import java.util.ArrayList;

import com.squareup.picasso.Picasso;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.app.Activity;
import android.content.Intent;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class Show extends Activity {
    ArrayList<String> lista = new ArrayList<String>();
    ArrayAdapter<String> adaptera;
       int po;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);

     ListView myListView = (ListView)findViewById(R.id.mylistView1);
        myListView.setClickable(true);  
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                 Intent appInfo = new Intent(getApplicationContext(), VideoViewActivity.class);
                 ArrayList<String> dad =appInfo.getStringArrayListExtra("eps");
                 int itemPosition  = position;
                 appInfo.putExtra("url",dad.get(itemPosition).toString());
                 startActivity(appInfo);
              }
            });



    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Intent i = getIntent();
        String aaaaa = i.getStringExtra("img");
        String tit = i.getStringExtra("rank");
        TextView te = (TextView) findViewById(R.id.textView1);
        te.setText(tit);
        ArrayList<String> dad =i.getStringArrayListExtra("eps");
        ImageView isd = (ImageView)findViewById(R.id.imageView1);
        Picasso.with(getBaseContext()).load(aaaaa).into(isd);
        ListView myListView = (ListView)findViewById(R.id.mylistView1);
        ArrayList<String> myStringArray1 =  new ArrayList<String>();
        for (int f= 1; f <= dad.size();f++)
        {
            myStringArray1.add(String.valueOf(f));
        }
        adaptera = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myStringArray1);

            adaptera.notifyDataSetChanged();
            myListView.setAdapter(adaptera);
    }


}

Upvotes: 3

Views: 1319

Answers (2)

Simon Marquis
Simon Marquis

Reputation: 7516

On this bit of code:

Intent appInfo = new Intent(getApplicationContext(), VideoViewActivity.class);
ArrayList<String> dad =appInfo.getStringArrayListExtra("eps");
int itemPosition  = position;
appInfo.putExtra("url",dad.get(itemPosition).toString());
startActivity(appInfo);

The variable dad is null, and therefore dad.get(itemPosition) throws a NullPointerException.

Upvotes: 2

Salem
Salem

Reputation: 12986

appInfo is a newly created intent, so it does not have any extra data associated, so dad will be null and you will get a NullPointerException when trying to call any method of dad.

You can only get data from an intent if it someone added data there with putExtra methods.

I can only guess what you are trying to do, but if you want to get the value of the list that was clicked the correct code would be:

Intent appInfo = new Intent(Show.this, VideoViewActivity.class);
appInfo.putExtra("url", Show.this.lista.get(position));
startActivity(appInfo);

Upvotes: 0

Related Questions