简体   繁体   中英

How can I open A Custom ListView Item in A separate Activity?

I have looked through other Posts as much as I can, and though this may be a simple problem, for the life of me I can't figure out how to make this work. I have a Database of Customers, including their names, address, etc.

I would Like to be able to select one of the customers from my custom ListView, and View all of their Database information in A separate Activity. Currently, I cant make it give me anything past the first record.

Any advice would be very helpful if you can see what I am doing wrong. I am fairly new to Java so Please take it easy on me :P My best guess right now is that I need to create a new cursor but I'm a bit lost.

Code attached below.

Database Viewer.java

public class DatabaseViewer extends AppCompatActivity{

TextView DisplayName;
TextView DisplayID;
TextView DisplayMarks;
TextView DisplayAddress;
DatabaseHelper mydb = new DatabaseHelper(this);
ListView customerlist;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_database_viewer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    customerlist = (ListView) findViewById(R.id.ListViewCustomers);

    populateDatabase();
    customerlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
            DisplayID = (TextView)  findViewById(R.id.TVCUSTOMERNAME);
            DisplayMarks = (TextView)  findViewById(R.id.TVADDRESS);
            DisplayName = (TextView)  findViewById(R.id.TVID);
            DisplayAddress = (TextView)  findViewById(R.id.TVMARKS);

            // NEED TO MAKE A CURSOR THAT GETS ALL ROWS NOT COLUMNS LIKE BELOW.
            int c1 = mydb.getallrows().getPosition();
            // 35 ROWS 4 COLUMNS.

            String item = String.valueOf(parent.getItemAtPosition(i));


            Intent clientViewIntent = new Intent(DatabaseViewer.this, ClientViewer.class);

            clientViewIntent.putExtra("Client ID", c1);
            startActivity(clientViewIntent);
        }
    });
}

private void populateDatabase(){
    Cursor c = mydb.getallrows();
    customerlist = (ListView) findViewById(R.id.ListViewCustomers);
    String[] fromfieldnames = new String[] {DatabaseHelper.COL_1, DatabaseHelper.COL_2, DatabaseHelper.COL_3, DatabaseHelper.COL_4};
    int[] tofieldnames = new int[] {R.id.TVCUSTOMERNAME, R.id.TVADDRESS, R.id.TVMARKS, R.id.TVID};
    SimpleCursorAdapter adapter;
    adapter = new SimpleCursorAdapter(getBaseContext(), R.layout.custom_db_viewer_row, c, fromfieldnames, tofieldnames, 0);
    customerlist.setAdapter(adapter);
}

public void OnClientLongPress(){
    // Function to Open up Client Information in a new activity.
    // Step 1, Use data from Client to pull up Full Client Records.
    // Step 2, Send Data in Intent Extras.
    Intent clientVIewIntent = new Intent(DatabaseViewer.this, ClientViewer.class);
    clientVIewIntent.putExtra("Client ID", customerlist.getSelectedItemId());
    startActivity(clientVIewIntent);
}

ClientViewer.java

  public class ClientViewer extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client_viewer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.inflateMenu(R.menu.menu_invoice_creator);

        Bundle NameIntentData = getIntent().getExtras();
        if (NameIntentData==null){
            return;
        }
        int IntentDataID = NameIntentData.getInt("Client ID");
        String IntentDataName = NameIntentData.getString("Client Name");
        String IntentDataAddress = NameIntentData.getString("Client Address");

        final TextView IDBar = (TextView) findViewById(R.id.ClientViewerIDTV);
        final TextView Namebar = (TextView) findViewById(R.id.ClientViewerNameTV);
        final TextView AddressBar = (TextView) findViewById(R.id.ClientViewerAddressTV);

        Namebar.setText(Integer.toString(IntentDataID));
        IDBar.setText(IntentDataName);
        AddressBar.setText(IntentDataAddress);

    }
}

Thanks so much for your time and effort guys. Really cant wait to hear back from you.

Get the ID of client onClick which is same and unique as stored in DB Then, pass it to another activity Pass id parameter and get all the data based on that ID and display..don't pass all the data via intent just pass one key and get all data and display

Hope this will help you

Thanks!!!

TextView textview =((TextView)view.findViewById(R.id.tvInVisitorName)).getText().toString();

在 onClick 上使用它并像这样获取文本视图数据。

SOLVED, Largely in thanks to Anamica!

The code

String a = Long.toString(l);

Intent clientViewIntent = new Intent(DatabaseViewer.this, 
ClientViewer.class);

clientViewIntent.putExtra("Client ID", a);
startActivity(clientViewIntent);

Got me where I needed to be.(Shows the Id of Item) Thanks again Guys!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM