簡體   English   中英

如何在單獨的活動中打開自定義 ListView 項目?

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

我盡可能多地瀏覽了其他帖子,雖然這可能是一個簡單的問題,但對於我的生活,我無法弄清楚如何使這項工作發揮作用。 我有一個客戶數據庫,包括他們的姓名、地址等。

我希望能夠從我的自定義 ListView 中選擇一個客戶,並在單獨的活動中查看他們的所有數據庫信息。 目前,我不能讓它給我超過第一張唱片的任何東西。

如果您能看到我做錯了什么,任何建議都會非常有幫助。 我對 Java 還很陌生,所以請放輕松 :P 我現在最好的猜測是我需要創建一個新的游標,但我有點迷茫。

下面附上代碼。

數據庫查看器.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);
}

客戶端查看器

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

    }
}

非常感謝你們的時間和精力。 真的迫不及待想聽到你的回音。

獲取與存儲在 DB 中相同且唯一的客戶端 onClick 的 ID 然后,將其傳遞給另一個活動傳遞 id 參數並根據該 ID 獲取所有數據並顯示..不要通過意圖傳遞所有數據,只需傳遞一個鍵並獲取所有數據並顯示

希望能幫到你

謝謝!!!

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

在 onClick 上使用它並像這樣獲取文本視圖數據。

已解決,很大程度上要歸功於 Anamica!

編碼

String a = Long.toString(l);

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

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

讓我到達我需要去的地方。(顯示項目的 ID)再次感謝各位!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM