这是我的ListView代码,我需要按ID而不是按位置选择元素。 任何人都可以帮我解决这个问题,我尝试过使用位置但是当我删除listView时,一个新的ListView进入了我不想要的位置!
public class MainActivity extends AppCompatActivity {
// The data to show
List<Planet> planetsList = new ArrayList<Planet>();
PlanetAdapter aAdpt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
// We get the ListView component from the layout
ListView lv = (ListView) findViewById(R.id.listView);
// This is a simple adapter that accepts as parameter
// Context
// Data list
// The row layout that is used during the row creation
// The keys used to retrieve the data
// The View id used to show the data. The key number and the view id must match
//aAdpt = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, planetsList);
aAdpt = new PlanetAdapter(planetsList, this);
lv.setAdapter(aAdpt);
// React to user clicks on item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
if (position == 0) {
Toast.makeText(MainActivity.this, "Jello", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),PhotoActivity.class);
startActivity(intent);
}
if (position == 1) {
Toast.makeText(MainActivity.this, "Jello", Toast.LENGTH_SHORT).show();
}
}
});
// we register for the contextmneu
registerForContextMenu(lv);
}
// We want to create a context Menu when the user long click on an item
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;
// We know that each row in the adapter is a Map
Planet planet = aAdpt.getItem(aInfo.position);
menu.setHeaderTitle("Options for " + planet.getName());
menu.add(1, 2, 2, "Delete");
}
// This method is called when user selects an Item in the Context menu
@Override
public boolean onContextItemSelected(MenuItem item) {
int itemId = item.getItemId();
AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) item.getMenuInfo();
planetsList.remove(aInfo.position);
aAdpt.notifyDataSetChanged();
return true;
}
private void initList() {
// We populate the planets
planetsList.add(new Planet("Work Gallery", 10));
planetsList.add(new Planet("Buildings Gallery", 20));
planetsList.add(new Planet("Miscellaneous", 30));
}
// Handle user click
public void addPlanet(View view) {
final Dialog d = new Dialog(this);
d.setContentView(R.layout.dialog);
d.setTitle("Add planet");
d.setCancelable(true);
final EditText edit = (EditText) d.findViewById(R.id.editTextPlanet);
Button b = (Button) d.findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String planetName = edit.getText().toString();
MainActivity.this.planetsList.add(new Planet(planetName, 0));
MainActivity.this.aAdpt.notifyDataSetChanged(); // We notify the data model is changed
d.dismiss();
}
});
d.show();
}
}
Planet.java
public class Planet {
private String name;
private Integer distance;
private String descr;
private int index;
public Planet(String name, Integer distance) {
this.name = name;
this.distance = distance;
}
public Planet(String name, String descr) {
this.name = name;
this.descr = descr;
}
public Planet(String name, Integer distance, String descr, int idImg) {
this.name = name;
this.distance = distance;
this.descr = descr;
this.index = idImg;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getDistance() {
return distance;
}
public void setDistance(Integer distance) {
this.distance = distance;
}
public int getIdImg() {
return index;
}
public void setIdImg(int idImg) {
this.index = idImg;
}
}
答案 0 :(得分:1)
假设你,
想要通过该对象的ID访问Planet Object而不是您的位置
在Plannet
班级
如果是的话,
你可以做点什么,
if (planetsList.get(position).getId() == 0) {
编辑:
在您的Planet
课程中
public Planet(String name, Integer distance, Integer index) {
this.name = name;
this.distance = distance;
this.index= index;
}
并且还为您的类变量index
设置了getter和setter。
在你的代码中,
planetsList.add(new Planet("Work Gallery", 10,0));
planetsList.add(new Planet("Buildings Gallery", 20,1));
planetsList.add(new Planet("Miscellaneous", 30,2));
答案 1 :(得分:0)
您也可以在适配器中使用方法view.setTag()。把id放在视野中。并在OnClickListener中获取它。例如:
public View getView(int position, View convertedView, ViewGroup parent) {
converterView = createView();
convertedView.setTag(items.get(position).getId())
}
lv.setOnClickListener(new OnclickListener -> int id = (Integer) view.getId());