2015-01-23 5 views
0

목록보기에서 특정 조건을 기반으로 단추의 가시성을 설정하려고합니다.프로그래밍 방식으로 목록보기 내에서 단추의 표시 여부를 설정할 수 없습니다.

컨텍스트 : listview에는 게시물에 대한 응답을위한 매개 변수가 있습니다. 여기에는 응답의 제목, 설명 등이 포함되어 있습니다. 상위 게시물의 소유자 인 사용자 만이 버튼을 볼 수있어 응답에 투표 할 수 있습니다.

나 버튼의 표시를 설정하려고하고있는 코드의 자바 부 :

adapter= new SimpleAdapter(MainActivity.this, list, 
        R.layout.response_list, columns, mapping); //response_list is the xml layout file where response parameters are defined. 
ListView listView = (ListView) findViewById(R.id.listallresponses); //listallresponses is the id of response_list layout file. 

if (!parent.equals(userLoggedin)) { //"parent" is the userid of the parent post. "userLoggedin" is the current user who is viewing the parent post and its responses. 
    LayoutInflater li = LayoutInflater.from(this); 
    View v = li.inflate(R.layout.response_list, null, false); 
    Button upVoteButton = (Button) v 
         .findViewById(R.id.upvoteButton); //upvoteButton is the one whose visibility we are talking about. 
    upVoteButton.setVisibility(View.GONE); 
} 

listView.setAdapter(adapter); 

나 응답 매개 변수를 정의 오전 response_list.xml 이하이다

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:id="@+id/responseList" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="6dip" > 
<!-- Other views are present here--> 
<Button 
    android:id="@+id/upvoteButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="upVoteResponse" 
    android:text="VoteUp"/> 

문제 : upvoteButton은 로그인 한 사용자가 부모 게시물의 소유자와 같지 않더라도 항상 응답 목록에 표시됩니다. 내가 어떻게 작동하게 할 수 있는지 알고 싶다! 미리 감사드립니다.

참고 : 안드로이드에 대한 나의 친숙 함은 겨우 5 개월입니다. 이 작업을 수행하는 방법을 알아 내기 위해 꽤 많이 검색했지만 지금까지는 성공하지 못했습니다.

+0

'getView'내부에서 어댑터 자체에서 동일한 작업을 수행합니다. –

+0

보기 재활용을 처리 했습니까? 그렇게 보이지 않는다! – Skynet

+0

@Skynet : 당신은 adapter.notifyDataSetChanged()를 의미 했습니까? ? 나는 그것을 시도했다. – maya

답변

0
LayoutInflater li = LayoutInflater.from(this); 
    View v = li.inflate(R.layout.response_list, null, false); 
    Button upVoteButton = (Button) v.findViewById(R.id.upvoteButton); 
    upVoteButton.setVisibility(View.GONE); 

뷰 라인이 & 3 여기에 언급 된 "V"& "upVoteButton는"언제 어디 볼 ​​수 없습니다. 뷰 "v"및 "upVoteButton"이 비정상적으로 증가했지만 사용자 인터페이스에서는 증가하지 않았습니다.

코드에서 참조하는보기 및 단추는 실제로 참조하려는 모든 것이 아닙니다.

if 블록 내부의보기를 확장하지 말고 getView() 메소드의 매개 변수 인보기 및 위치를 사용하여 목표를 달성해야합니다.

+0

좋습니다. 나는 getView() 메소드에 대해 약간의 독서를 할 것이다. 지금까지 사용하지 않았다. – maya

+0

simpleadapter를 사용자 정의하고 getView() 메소드를 대체하여 구현되었습니다. 올바른 설명을 제공 했으므로이 대답을 받아들입니다. – maya

관련 문제