2013-01-07 2 views
2

사용자 정의 ListAdapter을 구축했으며 onclick 이벤트 의 각 행에 문제가 있습니다.안드로이드 onclick 사용자 정의 ListView 어댑터의 이벤트가 작동하지 않습니다.

public class MainActivity extends Activity { 

// ArrayList of type "Person". We are making not a list of string, sondern eine list of persons 
ArrayList<Person> arrayOfWebData = new ArrayList<Person>(); 

class Person{ 
    public String person_id; 
    public String name; 
    public String birthday; 
    public String favorite_color; 
    public String profilbild; 
} 

// This is our new Adapter: 
FancyAdapter aa = null; 

// For each row we returned until we use the array to create our person obj 
static ArrayList<String> resultRow; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    new getJson().execute(); 
} 

// Params,Progress,Result 
private class getJson extends AsyncTask<Void,Void,Void> 
{ 
    @Override 
    protected Void doInBackground(Void... arg0) { 
     String result = ""; 
     try{ 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost("xxxxx"); 
      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      InputStream is = httpEntity.getContent(); 
      // Convert response to String: 
      try{ 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 

       while((line = reader.readLine()) != null){ 
        sb.append(line+"\n"); 
       } 
       is.close(); 
       result = sb.toString(); 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     // Parse JSON: 
     try{ 
      JSONArray jArray = new JSONArray(result); 
      for(int i = 0;i < jArray.length();i++) 
      { 
       // Get our object, this is on persons data: 
       JSONObject json_data = jArray.getJSONObject(i); 
       // Create a new person: 
       Person resultRow = new Person(); 
       // Set thats persons attributes: 
       resultRow.person_id = json_data.getString("id"); 
       resultRow.name = json_data.getString("name"); 
       resultRow.favorite_color = json_data.getString("favorite_color"); 
       resultRow.birthday = json_data.getString("birthday"); 
       resultRow.profilbild = json_data.getString("profilbild"); 
       arrayOfWebData.add(resultRow); 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return null;  
    } 
    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     populateListView(); 
    } 
} 


public void populateListView() 
{ 
    final ListView myListView = (ListView)findViewById(R.id.myListView); 
    // initialite FancyAdapter object: 
    aa = new FancyAdapter(); 
    myListView.setAdapter(aa); 

    // Click on ListView Row: 
    myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    { 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
      { 
       Object o = myListView.getItemAtPosition(position); 
       // Here i want have values of the clicked row,- like the name and/or id...,- but i get the following: 
       // Output o.toString(): [email protected] 
       Log.i("TEST", o.toString()); 

       // Here i intend to Start a new Activity passing the "name" of the user (and/or id ...) to the new activity 
      } 
    }); 
} 


class FancyAdapter extends ArrayAdapter<Person> 
{ 
    FancyAdapter(){ 
     super(MainActivity.this,android.R.layout.simple_list_item_1,arrayOfWebData); 
     //imageLoader = new ImageLoader(MainActivity.this); 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 
     if(convertView == null){ 
      LayoutInflater inflater = getLayoutInflater(); 
      convertView = inflater.inflate(R.layout.row3, null); 
      holder = new ViewHolder(convertView); 
      convertView.setTag(holder); 
     }else{ 
      holder = (ViewHolder)convertView.getTag(); 
     } 
     holder.populateFrom(arrayOfWebData.get(position)); 
     return convertView; 
    } 
} 

class ViewHolder 
{ 
    public TextView name = null; 
    public TextView birthday = null; 
    public TextView favorite_color = null; 
    public ImageView profilbild = null; 
    //public ImageLoader imageLoader; 
    ImageLoader imageLoader = new ImageLoader(MainActivity.this); 

    ViewHolder(View row){ 
     name = (TextView)row.findViewById(R.id.name); 
     birthday = (TextView)row.findViewById(R.id.birthday); 
     favorite_color = (TextView)row.findViewById(R.id.favorite_color); 
     profilbild = (ImageView)row.findViewById(R.id.imageView1); 
    } 
    // Notice we have to change our populateFrom() to take an argument of type "Person" 
    void populateFrom(Person r){ 
     name.setText(r.name); 
     birthday.setText(r.birthday); 
     favorite_color.setText(r.favorite_color); 
     imageLoader.DisplayImage(r.profilbild, profilbild); 
    } 
} 

populateListView() 방법에서 참조하시기 바랍니다 : 나중에 다른 활동을 사용하여 에 대한 클릭 한 행의 내용의 일부를 싶어.

// 출력 o.toString() :
com.example.customlistview.MainActivity $ 사람 이 응용 프로그램을 실행할 때 내가 행을 클릭, 내가 Log.i에서 볼 수있는 출력은 다음과 같다 @ 41252310 Log.i ("TEST", o.toString());

하지만 행의 일부 콘텐츠 (예 : 이름 또는 ID)를 갖고 싶습니다.

어떻게하면됩니까?

답변

5

귀하의 AdapterPerson 유형의 개체가 있다고 생각합니다. Person 클래스의 필드는 공개되어 있으므로 다음과 같이 액세스 할 수 있습니다.

@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
{ 
    // Get Person "behind" the clicked item 
    Person p = (Person) myListView.getItemAtPosition(position); 

    // Log the fields to check if we got the info we want 
    Log.i("SomeTag", "Persons name: " + p.name); 
    Log.i("SomeTag", "Persons id : " + p.person_id); 

    // Do something with the data. For example, passing them to a new Activity 

    Intent i = new Intent(MainActivity.this, NewActivity.class); 
    i.putExtra("person_id", p.person_id); 
    i.putExtra("person_name", p.name); 

    MainActivity.this.startActivity(i); 
} 
+0

완벽한 ... 감사합니다, 그것은 작동합니다. –

0
//set Item click listener for list view 

listView_matches.setOnItemClickListener(this); 
//implement method for item click 

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { 

    System.out.println("row Number Clicked "+ position); 
    Intent intent = new Intent(this,Score.class); 
    intent.putExtra("clicked",position); 
    startActivity(intent);  

}` 
관련 문제