2016-06-26 4 views
0

내 안드로이드 프로젝트에서 모든 값을 서버에서 검색하여 listview에 표시하는 사용자 지정 목록보기를 사용하고 있습니다. 나는 listview 항목에있는 textview와 함께 버튼을 가지고 있습니다. 버튼을 클릭하면 같은 그룹의 textview에서 값을 가져 와서 경고 상자에 표시하는 것과 같은 버튼 기능을 원합니다. 데이터를 검색하고 성공적으로 표시하지만 목록의 textview 값을 얻기 위해 버튼을 사용하는 방법을 알아낼 수 없습니다. 사전 도움을 주셔서 감사합니다 ... 내 코드는 아래에 게시됩니다. 사용자 지정 목록보기의 단추 사용

내 연락처 어댑터 클래스입니다 :

package com.example.sohan.doctor; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by Sohan on 6/9/2016. 
*/ 
public class ContactAdapter extends ArrayAdapter { 
    notification nt = new notification(); 
    List list = new ArrayList(); 
    View row; 
    ContactHolder contactHolder; 
    public ContactAdapter(Context context, int resource) { 
     super(context, resource); 
    } 


    public void add(List<Contacts> updatedList) { 
     list.clear(); 
     list.addAll(updatedList); 
     notifyDataSetChanged(); 
    } 


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

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

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     row = convertView; 

     if(row==null){ 
      LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = layoutInflater.inflate(R.layout.view_symptom_layout,parent,false); 
      contactHolder = new ContactHolder(); 
      contactHolder.Name =(TextView) row.findViewById(R.id.textView2); 
      contactHolder.Age =(TextView) row.findViewById(R.id.textView3); 
      contactHolder.Height =(TextView) row.findViewById(R.id.textView4); 
      contactHolder.Weight =(TextView) row.findViewById(R.id.textView5); 
      contactHolder.Symptom =(TextView) row.findViewById(R.id.textView6); 
      contactHolder.button = (Button) row.findViewById(R.id.button3); 
      contactHolder.button.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        String name; 
        name = nt.newList.get(position).getName(); // This is where I'm stucked. 
        Toast.makeText(getContext().getApplicationContext(), name+" is served ", Toast.LENGTH_LONG).show(); 
       } 
      }); 

      row.setTag(contactHolder); 
     } 
     else{ 

      contactHolder = (ContactHolder)row.getTag(); 
     } 



     Contacts contacts = (Contacts)this.getItem(position); 
     contactHolder.Name.setText("Name: "+contacts.getName()); 
     contactHolder.Age.setText("Age: "+contacts.getAge()); 
     contactHolder.Height.setText("Height: "+contacts.getHeight()); 
     contactHolder.Weight.setText("Weight: "+contacts.getWeight()); 
     contactHolder.Symptom.setText("Symptoms: "+contacts.getSymptom()); 
     return row; 



    } 

    static class ContactHolder{ 
     TextView Name; 
     TextView Age; 
     TextView Height; 
     TextView Weight; 
     TextView Symptom; 
     Button button; 
    } 

} 

내가 서버에서 모든 값을 검색하여 목록보기에 저장 곳은 클래스입니다 :

package com.example.sohan.doctor; 

import android.app.Fragment; 
import android.content.Context; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Adapter; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.Spinner; 
import android.widget.TextView; 

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

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by Sohan on 6/25/2016. 
*/ 
public class notification extends Fragment implements AdapterView.OnItemSelectedListener{ 

    public final static String Message = "Sohan"; 
    View myView; 
    String selectedCity; 
    Context myContext; 
    String jsonResult; 
    JSONObject jsonObject; 
    JSONArray jsonArray; 
    String JSON_String; 
    ContactAdapter contactAdapter; 
    ListView listView; 
    List<Contacts> newList; 
    Button button; 
    String send; 
    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     myView = inflater.inflate(R.layout.notification, container, false); 
     myContext = inflater.getContext(); 
     contactAdapter = new ContactAdapter(myContext, R.layout.view_symptom_layout); 
     listView = (ListView)myView.findViewById(R.id.listView); 
     listView.setAdapter(contactAdapter); 
     retrieveInfo ri = new retrieveInfo(); 
     ri.execute(); 
     return myView; 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 

    } 


    class retrieveInfo extends AsyncTask<Void, Void, String> {    // send data to server 

     String myUrl; 


     protected void onPreExecute() { 
      myUrl ="httP://myserver.com"; // change php script 
     } 


     protected String doInBackground(Void... args) { 
      String city; 
      String result = null; 
      JSONArray jsonArray = null; 
      try{ 
       URL url = new URL(myUrl); 
       HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 
       OutputStream outputStream = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 
       // String data_to_send = URLEncoder.encode("city", "UTF-8")+"="+URLEncoder.encode(city,"UTF-8"); 
       //bufferedWriter.write(data_to_send); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       outputStream.close(); 
       InputStream is = httpURLConnection.getInputStream(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); 
       StringBuilder sb = new StringBuilder(); 


       while ((JSON_String = reader.readLine()) != null) 
       { 
        sb.append(JSON_String+"\n"); 
       } 
       reader.close(); 
       httpURLConnection.disconnect(); 
       is.close(); 
       return sb.toString().trim(); 
      }catch(MalformedURLException e){ 
       e.printStackTrace(); 
      }catch(IOException f){ 
       f.printStackTrace(); 
      } 
      return null; 
     } 


     protected void onPostExecute(String result) { 

      jsonResult = result; 
      parseJSON(jsonResult); 
     } 
    } 


    public void parseJSON(String json){ 
     Contacts contacts=null; 
     try { 
      jsonObject = new JSONObject(json); 
      jsonArray = jsonObject.getJSONArray("patient"); 
      int count = 0; 
      String name,age,height,weight,symptom; 
      newList = new ArrayList<Contacts>(); 
      while (count < jsonArray.length()) { 
       JSONObject jo = jsonArray.getJSONObject(count); 
       name = jo.getString("Name");       // data's are send to store in and print in listview 
       age = jo.getString("Age"); 
       height = jo.getString("Height"); 
       weight = jo.getString("Weight"); 
       symptom = jo.getString("Symptom"); 
       contacts = new Contacts(name,age,height,weight,symptom); 

       newList.add(contacts);         // data are stored in the newlist array 
       count++; 
      } 

      contactAdapter.add(newList);        // the newlist array are send to add in the listview 

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

} 
+0

토스트가 무엇인지를 표시 "" null로 초기화로 루프 동안 넣어해야합니까? –

+0

일부 blaberish 참조를 표시 토스트 .. –

답변

0

다음과 같이 몇 가지 변경 사항을 추가 할 수 있습니다.

ContactAdapter extends ArrayAdapter<Contacts> 
List<Contacts> list = new ArrayList<Contacts>(); 

그리고이 라인 :

String name,age,height,weight,symptom; 

당신은

+0

당신은 당신이 원래의 답변을 편집 할 수 있습니다. 왼쪽 아래에 링크가 있습니다. –

+0

Thnx 많이 작용했습니다. :-) –

+1

효과가 있지만 처음 3 행에서 값을 검색 할 수 있습니다. 4 행에서 처음 3 행의 값을 다시 보여 주므로 목록의 길이와 관련하여 문제가 있습니까? ?? –