我有一个列表视图行列表视图:
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical" >
<TextView
android:id="@+id/personName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="4dip"
android:text="TEST Arora"
android:textColor="@color/BLACK"
android:textSize="@dimen/activity_big_bold_text" />
<TextView
android:id="@+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="4dip"
android:text="200m away"
android:textColor="@color/GREY"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center" >
<TextView
android:id="@+id/tv_confirmHelp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Confirm Help"
android:textColor="@color/PURPLE"
android:textSize="@dimen/activity_solo_text" />
<ImageButton
android:id="@+id/report_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/report_icon"
android:layout_marginLeft="4dip"
android:visibility="visible" />
</LinearLayout>
<ImageButton
android:id="@+id/check_location"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:src="@drawable/notification_location" />
我的观点是:
@SuppressWarnings("null")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.notification_rowlayout, parent,false);
holder.tv_name = (TextView)convertView.findViewById(R.id.personName);
holder.btn_checkLocation = (ImageButton)convertView.findViewById(R.id.check_location);
holder.tv_ConfirmHelp = (TextView)convertView.findViewById(R.id.tv_confirmHelp);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.btn_checkLocation.setTag(position);
holder.tv_ConfirmHelp.setTag(position);
holder.tv_ConfirmHelp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer) holder.tv_ConfirmHelp.getTag();
System.out.println("Position Selectd"+pos );
if(holder.tv_ConfirmHelp.getText().toString().equalsIgnoreCase("confirm help")){
//TakeCareUtilities.startProgressBar(context, "Confirming. Please wait..", true, null);
JsonProcessor jsonProcessor = JsonProcessor.getInstance();
APIVariables apiVariables = new APIVariables();
jsonProcessor.makeRequest(apiVariables.confirmNotification(context, "",dataList.get(pos).getPhonenum() ), context, callback,null,"GET_NOTIFICATIONS");
Toast.makeText(context, "Alert Confirmed", 0).show();
holder.tv_ConfirmHelp.setText("Report User");
}else if(holder.tv_ConfirmHelp.getText().toString().equalsIgnoreCase("Report User")){
//Toast.makeText(context, "Already Reported", 0).show();
JsonProcessor jsonProcessor = JsonProcessor.getInstance();
APIVariables apiVariables = new APIVariables();
//jsonProcessor.makeRequest(apiVariables.confirmNotification(context, "",dataList.get(position).getPhonenum() ), context, callback,null,"GET_NOTIFICATIONS");
jsonProcessor.makeRequest(apiVariables.reportaUser(context,dataList.get(pos).getPhonenum() ), context, callback,null,"GET_NOTIFICATIONS");
holder.tv_ConfirmHelp.setText("Reported");
}else{
Toast.makeText(context, "Already Reported", 0).show();
/*JsonProcessor jsonProcessor = JsonProcessor.getInstance();
APIVariables apiVariables = new APIVariables();
//jsonProcessor.makeRequest(apiVariables.confirmNotification(context, "",dataList.get(position).getPhonenum() ), context, callback,null,"GET_NOTIFICATIONS");
jsonProcessor.makeRequest(apiVariables.reportaUser(context,dataList.get(position).getPhonenum() ), context, callback,null,"GET_NOTIFICATIONS");
*/
holder.tv_ConfirmHelp.setText("Reported");
}
}
});
holder.tv_name.setText(dataList.get(position).getName());
/*if(dataList.get(position).getStatus().equalsIgnoreCase("confirmed")||dataList.get(position).getStatus().equalsIgnoreCase("")){
holder.tv_ConfirmHelp.setText("Confirmed");
}else{
holder.tv_ConfirmHelp.setText("Confirm Help");
}*/
holder.btn_checkLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,TrackUserLocation.class);
intent.putExtra("LATITUDE", dataList.get(position).getLatitude());
intent.putExtra("LONGITUDE", dataList.get(position).getLongitude());
intent.putExtra("PERSON_NAME", dataList.get(position).getName());
context.startActivity(intent);
}
});
这里我遇到一个问题,当我点击ROW 1文本视图时,它没有更新,而是屏幕上的最后一个文本视图被更新。
我厌倦了发现自己的错误,弄明白我的错位。任何人都可以请帮助
由于
答案 0 :(得分:1)
使用v.getTag()
向右转position
:
@Override
public void onClick(View v) {
TextView txtConfirmHelp=((TextView)v);
int pos = (Integer) txtConfirmHelp.getTag();
System.out.println("Position Selectd"+pos );
//...now use `txtConfirmHelp` for setting TextView text
...
}
现在在txtConfirmHelp
方法中使用holder.tv_ConfirmHelp
代替onClick
来更改tv_confirmHelp
TextView
答案 1 :(得分:0)
在onClick而不是
int pos = (Integer) holder.tv_ConfirmHelp.getTag();
使用
ViewHolder currentHolder = (ViewHolder) v.getTag();
int pos = (Integer) currentHolder.tv_ConfirmHelp.getTag();