按照我的 privious qustion,你可以在这里看到:
如何在 2 个元素之间为 android 创建自定义列表视图?
由于我在这里得到的帮助,我取得了一些进展。所以我现在的状态是:
http://i.stack.imgur.com/yK5to.jpg
按下其中一项任务的蓝色部分会弹出以下色板活动:
http://i.stack.imgur.com/UWmIg.jpg
这里所有的 5 种颜色都是按钮,我需要做的是检查按下了什么颜色,然后更改打开色板活动的任务的背景图。
只是我不明白它是如何完成的,有什么建议吗?在色板活动中,每个按钮都有一个“setOnClickListener”功能。
这是我的列表活动的 java 代码:
public class SkyGiraffeAppActivity extends Activity implements AdapterView.OnItemClickListener, View.OnClickListener {
// All static variables
static final String TAG = "SkyGiraffeAppActivity";
// XML node keys
static final String KEY_TASK = "task"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_TIME = "time";
static final String KEY_PRIORITY = "priority";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list = new ListView(this);
list.setDivider(null);
list.setDividerHeight(0);
setContentView(list);
final ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
Log.e(TAG, "Created Array");
XMLParser parser = new XMLParser();
Log.e(TAG, "Getting xml from raw folder");
InputStream xml = getResources().openRawResource(R.raw.taskslists); // getting XML
Log.e(TAG, "reading xml file to string");
String sxml = parser.readTextFile(xml);
Log.e(TAG, "creating dom element from string");
Document doc = parser.getDomElement(sxml); // getting DOM element
Log.e(TAG, "getting node list of all the elements");
NodeList nodeList = doc.getElementsByTagName(KEY_TASK);
Log.e(TAG, "looping through all the elements");
for (int i = 0; i < nodeList.getLength(); i++)
{
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nodeList.item(i);
Log.e(TAG, "element number: " + i + " added to hash map");
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_TIME, parser.getValue(e, KEY_TIME));
map.put(KEY_PRIORITY, parser.getValue(e, KEY_PRIORITY));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.row,
new String[] { KEY_TITLE, KEY_DATE, KEY_TIME, KEY_PRIORITY},
new int[] { R.id.text_title, R.id.text_date, R.id.text_time ,R.id.image_priority}){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
View left = row.findViewById(R.id.bchangecolor);
left.setTag(position);
left.setOnClickListener(SkyGiraffeAppActivity.this);
View right = row.findViewById(R.id.barrow);
right.setTag(position);
right.setOnClickListener(SkyGiraffeAppActivity.this);
ImageView prioriy = (ImageView) row.findViewById(R.id.image_priority);
Log.e(TAG, "pririty number: " + menuItems.get(position).get(KEY_PRIORITY));
int number = Integer.valueOf(menuItems.get(position).get(KEY_PRIORITY));
switch ( number )
{
case 1:
prioriy.setImageResource(R.drawable.priority_first);
break;
case 2:
prioriy.setImageResource(R.drawable.priority_second);
break;
case 3:
prioriy.setImageResource(R.drawable.priority_third);
break;
case 4:
prioriy.setImageResource(R.drawable.priority_fourth);
break;
case 5:
prioriy.setImageResource(R.drawable.priority_fifth);
break;
default:
prioriy.setImageResource(R.drawable.priority_first);
}
return row;
}
};
list.setAdapter(adapter);
list.setOnItemClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.bchangecolor:
Log.e(TAG, "pressed the color change button on" + v.getId());
startActivity(new Intent(getApplicationContext(),TaskColorChangeActivity.class));
break;
case R.id.barrow:
Log.e(TAG, "pressed the arrow button on" + v.getId());
Intent intent = createIntentwithExtra(v);
startActivity(intent);
break;
default:
break;
}
}
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.e(TAG, "pressed the row itself" + v.getId());
Intent intent = createIntentwithExtra(v);
startActivity(intent);
}
public Intent createIntentwithExtra(View v)
{
Intent intent = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
String title = ((TextView) v.findViewById(R.id.text_title)).getText().toString();
String date = ((TextView) v.findViewById(R.id.text_date)).getText().toString();
String time = ((TextView) v.findViewById(R.id.text_time)).getText().toString();
intent.putExtra(KEY_TITLE, title);
intent.putExtra(KEY_DATE, date);
intent.putExtra(KEY_TIME, time);
return intent;
}}
和我的色板活动:
public class TaskColorChangeActivity extends Activity
{
static final String TAG = "TaskColorChangeActivity";
Button bBlue, bGreen, bYellow, bOrange, bRed;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.colorplate);
bBlue = (Button) findViewById(R.id.bblue);
bGreen = (Button) findViewById(R.id.bgreen);
bYellow = (Button) findViewById(R.id.byellow);
bOrange = (Button) findViewById(R.id.borange);
bRed = (Button) findViewById(R.id.bred);
bBlue.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
bGreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
bYellow.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
bOrange.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
bRed.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}}
如果有权限的人将链接更改为实际图像,那就太好了。谢谢。