2016-01-21 116 views
-2

我正在開發一個應用程序,我可以選擇「發佈」,當您按下發布按鈕時,您將轉到另一個活動,我們有EditTextButton,一旦按下Button該文本應該在ListView的提要頁面中進行並「張貼」。在列表視圖中沒有顯示

現在,當我按Button一切工作正常,但沒有出現在列表視圖中。

下面的代碼:

帖子:

package com.example.ali.test1; 

import android.app.Activity; 
import android.content.Intent; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.view.View; 
import android.widget.EditText; 

public class Post extends ActionBarActivity { 
     EditText input; 

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

     // android:id="@+id/input" 
     input = (EditText) findViewById(R.id.input); 

    } 


    public void addToList(View v) { 
     Editable text = input.getText(); 
     Intent intent = new Intent(); 
     intent.putExtra("result", text); 
     setResult(Activity.RESULT_OK, intent); 
     finish(); 
    } 

    } 

應該在哪裏可以看到郵件類:

package com.example.ali.test1; 

import android.app.Activity; 
import android.content.Intent; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import java.util.ArrayList; 


public class MainActivity extends ActionBarActivity { 


    ListView list; 
    ArrayAdapter<String> adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     list= (ListView) findViewById(R.id.list); 

     adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<String>()); 
     list.setAdapter(adapter); 

    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 


      if (requestCode == 1) { 
       if (resultCode == Activity.RESULT_OK) { 
        String result = data.getStringExtra("result"); 
        adapter.add(result); 
        adapter.notifyDataSetChanged(); 


       } 
      } 
    } 

    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 

     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     int id2 = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     else if (id2 == R.id.action_cart) { 
      startActivityForResult(new Intent(MainActivity.this , Post.class), 1); 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

的XML文件:

帖子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.example.ali.test1.Post"> 


    <EditText 
     android:id="@+id/input" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Add to list" 
     android:onClick="addToList" 
     android:layout_below="@id/input"/> 


</RelativeLayout> 

主營:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.example.ali.test1.MainActivity"> 


    <ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:layout_weight="1" /> 


</RelativeLayout> 

的問題是,沒有什麼是列表視圖 任何幫助顯示?

+5

請發表您的logcat的錯誤跟蹤。 – Rohit5k2

+1

沒有錯誤是有點難以確定是什麼問題。請編輯併發布對應於崩潰的LogCat錯誤 – diogojme

+0

在哪裏可以找到android studio中的logcat錯誤跟蹤?在logcat窗口中有數百萬行 –

回答

0

嘗試在添加和設置結果之前檢查數據的空值。

if(data != null && data.getStringExtra("result") != null){ 
    String result = data.getStringExtra("result"); 
    adapter.add(result); 
    adapter.notifyDataSetChanged(); 
} 
+0

好吧這工作,並停止崩潰,但沒有在列表視圖中顯示任何幫助?並感謝你。 –

+0

當你通過「adapter.add(result)」直接添加到他的適配器時,它會自動調用notifyDataSetChanged函數,所以嘗試刪除notify函數。如果這不起作用,請嘗試使用list.add(result),然後調用list.getAdapter()。notifyDataSetChanged()。你應該爲你的適配器提供一個函數getView(),我建議你在你的問題中也發佈這個函數 – DadoZolic

+0

他使用的默認適配器已經有了getView()方法。 – Fustigador

0

而不是字符串結果= data.getStringExtra( 「結果」)試試這個方法:

Bundle MBuddle = data.getExtras(); 
String MMessage = MBuddle.getString("result");