1
我有一個listView,用戶可以在其中輸入輸入來更改每個listView行上的textView。當您將該行從屏幕上滾動時,編輯後的文本將消失,並重新顯示默認文本。我知道這是用viewHolder修復的,但我無法讓它在我的自定義適配器類中工作。如何使用viewHolder使listView用戶輸入不會回滾到默認時滾動屏幕
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> Chores = new ArrayList<>();
//Chores.add("");
final ListAdapter MyAdapter = new CustomAdapter(this, Chores);
ListView listViewObject = (ListView)findViewById(R.id.customListView_ID);
listViewObject.setAdapter(MyAdapter);
listViewObject.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String ChoreString = String.valueOf(parent.getItemAtPosition(position));
}
}
);
final Button button = (Button) findViewById(R.id.button_ID);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Chores.add("");
((ArrayAdapter)MyAdapter).notifyDataSetChanged();
}
});
}
}
CustomAdapter.java
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import static com.example.emilythacker.chorelist.R.id.textView_ID;
class CustomAdapter extends ArrayAdapter{
ArrayList<String> choreText;
public CustomAdapter(Context context, ArrayList choreText) {
super(context, R.layout.custon_listview_row, choreText);
this.choreText = choreText;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
//final TextView textView = (TextView) customView.findViewById(textView_ID);
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
convertView = layoutInflater.inflate(R.layout.custon_listview_row, null);
holder = new ViewHolder();
holder.imageButton = (ImageButton) convertView.findViewById(R.id.imageButton_ID);
holder.textView = (TextView) convertView.findViewById(textView_ID);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(choreText.get(position));
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//what happens when textView is clicked
AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
final EditText input = new EditText(getContext());
input.setHint("hint");
alertDialog.setView(input);
alertDialog.setTitle("Set Chore");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
holder.textView.setText(input.getText().toString());
dialog.dismiss();
}
});
alertDialog.show();
}
});
holder.imageButton.setImageResource(R.drawable.clock);
return convertView;
}
static class ViewHolder {
ImageButton imageButton;
TextView textView;
}
}
activity_main.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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.emilythacker.chorelist.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/customListView_ID"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp" />
<Button
android:text="Add Chore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/button_ID" />
</RelativeLayout>
custon_listview_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="60dp">
<ImageButton
android:layout_width="60dp"
android:scaleType="fitCenter"
app:srcCompat="@drawable/clock"
android:id="@+id/imageButton_ID"
android:layout_height="60dp"
android:background="@null"
android:layout_alignParentRight="true"
android:padding="5dp"
android:layout_weight="1" />
<TextView
android:text="Click here to add chore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="@+id/textView_ID"
android:textSize="25dp"
android:layout_centerVertical="true"
android:layout_toStartOf="@+id/imageButton_ID"
android:layout_marginEnd="11dp"
/>
</RelativeLayout>
我不明白我沒怎麼做?這個viewHolder方法對我來說非常混亂。 –
@RyderThacker所以不要使用視圖持有者模式,因爲在90%的情況下它幾乎是無用的 - 它只會讓更多麻煩(事實上沒有一個標準谷歌的'ListView'適配器使用該模式) – pskink
@RyderThacker不,你不是。你將它設置爲文本視圖,但不是將它保存在變量中,並使用它在getView中重置它記住,用戶滾動項目開/關屏幕時,任何基於視圖的變量都會丟失 - 將其保存在視圖不足 –