2017-10-11 81 views
0

我建立了一個列表視圖,顯示動態按鈕與數據庫中表的名稱。當一個人點擊按鈕時,應該抓住按鈕的文本,然後將其傳遞給下一個活動,該活動將填充與數據庫表相對應的信息並在屏幕頂​​部顯示該文本。當我點擊按鈕時,我寫的代碼不斷崩潰。還有什麼我需要打電話或者這個代碼不能用按鈕工作嗎?Android的listview onclicklistener與動態按鈕

public class UserArea extends AppCompatActivity { 

SectionListAdapter sectionListAdapter; 
ListView listView; 

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

TextView tvWelcomeMsg = (TextView) findViewById(R.id.tvWelcome); 

/**Get Sections and Display as buttons*/ 
listView = (ListView) findViewById(R.id.lvSections); 
sectionListAdapter = new SectionListAdapter(this, R.layout.section_layout); 
listView.setAdapter(sectionListAdapter); 

Response.Listener<String> responseListener = new Response.Listener<String>() { 
    @Override 
    public void onResponse(String response) { 
     try { 

      JSONObject jsonResponse = new JSONObject(response); 
      boolean success = jsonResponse.getBoolean("success"); 
      /** If data successfully gathered*/ 
      if (success) { 

       JSONArray jsonArray= jsonResponse.getJSONArray("Flights"); 

       int count = 0; 

       String flight; 

       while(count<jsonArray.length()) { 

        JSONObject SL = jsonArray.getJSONObject(count); 
        flight = SL.getString("Flight"); 

        SectionList sl = new SectionList(flight); 
        sectionListAdapter.add(sl); 

        count++; 

       } 

      } 
      /** If data is not gathered*/ 
      else { 
       AlertDialog.Builder builder = new AlertDialog.Builder(UserArea.this); 
       builder.setMessage("Failed to connect") 
         .setNegativeButton("Retry", null) 
         .create() 
         .show(); 
      } 
     } 
     /** if any other response is received*/ 
     catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
}; 

/**Creates Request to get the data*/ 
GetSectionRequest getSections = new GetSectionRequest(responseListener); 
/**Creates a queue to run the code*/ 
RequestQueue queue = Volley.newRequestQueue(UserArea.this); 
queue.add(getSections); 

/**End*/ 

/**Creates onclicklistener to pass clicked section name*/ 

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int i, long id) { 

     Intent intent = new Intent (UserArea.this, Personnel.class); 
     intent.putExtra("section", listView.getItemAtPosition(i).toString()); 
     UserArea.this.startActivity(intent); 
    } 
}); 

SectionListAdapter

public class SectionListAdapter extends ArrayAdapter { 

    List list = new ArrayList(); 

    public SectionListAdapter(Context context, int resource) { 
     super(context, resource); 
    } 


    public void add(SectionList object) { 
     super.add(object); 
     list.add(object); 
    } 

    @Override 
    public int getCount() { 
     return list.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return list.get(position); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View row; 
     row = convertView; 

     SectionListAdapter.SectionListHolder sectionListHolder; 

     if (row == null){ 
      LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = layoutInflater.inflate(R.layout.section_layout, parent, false); 
      sectionListHolder = new SectionListAdapter.SectionListHolder(); 
      sectionListHolder.bSection = row.findViewById(R.id.bSectionName); 

     }else{ 

      sectionListHolder = (SectionListAdapter.SectionListHolder)row.getTag(); 

     } 
     SectionList SectionList = (SectionList) this.getItem(position); 
     sectionListHolder.bSection.setText(SectionList.getFlight()); 



     return row; 
    } 

    static class SectionListHolder{ 

     Button bSection; 
    } 
} 

登錄貓

10-10 19:31:26.797 6595-6595/com.example.yikes.recall E/AndroidRuntime: 
FATAL EXCEPTION: main 
Process: com.example.yikes.recall, PID: 6595 
java.lang.NullPointerException: Attempt to read from field 'android.widget.Button com.example.yikes.recall.SectionListAdapter$SectionListHolder.bSection' on a null object reference 
+1

可以請您發佈更多的代碼或錯誤日誌? – Jerrol

+0

這將回答你的問題** java.lang.NullPointerException:嘗試從字段'android.widget.Button com.example.yikes.recall.SectionListAdapter $ SectionListHolder.bSection'讀空對象引用**可能的佈局您的** bsection **在您的佈局中找不到,請嘗試仔細檢查您的xml佈局。 – Jerrol

+0

@Jerrol bSection在SectionListAdapter中按鈕應該被存儲,也許我建立了錯誤的... – Kirakos

回答

0

我嘗試代碼:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     final ListView listView = new ListView(this); 
     listView.setBackgroundColor(Color.WHITE); 
     setContentView(listView); 

     final String[] activities = new String[]{"Item1", "Item2", "Item3", "Item4"}; 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line); 
     listView.setAdapter(adapter); 

     for (String item : activities) { 
      adapter.add(item); 
     } 


     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       String item = listView.getItemAtPosition(position).toString(); 
       Intent intent = new Intent(MainActivity.this, SampleActivity.class); 
       intent.putExtra("item", item); 
       MainActivity.this.startActivity(intent); 
      } 
     }); 
    } 

它的工作,我覺得

/**Creates Request to get the data*/ 
GetSectionRequest getSections = new GetSectionRequest(responseListener); 
/**Creates a queue to run the code*/ 
RequestQueue queue = Volley.newRequestQueue(UserArea.this); 
queue.add(getSections); 

需要一些時間,您可以在啓動應用程序時添加ProgressDialog,並在響應回調時解除。希望它能幫助你!

+0

我已經添加了我用來填充ListView的代碼。如果有幫助,我可以添加適配器。 – Kirakos

+0

下班後我會試試這個,還需要把這個放到response.listener中嗎?我從一個在線sql數據庫中提取表格。 – Kirakos

+0

當你從sql獲取數據時,連接需要時間連接和查詢。請嘗試並反饋給我。 – Dungnbhut