我有两个活动MainActivity.java和Second.java。我希望当用户点击图片视图时,其背景图片会发生变化,当用户点击列表中的任何项目时,将会打开第二个Activity
。
这是我的代码,它不起作用.....
rowlaout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip" >
<ImageView
android:id="@+id/button"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:src="@drawable/ok" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="30sp" >
</TextView>
</LinearLayout>
dataListAdapter.java
class dataListAdapter extends BaseAdapter {
String[] Title;
Context context;
ImageView i1;
TextView t1;
public dataListAdapter(Context cont,String[] text) {
Title = text;
context=cont;
}
public int getCount() {
// TODO Auto-generated method stub
return Title.length;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
View row;
row = inflater.inflate(R.layout.rowlayout, parent, false);
i1=(ImageView)row.findViewById(R.id.button);
t1=(TextView) row.findViewById(R.id.label);
t1.setText(Title[position]);
i1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
i1.setImageResource(R.drawable.no);
}
});
return (row);
}
}
MainActivity.java
public class MainActivity extends Activity {
Intent intent;
ListView list;
int [] state;
int po;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.list);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
dataListAdapter adapter = new dataListAdapter(this, values);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
String itemValue= (String) list.getItemAtPosition(position);
Bundle sendBundle = new Bundle();
sendBundle.putString("value", itemValue);
intent=new Intent(MainActivity.this,Second.class);
intent.putExtras(sendBundle);
startActivity(intent);
po=position;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:2)
首先,你应该改变ImageView i1的可见性,即它不应该在getView方法中可见。
Context context;
// ImageView i1; // remove this declaration from here
TextView t1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
View row;
row = inflater.inflate(R.layout.rowlayout, parent, false);
final ImageView i1=(ImageView)row.findViewById(R.id.button);
t1=(TextView) row.findViewById(R.id.label);
t1.setText(Title[position]);
i1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
i1.setImageResource(R.drawable.no);
// OR
// ((ImageView)view).setImageResource(R.drawable.no);
}
});
return (row);
}
之后尝试使用onClick()方法中的处理程序设置imageResource
public void onClick(View v) {
// TODO Auto-generated method stub
new Handler().post(new Runnable() {
@Override
public void run() {
i1.setImageResource(R.drawable.no);
}
});
}
这可以解决您的问题。