简体   繁体   中英

How to set on itemClickListener in costom listView inside a fragment?

i make a java class names contact that is contain setter and getter for string value ,and make a class names contactAdapter extends ArrayAdapter the problem is all the data i return from JSON displayed but every item in ListView not clickable! I think is the problem in contactAdapter class

my contactAdapter class is :

public class contactadapter extends ArrayAdapter {
List list=new ArrayList();
public contactadapter(Activity activity, int resource) {
    super(activity, resource);

}


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

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

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View matches;
    matches=convertView;
    contactholder contactholder;
    if(matches==null){
        LayoutInflater layoutInflater=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        matches=layoutInflater.inflate(R.layout.matches,null);
        contactholder=new contactholder();
        contactholder.team1=(TextView)matches.findViewById(R.id.tv1);
        contactholder.team2=(TextView)matches.findViewById(R.id.tv2);
        contactholder.date=(TextView)matches.findViewById(R.id.date);
        contactholder.time=(TextView)matches.findViewById(R.id.time);

        matches.setTag(contactholder);

    }
    else{

        contactholder=(contactholder) matches.getTag();
    }
    contact contact=(contact) this.getItem(position);
    contactholder.team1.setText(contact.getTeam1());
    contactholder.team2.setText(contact.getTeam2());
    contactholder.date.setText(contact.getDate());
    contactholder.time.setText(contact.getTime());
    return  matches;
}

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

    TextView team1,team2,date,time;
}

public class t1 extends  Fragment {
contactadapter contactadapter;
View rootView;
ProgressDialog pd;
ListView lv;
Button b;

public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
     rootView = inflater.inflate(R.layout.t1, container,false);
    pd = new ProgressDialog(getActivity());
    pd.setMessage("تجميع المعلومات ...");
    pd.show();

   lv=(ListView)rootView.findViewById(R.id.lv);
   contactadapter=new contactadapter(getActivity(),R.layout.matches);
    lv.setAdapter(contactadapter);
    new readdata1().execute("http://isatstudent10.xyz/getdata.php");

    return rootView;

}
private class readdata1 extends AsyncTask<String,Void,String> {
    String jsonstring;
    @Override
    protected String doInBackground(String... params) {
        URL url = null;
        try {
            url = new URL(params[0]);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            while ((jsonstring = reader.readLine()) != null) {

                stringBuilder.append(jsonstring + "\n");
            }
            reader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String result) {
        int count=0;

        System.out.println(result);
        try {
            JSONObject joo=new JSONObject(result);
            JSONArray array = joo.getJSONArray("server_response");
            while (count < array.length()) {
                JSONObject j = array.getJSONObject(count);
                String team1 = j.getString("team1");
                String team2=j.getString("team2");
                String date=j.getString("date");
                String time=j.getString("time");
                contact contact=new contact(team1,team2,date,time);
                contactadapter.add(contact);
                count++;

            }
            lv.setAdapter(contactadapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    System.out.println(i+"   eejjjjjjjjjjj");
                }
            });
            pd.dismiss();
            super.onPostExecute(result);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

}

Set setOnItemClickListener on your listview in a fragment like this:

yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(getActivity(), "Position: " + position, Toast.LENGTH_LONG).show();
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM