如何在单独的活动中打开自定义ListView项?

时间:2017-09-27 02:58:30

标签: java listview android-studio android-sqlite

我尽可能多地查看其他帖子,虽然这可能是一个简单的问题,但对于我的生活,我无法弄清楚如何使这项工作。我有一个客户数据库,包括他们的姓名,地址等。

我希望能够从我的自定义ListView中选择一个客户,并在单独的Activity中查看他们的所有数据库信息。目前,我无法通过第一张唱片给我任何东西。

如果你能看到我做错了什么,任何建议都会非常有用。 我对Java很新,所以请放轻松对待我:P我现在最好的猜测是我需要创建一个新游标,但我有点迷失。

下面的代码。

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

    }
}

非常感谢您的时间和精力。真的无法等待你的回复。

3 个答案:

答案 0 :(得分:0)

获取客户端onClick的ID,该ID与存储在DB中的相同且唯一 然后,将其传递给另一个活动 传递id参数并根据该ID获取所有数据并显示..然后通过意图传递所有数据只需传递一个键并获取所有数据并显示

希望这会对你有所帮助

感谢!!!

答案 1 :(得分:0)

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

在onClick上使用此功能并获取此类文本视图数据。

答案 2 :(得分:0)

已经解决了,非常感谢Anamica!

代码

data=[('key4', {'site': 'Site4', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 05:58:08.451829', 'date_accessed': '2017-09-27 05:58:08.451832'}), ('key3', {'site': 'Site3', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 05:58:08.451522', 'date_accessed': '2017-09-27 05:58:08.451527'}), ('key2', {'site': 'Site2', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 05:58:08.451294', 'date_accessed': '2017-09-27 05:58:08.451297'}), ('key1', {'site': 'Site1', 'machine': 'null', 'state': 'unprocessed', 'date_populated': '2017-09-27 05:58:08.451139', 'date_accessed': '2017-09-27 05:58:08.451145'})]

for tuple_a in data:
    for dic_a in tuple_a:
        if isinstance(dic_a,dict):
            for keys in dic_a.keys():
                if dic_a[keys]=='unprocessed':
                    dic_a[keys]="processed"



            print(dic_a)

让我到达我需要的地方。(显示项目的ID)再次感谢大家!