2016-10-19 52 views
0

ListView中的我的项目是使用PHP生成的,并存储在MySQL数据库中。我如何添加绑定到按钮的事件处理函数?我需要的过程是这样的:当你点击按钮时,它将获得该项目并将其保存到其他表格,以及获取当前日期时间。如何在Android的Listview中将事件处理程序添加到按钮中?

public class ViewBusFiveStudent extends AppCompatActivity implements ListView.OnItemClickListener { 

private ListView listView; 
private Button btntime; 
private String JSON_STRING; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_view_busfour); 
    listView = (ListView) findViewById(R.id.listView); 
    listView.setOnItemClickListener(this); 

    getJSON(); 
} 


private void showStudents(){ 
    JSONObject jsonObject = null; 
    ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>(); 
    try { 
     jsonObject = new JSONObject(JSON_STRING); 
     JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY); 

     for(int i = 0; i<result.length(); i++){ 
      JSONObject jo = result.getJSONObject(i); 
      String id = jo.getString(Config.TAG_ID); 
      String name = jo.getString(Config.TAG_NAME); 

      HashMap<String,String> student = new HashMap<>(); 
      student.put(Config.TAG_ID,id); 
      student.put(Config.TAG_NAME,name); 
      list.add(student); 
     } 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    ListAdapter adapter = new SimpleAdapter(
      ViewBusFiveStudent.this, list, R.layout.list_items, 
      new String[]{Config.TAG_ID,Config.TAG_NAME}, 
      new int[]{R.id.id, R.id.name}); 

    listView.setAdapter(adapter); 
} 

private void getJSON(){ 
    class GetJSON extends AsyncTask<Void,Void,String>{ 

     ProgressDialog loading; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      loading = ProgressDialog.show(ViewBusFiveStudent.this,"Fetching Data","Wait...",false,false); 
     } 

     @Override 
     protected void onPostExecute(String s) { 
      super.onPostExecute(s); 
      loading.dismiss(); 
      JSON_STRING = s; 
      showStudents(); 
     } 

     @Override 
     protected String doInBackground(Void... params) { 
      RequestHandler rh = new RequestHandler(); 
      String s = rh.sendGetRequest(Config.URL_GET_BUS_FOUR); 
      return s; 
     } 
    } 
    GetJSON gj = new GetJSON(); 
    gj.execute(); 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    Intent intent = new Intent(this, ViewBusFive.class); 
    HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position); 
    String studId = map.get(Config.TAG_ID).toString(); 
    intent.putExtra(Config.STUD_ID,studId); 
    startActivity(intent); 
} 
} 

这里是我的list_view:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

<TextView 
    android:id="@+id/id" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<TextView 
    android:id="@+id/name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/id"/> 

    <CheckBox 
     android:id="@+id/checkBoxPres" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:text="" 
     android:layout_alignBaseline="@+id/btnTime" 
     android:layout_alignBottom="@+id/btnTime" 
     android:layout_toLeftOf="@+id/btnTime" 
     android:layout_toStartOf="@+id/btnTime" /> 

    <Button 
     android:text="TimeStamp" 
     android:layout_width="105dp" 
     android:layout_alignParentRight="true" 
     android:layout_height="wrap_content" 
     android:id="@+id/btnTime" /> 

</RelativeLayout> 

我只是想知道如何定义按钮被点击时调用的功能,我在哪里可以把它。

回答

0

通常,当您将数据绑定到行时,您可以在行布局的子视图上设置点击侦听器。这发生在适配器内部。由于您使用的是框架提供的适配器,因此它没有逻辑将点击侦听器添加到行中的按钮,它只知道如何进行简单的绑定(例如,将字符串转换为TextView等)。您将不得不子类SimpleAdapter(或别的东西,如BaseAdapter),并自己添加必要的逻辑。

当你在这里,我建议你切换到使用RecyclerView而不是ListView。这意味着使用RecyclerView.Adapter而不是现在使用的那个,这将迫使您无论如何实施数据绑定逻辑,因此您可以将点击侦听器添加到行布局的子视图。此外,ListView还有很多怪癖和特质,包括奇怪的交互,当行有可聚焦的子视图时(如可点击Button)。 RecyclerView在这方面对你来说会好很多。

+0

你能告诉我一些RecyclerView的样本和一个funcgtioning按钮吗?我还没有使用RecyclerView。谢谢 – Siegrain

+0

@Siegrain我认为你应该在网上寻找一个教程,因为ListView和RecyclerView之间有很多重要的区别,一个小的代码片段不会做主题正义。 – Karakuri

+0

RecyclerView比ListView更容易吗?我在列表视图中看到关于按钮的教程,但没有人有答案,因为我在listview中的项目是在mysql中。 – Siegrain

0

我已经改变了onitemclick的代码,我可以点击列表视图中的按钮,但只有一个按钮正在工作/可点击。

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    final TextView name1 = (TextView) findViewById(R.id.name); 
    btnTime = (Button) findViewById(R.id.btnTime); 
    btnTime.setTag(position); 
    btnTime.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      BusArrived(); 
     } 

     public void BusArrived() { 

      final String name = name1.getText().toString().trim(); 

      class SendStudent extends AsyncTask<Void, Void, String> { 

       ProgressDialog loading; 

       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        loading = ProgressDialog.show(ViewBusFiveStudent.this, "Saving Dismissal Time...", "Wait...", false, false); 
       } 

       @Override 
       protected void onPostExecute(String s) { 
        super.onPostExecute(s); 
        loading.dismiss(); 
        Toast.makeText(ViewBusFiveStudent.this, s, Toast.LENGTH_LONG).show(); 
       } 

       @Override 
       protected String doInBackground(Void... v) { 
        HashMap<String, String> params = new HashMap<>(); 
        params.put(Config.KEY_STUD_NAME, name); 

        RequestHandler rh = new RequestHandler(); 
        String res = rh.sendPostRequest(Config.URL_SAVING_TO_BUS_FIVE_TIME, params); 
        return res; 
       } 
      } 

      SendStudent ae = new SendStudent(); 
      ae.execute(); 
     } 
    }); 
} 

请在这里纠正我的错误。谢谢!

相关问题