自定义列表视图项目消失滚动或单击编辑文本时。
自定义adater值绑定到列表
的主类package com.example.customlistview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
ArrayList<String> nameList;
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView)findViewById(R.id.listView1);
nameList = new ArrayList<String>();
nameList.add("Iphone");
nameList.add("Android");
nameList.add("Samsung");
nameList.add("BlackBery");
nameList.add("Lava");
nameList.add("T Mobile");
nameList.add("Lg Mobile");
CustomAdapter adapter = new CustomAdapter(this,nameList);
list.setAdapter(adapter);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
自定义适配器类。此类创建视图以在列表上进行绑定
package com.example.customlistview;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<String> {
TextView name;
TextView add;
TextView state;
public Context context;
ArrayList<String> List;
View rowView;
String str1,str2,str3;
public CustomAdapter(Context context, ArrayList<String> nameList) {
super(context, 0,nameList);
List = nameList;
this.context = context;
}
@SuppressWarnings("null")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
str1 = List.get(position);
str2 = List.get(position);
str3 = List.get(position);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if( convertView==null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.customlayout, parent, false);
TextView name = (TextView) rowView.findViewById(R.id.nameT);
TextView add= (TextView) rowView.findViewById(R.id.addT);
TextView state = (TextView) rowView.findViewById(R.id.stateT);
if(name != null) {
try {
name.setVisibility(View.VISIBLE);
name.setText(str1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
name.setVisibility(View.INVISIBLE);
}
if(add != null) {
add.setVisibility(View.VISIBLE);
add.setText(str2);;
}else {
add.setVisibility(View.INVISIBLE);
}
if(state != null) {
state.setVisibility(View.VISIBLE);
state.setText(str3);;
}else {
state.setVisibility(View.INVISIBLE);
}
}
return rowView;
}
}
Main Xml是
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10" >
<requestFocus />
</EditText>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:layout_weight="1.67"
android:cacheColorHint="#00000000" >
</ListView>
</LinearLayout>
</RelativeLayout>
Custom Xml是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp" >
<TextView
android:id="@+id/nameT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="@+id/addT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/nameT"
android:layout_marginLeft="60dp"
android:layout_toRightOf="@+id/nameT"
android:text="TextView" />
<TextView
android:id="@+id/stateT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="38dp"
android:text="TextView" />
</LinearLayout>
点击编辑文字之前---&gt; First Image。点击编辑文字后---&gt; Second Image。
最后当虚拟键盘只在列表中显示一个项目时
答案 0 :(得分:1)
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("position"+position);
View rowView=convertView;
if( rowView==null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row, parent, false);
}
TextView name = (TextView) rowView.findViewById(R.id.nameT);
TextView add= (TextView) rowView.findViewById(R.id.addT);
TextView state = (TextView) rowView.findViewById(R.id.stateT);
if(name != null&&add != null&&state != null) {
str1 = List.get(position);
str2 = List.get(position);
str3 = List.get(position);
// name.setVisibility(View.VISIBLE);
name.setText(str1);
// add.setVisibility(View.VISIBLE);
add.setText(str2);
// state.setVisibility(View.VISIBLE);
state.setText(str3);
}else {
name.setVisibility(View.INVISIBLE);
add.setVisibility(View.INVISIBLE);
state.setVisibility(View.INVISIBLE);
}
System.out.println("pos"+position);
return rowView;
}
答案 1 :(得分:0)
就我而言,关键部分是:
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row, parent, false);
}
//...
}