2017-02-18 2 views
-2

Ich habe die Liste der Schüler aus der Datenbank, Feld angezeigt, die Student_Roll_no, Student_Name und Student_ID angezeigt werden. Student_Roll_no und Student_Name werden mit TextView angezeigt, und Student_ID ID wird mit Kontrollkästchen angezeigt. Jetzt möchte ich den Code, mit dem, wenn ich auf die Liste klicken das Kontrollkästchen dieser bestimmten Zeile sollte überprüft werden, aber die Listview reagiert nicht auf OnItemClickListener Funktion, wie dort ich habe einen Toast, der nicht erscheint, wenn ich Klicken Sie auf die Liste.So legen Sie OnItemClickListener in ListView mit CustomAdapter fest

Sry für die schlechte Englisch

activity_attendence.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_attendence" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.tejas.paras.bpibs.Attendence" 
android:orientation="vertical"> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/roll" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:paddingBottom="2dip" 
     android:paddingTop="6dip" 
     android:text="Roll" 
     android:layout_weight=".1" 
     android:textColor="#000000" /> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:paddingBottom="2dip" 
     android:text="Name" 
     android:layout_weight=".6" 
     android:textColor="#000000" /> 

    <TextView 
     android:text="Present" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView26" 
     android:layout_weight=".2" 
     android:textColor="#000000" /> 

</LinearLayout> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:background="#000000" 
     android:layout_height="1dp" 
     android:layout_marginBottom="5dp" 
     android:layout_marginTop="5dp"></LinearLayout> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="375dp" 
     android:id="@+id/listview" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     android:layout_weight="1.10" /> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:text="Save Attendence" 
      android:layout_width="match_parent" 
      android:background="@drawable/buttonlayout2" 
      android:textColor="#FFFFFF" 
      android:layout_height="wrap_content" 
      android:id="@+id/button5" /> 
    </LinearLayout> 
</LinearLayout> 
</LinearLayout> 

Attendence.java

package com.tejas.paras.bpibs; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Toast; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.util.ArrayList; 
import java.util.HashMap; 

public class Attendence extends AppCompatActivity{ 

String date, course, year,ID; 
String myJSON; 

private static final String TAG_RESULTS = "result"; 

String link; 
JSONArray peoples = null; 
BufferedReader bufferedReader; 
String result,data; 
ArrayList<HashMap<String, String>> personList; 
Button b5; 
ListView list,listView; 
private DataModel dataModel; 
private ArrayList<DataModel> dataModels; 
private static CustomAdapter adapter; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_attendence); 
    list=(ListView)findViewById(R.id.listview); 
    b5=(Button)findViewById(R.id.button5); 

    getData(); 

    b5.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(Attendence.this,"1."+view, Toast.LENGTH_SHORT).show(); 
     } 
    }); 


    list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> a, View v, int position, 
           long id) { 
      dataModel = dataModels.get(position); 
      Toast.makeText(Attendence.this,"2.1",Toast.LENGTH_LONG).show(); 
      ID=dataModel.getID(); 
       Toast.makeText(Attendence.this,dataModel.getRoll(),Toast.LENGTH_LONG).show(); 

     } 
    }); 
} 


protected void showList() { 
    dataModels= new ArrayList<>(); 

    try { 

     JSONObject jsonObj = new JSONObject(myJSON); 
     peoples = jsonObj.getJSONArray(TAG_RESULTS); 

     for (int i = 0; i < peoples.length(); i++) { 
      JSONObject c = peoples.getJSONObject(i); 

      dataModels.add(new DataModel(c.getString("name"),c.getString("id"), c.getString("roll"))); 
     } 
     adapter = new CustomAdapter(dataModels, getApplicationContext()); 
     list.setAdapter(adapter); 

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


public void getData() { 
    class GetDataJSON extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... params) { 

      try { 

       link = "http://painnation.esy.es/attendence.php?date=18022017&course=MCA&year=3"; 
       URL url = new URL(link); 
       HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
       bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); 


       StringBuilder sb = new StringBuilder(); 

       String line = null; 
       while ((line = bufferedReader.readLine()) != null) 
       { 
        sb.append(line + "\n"); 
       } 
       result = sb.toString(); 
      } catch (Exception e) { 
       // Oops 
      } 
      return result; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      myJSON = result; 
      showList(); 
     } 
    } 
    GetDataJSON g = new GetDataJSON(); 
    g.execute(); 
} 
} 

item_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="10dp" 
android:paddingLeft="10dp" 
android:paddingRight="10dp" > 


<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight=".1"> 

    <TextView 
     android:id="@+id/roll" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:paddingBottom="2dip" 
     android:paddingTop="6dip" 
     android:textStyle="bold" 
     android:text="Roll" 
     android:layout_weight=".1" 
     android:textColor="#000000" /> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:paddingBottom="2dip" 
     android:textStyle="bold" 
     android:text="Name" 
     android:layout_weight=".6" 
     android:textColor="#000000" /> 

    <CheckBox 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/id" 
     android:layout_weight=".15" 
     android:textColor="#000000" /> 

</LinearLayout> 
</LinearLayout> 

CustomAdapter.java

package com.tejas.paras.bpibs; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CheckBox; 
import android.widget.TextView; 

import java.util.ArrayList; 


public class CustomAdapter extends ArrayAdapter<DataModel> implements View.OnClickListener{ 

private ArrayList<DataModel> dataSet; 
Context mContext; 

private static class ViewHolder { 
    TextView name; 
    TextView roll; 
    CheckBox id; 
} 

public CustomAdapter(ArrayList<DataModel> data, Context context) { 
    super(context, R.layout.item_layout, data); 
    this.dataSet = data; 
    this.mContext=context; 

} 

@Override 
public void onClick(View v) { 

    int position=(Integer) v.getTag(); 
    Object object= getItem(position); 
    DataModel dataModel=(DataModel)object; 

} 

private int lastPosition = -1; 

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

    DataModel dataModel = getItem(position); 

    ViewHolder viewHolder; 

    final View result; 

    if (convertView == null) { 

     viewHolder = new ViewHolder(); 
     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     convertView = inflater.inflate(R.layout.item_layout, parent, false); 
     viewHolder.name = (TextView) convertView.findViewById(R.id.name); 
     viewHolder.roll = (TextView) convertView.findViewById(R.id.roll); 
     viewHolder.id=(CheckBox) convertView.findViewById(R.id.id); 


     result=convertView; 

     convertView.setTag(viewHolder); 
    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
     result=convertView; 
    } 
    lastPosition = position; 
    viewHolder.name.setText(dataModel.getName()); 
    viewHolder.roll.setText(dataModel.getRoll()); 
    viewHolder.id.setText(dataModel.getID()); 

    return convertView; 
} 
} 

DataModel.java

package com.tejas.paras.bpibs; 

public class DataModel { 

String roll; 
String name; 
String id; 

public DataModel(String name1, String id1, String roll1) { 
    this.name=name1; 
    this.id=id1; 
    this.roll=roll1; 
} 

public String getName() { 
    return name; 
} 

public String getID() { 
    return id; 
} 

public String getRoll() { 
    return roll; 
} 
} 

Attendence.php Datei und die Datenbank auf einem kostenlosen Hosting-Webserver (Hostinger) gehostet wird. Sie können den DB-Ausgang der vom Server http://painnation.esy.es/attendence.php?date=18022017&course=MCA&year=3

Output Screen

Daten mit Check wird in json holen format.plz den Code zu kommentieren, um das Kontrollkästchen tun überprüfen, indem Sie Listenansicht klicken.

Danke

Antwort

0

Ich glaube, Sie android:focusable="false" in item_layout.xml für Checkbox setzen sollte, dann onItemClickListener zu reagieren. für Checkbox müssen Sie Variable in Datenmodell setzen, Android es wahr oder falsch und Änderung setzen in Listview adapter.notifyDataSetChanged

<CheckBox 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/id" 
     android:layout_weight=".15" 
     android:focusable="false" 
     android:textColor="#000000" /> 
+0

Danke, OnItemClickListener arbeitet jetzt machen, wie kann ich den Zustand der Kontrollkästchen ändern, indem Sie die Listenansicht klicken? –

Verwandte Themen