import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class First_Activity extends Activity
{
ListView listView;
ArrayList<Item> arrayList= new ArrayList<Item>();
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.first_actiivty);
listView =(ListView) findViewById(R.id.listView);
Item item = new Item();
item.setText1("1");
item.setText2("2");
item.setSelected(false);
arrayList.add(item);
Item item1 = new Item();
item1.setText1("1");
item1.setText2("2");
item1.setSelected(true);
arrayList.add(item1);
Item item2 = new Item();
item2.setText1("1");
item2.setText2("2");
item2.setSelected(true);
arrayList.add(item2);
adapter = new Adapter();
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
for (Item item :arrayList){
item.setSelected(false);
}
arrayList.get(position).setSelected(true);
System.out.println(arrayList.get(0).isSelected());
System.out.println(arrayList.get(1).isSelected());
System.out.println(arrayList.get(2).isSelected());
adapter.notifyDataSetChanged();
}
});
}
public class Adapter extends BaseAdapter
{
public Adapter()
{
}
@Override
public int getCount()
{
return arrayList.size();
}
@Override
public Object getItem(int position)
{
return arrayList.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row, parent, false);
holder.text1 = (TextView) convertView.findViewById(R.id.textView1);
holder.text2 = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(arrayList.get(position).getText1());
holder.text2.setText(arrayList.get(position).getText2());
if(arrayList.get(position).isSelected()==true)
{
holder.text2.setVisibility(View.VISIBLE);
}
else
{
holder.text2.setVisibility(View.GONE);
}
return convertView;
}
class ViewHolder
{
TextView text1;
TextView text2;
}
}
}
javabeen get set
public class Item
{
private String text1;
private String text2;
private boolean isSelected;
public String getText1() {
return text1;
}
public void setText1(String text1) {
this.text1 = text1;
}
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
}
主要的XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
行的 XML 文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
android:textSize="30dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>