我正在使用自定義ListView與自定義ArrayAdapter爲我的自定義對象。我使用來自xml文件的已分析數據填充我的ListView。我能夠用這些數據填充ListView,但我想切換到不同的活動取決於哪個項目被點擊。我可以與OnItemClick自定義ListView上的問題
INT位置
該項目被點擊展示,但我怎麼能找回例如點擊的按鈕的名字嗎?
我的代碼:
/**
* Setup
* */
@SuppressWarnings("unchecked")
private void setup() throws IOException {
device_array = new ArrayList<Device>();
//-----------------ACTIONBAR------------------------------------
//activates the default ActionBar
ActionBar actionBar = getActionBar();
actionBar.show();
// set the app icon as an action to go home
actionBar.setDisplayHomeAsUpEnabled(true);
//-------------------INTENT--------------------------------------
//get data from Intent
Intent intent = getIntent();
Name = intent.getStringExtra("Projectname");
IP = intent.getStringExtra("RouterIP");
URL = intent.getStringExtra("URL");
Port = intent.getStringExtra("Port");
//-------------------TEXTVIEWS+LISTVIEW---------------------------------------
nameproject = (TextView)findViewById(R.id.nameproject);
nameproject.setText("Projektname: "+Name);
routerip = (TextView)findViewById(R.id.routerip);
routerip.setText("KNX/IP-Router-Adresse: "+IP);
port = (TextView)findViewById(R.id.port);
port.setText(":"+Port);
url = (TextView)findViewById(R.id.url);
url.setText("URL: "+URL);
//Fill ListView
display_listview();
devices = (ListView)findViewById(R.id.deviceList);
adapter = new DeviceAdapter(this,
R.layout.listviewitem_device, device_array);
//add a view which is shown if the ListView is empty
devices.setEmptyView(findViewById(R.id.empty_list_view));
//Click on item in listview
devices.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(ProjectView.this, OnOff.class);
i.putExtra("Uniqid","From_ProjectView_Activity");
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
});
devices.setAdapter(adapter);
//-------------------- READ ARRAYLIST-------------------------------
}
/**
* Insert Controls
* */
@SuppressWarnings("unchecked")
private void display_listview() {
//------------------ READ ARRAYLIST--------------------------------
//read ArrayList which is saved local
//change path to non-root folder
String filePath = context.getFilesDir().getPath().toString()+"/"+Name;
Log.d("FilestreamfromFolder", "Filepath:"+filePath+ " Projektname:"+Name);
File f = new File(filePath);
try
{
FileInputStream fileIn = new FileInputStream(f);
ObjectInputStream in = new ObjectInputStream(fileIn);
XML = (ArrayList<Datapoint>) in.readObject();
Log.d("XML",XML.toString());
in.close();
fileIn.close();
} catch(IOException ioe)
{
ioe.printStackTrace();
return;
} catch(ClassNotFoundException c)
{
Log.d("FILESTREAM", ".Datapoint class not found.");
c.printStackTrace();
return;
}
//------------------ FILL DEVICE ARRAY FROM XML--------------------------------
for (int i=0; i<XML.size(); i++) {
if (XML.get(i).getDptID().contains(ValueTemp)) {
Datapoint d = XML.get(i);
device_array.add(new Device(d.getName().toString(), R.drawable.temperature_5));
Log.d("Temperature", d.getName().toString()+" added");
}
}
for (int i=0; i<XML.size(); i++) {
if (XML.get(i).getDptID().contains(ValueLux)) {
Datapoint d = XML.get(i);
device_array.add(new Device(d.getName().toString(), R.drawable.brightness));
Log.d("Brightness", d.getName().toString()+" added");
}
}
for (int i=0; i<XML.size(); i++) {
if (XML.get(i).getDptID().contains(PercentScaling)) {
Datapoint d = XML.get(i);
device_array.add(new Device(d.getName().toString(), R.drawable.lamp));
Log.d("Dimmer", d.getName().toString()+" added");
}
}
for (int i=0; i<XML.size(); i++) {
//check XML file for datapoint 1.001 which demontrates a on/off switch
if (XML.get(i).getDptID().contains(OnOff)) {
Datapoint d = XML.get(i);
device_array.add(new Device(d.getName().toString(), R.drawable.lamp_switch));
Log.d("OnOFF", d.getName().toString()+" added");
}
}
}
我的數據點類:
package XMLParser;
import java.io.Serializable;
public class Datapoint implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4917183601569112207L;
private String stateBased;
private String name;
private String priority;
private String mainNumber;
private String groupadress;
private String dptID;
public Datapoint(){
}
public String getMainNumber() {
return mainNumber;
}
public void setMainNumber(String mainNumber) {
this.mainNumber = mainNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStateBased() {
return stateBased;
}
public void setStateBased(String stateBased) {
this.stateBased = stateBased;
}
public String getPriority() {
return priority;
}
public void setPriority(String priority) {
this.priority = priority;
}
public String getGroupadress() {
return groupadress;
}
public void setGroupadress(String group) {
this.groupadress = convert_groupadress(group);
}
public String getDptID() {
return dptID;
}
public void setDptID(String dptID) {
this.dptID = dptID;
}
@Override
public String toString() {
return "[[" + this.stateBased + "] ["+ this.name + "] [" + this.mainNumber + "]" + " [" + this.dptID
+ "] [" + this.priority + "] [" + this.groupadress + " ]]";
}
public String convert_groupadress(String groupadress) {
String groupaddress ="";
int grpadr = Integer.parseInt(groupadress);
int first = grpadr/2048;
int first_res = grpadr-(first*2048);
int second = first_res/256;
int second_res = first_res-(second*256);
int third = second_res/1;
//int third_res = second_res-(third*32);
groupaddress = Integer.toString(first) +"/"+ Integer.toString(second) + "/"+ Integer.toString(third);
return groupaddress;
}
}