簡體   English   中英

返回editText值到listView

[英]Return editText value into listView

活動A中 ,它具有多行listView 活動B返回的值應填充活動A listView

活動A

 MyCustomBaseAdapter objMyCustomBaseAdapter;
 ArrayList<SearchResults> results=new ArrayList<SearchResults>();

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.work_details);

        listview = (ListView) findViewById(R.id.listView);
        name = getIntent().getExtras().getString("Name"); // receive name from Information
        weather = getIntent().getExtras().getString("Weather"); //receive weather
        date = getIntent().getExtras().getString("date2"); //receive date
        status = getIntent().getExtras().getString("Status"); // receive status
        objMyCustomBaseAdapter=new MyCustomBaseAdapter(getApplicationContext(),results);

    }

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.addDetails:
                View menuItemView = findViewById(R.id.addDetails);
                PopupMenu po = new PopupMenu(this, menuItemView); //for drop-down menu
                po.getMenuInflater().inflate(R.menu.popup_details, po.getMenu());
                po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                if ("Add Work Details".equals(item.getTitle())) {
                Intent intent = new Intent(getApplication(), B.class);  // go to B class  
                startActivityForResult(intent, PROJECT_REQUEST_CODE);
                 }
                        return true;
                    }
                });
                po.show(); //showing popup menu
        }
        return super.onOptionsItemSelected(item);

    }

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B
        if (requestCode == PROJECT_REQUEST_CODE) {
            ReceiveProject = data.getStringExtra("Project");
            ReceiveDescription = data.getStringExtra("Description");
            ReceiveProgress = data.getIntExtra("progress", 0);
            ReceiveTimeIn = data.getStringExtra("TimeIn");
            ReceiveTimeOut = data.getStringExtra("TimeOut");
            MyCustomBaseAdapter objMyCustomBaseAdapter= (MyCustomBaseAdapter)listview.getAdapter();
            objMyCustomBaseAdapter.addNewItem(ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);

        }
    }

活動B

  save.setOnClickListener(new View.OnClickListener()
        {  // return values to Activity A
            @Override
            public void onClick(View v)
            {
                Intent returnIntent=new Intent();
                Project=project.getSelectedItem().toString();
                Description=description.getText().toString();
                progress=seekBar.getProgress();
                returnIntent.putExtra("Project",Project);
                returnIntent.putExtra("Description", Description);
                returnIntent.putExtra("progress", progress);
                Toast.makeText(getApplicationContext(), progress+"", Toast.LENGTH_LONG).show();
                returnIntent.putExtra("TimeIn", TimeIn);
                returnIntent.putExtra("TimeOut",TimeOut);
                setResult(Activity.RESULT_OK,returnIntent);
                finish();

            }
        });

MyCustomBaseAdapter.java

    public class MyCustomBaseAdapter extends BaseAdapter{   // for ListView in Activity A

     private static ArrayList<SearchResults> searchArrayList;
     private LayoutInflater mInflater;

public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
                searchArrayList = results;
                mInflater = LayoutInflater.from(context);
            }

            public int getCount() {
                return searchArrayList.size();
            }

           public void addNewItem(String P,String D,int Per,String I,String O)
           {
               SearchResults obj=new SearchResults();
               obj.setProject(P);
               obj.setDescription(D);
               obj.setProgress(Per);
               obj.setTimeIn(I);
               obj.setTimeOut(O);
               searchArrayList.add(obj);
               this. notifyDataSetChanged();
           }

custom_row_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/ListProject"
        android:textSize="15sp"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/ListDescription"
        android:layout_width="wrap_content"
        android:textSize="15sp"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/ListProgress"
        android:layout_width="wrap_content"
        android:textSize="15sp"
        android:layout_height="wrap_content"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:id="@+id/ListTimeIn"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:id="@+id/ListTimeOut"/>

</LinearLayout>

當我在活動B中單擊保存按鈕以將值返回給A時,應用程序崩潰了。

  Process: com.example.project.myapplication, PID: 23777
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.project.myapplication/com.example.project.myapplication.GUI.A}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.project.myapplication.Adapter.MyCustomBaseAdapter.addNewItem(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)' on a null object reference
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
            at android.app.ActivityThread.-wrap16(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.project.myapplication.Adapter.MyCustomBaseAdapter.addNewItem(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)' on a null object reference
            at com.example.project.myapplication.GUI.A.onActivityResult(A.java:95)

代碼引用錯誤

 objMyCustomBaseAdapter.addNewItem(ReceiveProject, ReceiveDescription, ReceiveProgress, ReceiveTimeIn, ReceiveTimeOut);

在開始活動B之前,嘗試將適配器設置為listview,因為listView沒有適配器,所以它將在getAdapter中返回null

編輯

在此代碼后添加設置適配器,以在您調用listview.getAdapter()時使用適配器,否則,適配器將為null

listview = (ListView) findViewById(R.id.listView);
        name = getIntent().getExtras().getString("Name"); // receive name from Information
        weather = getIntent().getExtras().getString("Weather"); //receive weather
        date = getIntent().getExtras().getString("date2"); //receive date
        status = getIntent().getExtras().getString("Status"); // receive status
        objMyCustomBaseAdapter=new MyCustomBaseAdapter(getApplicationContext(),results);
listview.setAdapter(objMyCustomBaseAdapter);

暫無
暫無

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

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