Android 更改CustomListView中已单击按钮的颜色
我正在开发android应用程序,我想在点击CustomListView后更改文本颜色,但我想保存点击位置,并在活动开始时更改CustomListView中点击按钮的颜色。我试着做下面的代码,但它不工作Android 更改CustomListView中已单击按钮的颜色,android,Android,我正在开发android应用程序,我想在点击CustomListView后更改文本颜色,但我想保存点击位置,并在活动开始时更改CustomListView中点击按钮的颜色。我试着做下面的代码,但它不工作 package com.androidbegin.jsonparsetutorial; import java.util.ArrayList; import java.util.HashMap; import android.content.Context; import android.c
package com.androidbegin.jsonparsetutorial;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.anket1.R;
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
private final Integer[] imageId;
ImageView image;
ArrayList<Integer> yourSelectedImageArray = new ArrayList<Integer>();
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist, Integer[] imageId) {
this.imageId = imageId;
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView rank;
TextView country;
TextView population;
ImageView flag;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
rank = (TextView) itemView.findViewById(R.id.rank);
country = (TextView) itemView.findViewById(R.id.country);
population = (TextView) itemView.findViewById(R.id.population);
// Locate the ImageView in listview_item.xml
flag = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set results to the TextViews
image = (ImageView) convertView.findViewById(R.id.flag);
rank.setText(resultp.get(MainActivity.RANK));
country.setText(resultp.get(MainActivity.COUNTRY));
// population.setText(resultp.get(MainActivity.POPULATION));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(MainActivity.FLAG), flag);
if (yourSelectedImageArray.contains(position)) {
country.setTextColor(Color.GREEN);
}
// Capture ListView item click
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// Get the position
resultp = data.get(position);
yourSelectedImageArray.add(position);
Intent intent = new Intent(context, SingleItemView.class);
// Pass all data rank
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.putExtra("rank", resultp.get(MainActivity.RANK));
// Pass all data country
intent.putExtra("country", resultp.get(MainActivity.COUNTRY));
// Pass all data population
intent.putExtra("population",
resultp.get(MainActivity.POPULATION));
// Pass all data flag
intent.putExtra("flag", resultp.get(MainActivity.FLAG));
intent.putExtra("message", resultp.get(MainActivity.MESAJ));
// Start SingleItemView Class
context.startActivity(intent);
}
});
return itemView;
}
}
package com.androidbegin.jsonparsetutorial;
导入java.util.ArrayList;
导入java.util.HashMap;
导入android.content.Context;
导入android.content.Intent;
导入android.graphics.Color;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
导入com.example.anket1.R;
公共类ListViewAdapter扩展了BaseAdapter{
//声明变量
语境;
充气机;
阵列列表数据;
图像加载器;
HashMap resultp=新的HashMap();
私有最终整数[]imageId;
图像视图图像;
ArrayList yourSelectedImageArray=新建ArrayList();
公共ListViewAdapter(上下文,
ArrayList ArrayList,整数[]图像ID){
this.imageId=imageId;
this.context=上下文;
数据=数组列表;
imageLoader=新的imageLoader(上下文);
}
@凌驾
public int getCount(){
返回data.size();
}
@凌驾
公共对象getItem(int位置){
返回null;
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
//声明变量
文本视图等级;
TextView国家/地区;
文本视图人口;
图像视图标志;
充气器=(充气器)上下文
.getSystemService(上下文布局\充气机\服务);
视图项视图=充气机。充气(R.layout.listview\u项,父项,false);
//获得职位
resultp=data.get(位置);
//在listview_item.xml中找到文本视图
rank=(TextView)itemView.findViewById(R.id.rank);
country=(TextView)itemView.findViewById(R.id.country);
population=(TextView)itemView.findViewById(R.id.population);
//在listview_item.xml中找到ImageView
flag=(ImageView)itemView.findViewById(R.id.flag);
//捕获位置并将结果设置为文本视图
image=(ImageView)convertView.findViewById(R.id.flag);
rank.setText(resultp.get(MainActivity.rank));
country.setText(resultp.get(MainActivity.country));
//setText(resultp.get(MainActivity.population));
//捕获位置并将结果设置为ImageView
//将标志图像URL传递到ImageLoader.class
imageLoader.DisplayImage(resultp.get(MainActivity.FLAG),FLAG);
如果(您选择的EdImageArray.contains(位置)){
country.setTextColor(Color.GREEN);
}
//捕获列表视图项单击
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//获得职位
resultp=data.get(位置);
您可以选择edimagerarray.add(位置);
意向意向=新意向(上下文,SingleItemView.class);
//通过所有数据等级
intent.addFlags(intent.FLAG\u活动\u无\u动画);
intent.putExtra(“rank”,resultp.get(MainActivity.rank));
//通过所有国家/地区的数据
intent.putExtra(“国家”,resultp.get(MainActivity.country));
//通过所有数据填充
意向.额外(“人口”,
resultp.get(MainActivity.POPULATION));
//传递所有数据标志
intent.putExtra(“标志”,resultp.get(MainActivity.flag));
intent.putExtra(“message”,resultp.get(MainActivity.MESAJ));
//启动SingleItemView类
背景。开始触觉(意图);
}
});
返回项目视图;
}
}
您不能做什么?保存项目位置或更改按钮的颜色?在onClick Listener中保存项目位置,我想在StartActivity中更改TextView的颜色要更改TextView的颜色,您只需在onClick方法中更改它,在阅读代码时,您似乎将项目的位置放在了您的意图之外。你是说在开始的活动中你看不到额外的内容吗?