JamMan9
JamMan9

Reputation: 776

Clicking a list view item and passing a database item ID to the next activity?

I have an activity which contains a list view which is loaded from an SQLiteDatabase. What I want to happen is, when I click one of the items, I want a second activity to load and display the Item ID. This is what I have so far:

setOnItemClickListener:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getBaseContext(),Detail.class);
            intent.putExtra("DB_ID_EXTRA",IWantThisToBeMyID);
            startActivity(intent);
        }
    }

populating IWantThisToBeMyID:

public static String IWantThisToBeMyID = "com.example.projectloc.assignment.pictures._id";

Detail.class

public class Detail extends AppCompatActivity{
String passedVar = null;
private TextView passedView = null;

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

    passedVar=getIntent().getStringExtra(Master.dbIDEx);

    passedView=(TextView)findViewById(R.id.passedText);
    passedView.setText(passedVar);
}

When I click one of the list view items it loads the new activity but displays nothing.

Upvotes: 1

Views: 60

Answers (3)

Goku
Goku

Reputation: 9692

check out when you add data in intent you are adding DB_ID_EXTRA key name and in your Detail activity your are using Master.dbIDEx different key

You need to use same key name when passing data through intent

Use this

passedVar = getIntent().getStringExtra("DB_ID_EXTRA");

Instead of

passedVar = getIntent().getStringExtra(Master.dbIDEx);

Upvotes: 2

Hemant Parmar
Hemant Parmar

Reputation: 3976

You are pass the key is DB_ID_EXTRA and getting value from intent with wrong key. just change this

public class Detail extends AppCompatActivity{
String passedVar = null;
private TextView passedView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail);    
    passedVar=getIntent().getStringExtra("DB_ID_EXTRA");
    passedView=(TextView)findViewById(R.id.passedText);
    passedView.setText(passedVar);
 }

Upvotes: 1

Ramesh sambu
Ramesh sambu

Reputation: 3539

Try this

passedVar=getIntent().getStringExtra("DB_ID_EXTRA");

Instead

passedVar=getIntent().getStringExtra(Master.dbIDEx);

Upvotes: 2

Related Questions