2017-10-11 78 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