iT邦幫忙

0

請問如何讓List view 顯示兩筆所輸入的資料

各位大家好 小的第一次接觸android studio
希望大家能告訴小的究竟何處出了問題導致執行可以
但listview卻沒有顯示任何東西!

package tw.edu.tut.mis.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    //ArrayAdapter<String> mAdapter;
    ListView mLv;
    ArrayList<String> mlist;
    public  mysql DH = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Intent add =this.getIntent();
        //mlist=add.getStringArrayListExtra("lll");
        mlist =add.getStringArrayListExtra("lll");

        mLv=findViewById(R.id.listview);
        List<HashMap<String , String>> list = new ArrayList<>();




        ListView mLv=(ListView)findViewById(R.id.listview);
        ListAdapter listAdapter = new SimpleAdapter(
                this,
                list,
                android.R.layout.simple_list_item_2 ,
                new String[]{"title" , "text"} ,
                new int[]{android.R.id.text1 , android.R.id.text2});
        mLv.setAdapter(listAdapter);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(it);
        }
    });


}


}

package tw.edu.tut.mis.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
   // ArrayAdapter<String> mAdapter;
    ArrayList<String> mlist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        final List<HashMap<String,String>> list=new ArrayList<>();

        Button bt6;
        bt6 = findViewById(R.id.button6);
        findViewById(R.id.button6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText ed1;
                ed1=findViewById(R.id.editText);
                String s;
                s=ed1.getText().toString();
                EditText ed2;
                ed2=findViewById(R.id.editText2);
                String x ;
                x=ed2.getText().toString();
                String[] title = new String[]{s};
                String[] text = new String[]{x};
               HashMap<String,String> hashMap=new HashMap<>();
               hashMap.put("title",s);
               hashMap.put("text",x);
               list.add(hashMap);


                Intent it = new Intent(Main2Activity.this, MainActivity.class);
                it.putStringArrayListExtra("lll", mlist);
                startActivity(it);

            }

        });

https://ithelp.ithome.com.tw/upload/images/20201019/20131900A1U1GSqweT.jpghttps://ithelp.ithome.com.tw/upload/images/20201019/20131900kFQRKzhBbj.jpghttps://ithelp.ithome.com.tw/upload/images/20201019/20131900VwrNAQaLXD.jpghttps://ithelp.ithome.com.tw/upload/images/20201019/20131900lfYkI00Be1.jpg

Zed_Yang iT邦新手 3 級 ‧ 2020-10-19 17:06:43 檢舉
麻煩將你覺得可能有問題的程式碼
或是整個程式碼
用這個包起來貼上來
```
*程式碼放這*
```
用編輯文章即可

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-10-20 12:51:47

參考這篇看看

我要發表回答

立即登入回答