2017-07-27 15 views
0

私は文字列値のsetterとgetterを含むjavaクラス名の連絡先を作成し、クラス名を作成しますcontactAdapter extends ArrayAdapter問題はJSONから返されたすべてのデータですがListViewの各項目はクリックできません!フラグメント内のcostView listViewでitemClickListenerを設定するには?

このようなフラグメントでは、あなたのリストビューに
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(); 
     } 
    } 
} 

}

+0

あなたframgmentのコードを投稿しますまた – AndroidGeek

答えて

0

設定setOnItemClickListener:私はcontactAdapterクラスの問題

私contactAdapterクラスがあると思う

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

私はそれを作るがリストビューはまだクリック可能ではない –

+0

とgetApplicationContext()は表示されず、ただgetContext() getActivity() –

+0

getActivity()も使用できます – AndroidGeek

関連する問題