2016-10-19 5 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> 

私はちょうどボタンをクリックすると、ここで、iはそれを置くことができたときに呼び出される関数を定義する方法を知りたいです。

答えて

0

通常、データを行にバインドするときに、行レイアウトのサブビューでクリックリスナーを設定します。これはアダプタ内部で発生します。フレームワークで提供されているアダプタを使用しているため、行のボタンにクリックリスナーを追加するロジックがなく、単純なバインディングのしかた(たとえば、TextViewなどの文字列)しか知りません。 SimpleAdapter(またはBaseAdapterのようなもの)をサブクラス化し、必要なロジックを自分で追加する必要があります。

ListViewの代わりにRecyclerViewを使用することをお勧めします。これは、現在使用しているものの代わりにRecyclerView.Adapterを使用することを意味します。これは、データバインディングロジックを実装するよう強制します。そのため、行レイアウトのサブビューにクリックリスナーを追加できます。また、ListViewには、行にフォーカス可能なサブビューがある場合(クリック可能な場合のように)、奇妙なやりとりを含む多くの癖や特質があります(Button)。 RecyclerViewはそれに関してあなたにとってはるかに良いでしょう。

+0

RecyclerViewのいくつかのサンプルとそのボタンを表示することができますか?私はRecyclerViewをまだ使用していません。ありがとうございます。 – Siegrain

+0

@Siegrain ListViewとRecyclerViewの間に重要な違いがたくさんあるので、チュートリアルをオンラインで探す必要があると思います。小さなコードスニペットは対象を正義できません。 – Karakuri

+0

RecyclerViewはListViewより簡単ですか?リストビューのボタンについてのチュートリアルが表示されますが、リストビュー内のアイテムがmysqlにあるため、誰も答えがありません。 – Siegrain

0

私はonitemclickのコードを変更しました。リストビューのボタンをクリックすることはできますが、ボタンは1つしか使用できません。

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(); 
     } 
    }); 
} 

ここで間違いを修正してください。ありがとう!

関連する問題