我是android中的新手。我想从列表视图中选择一个获取和存储项目ID。该项目列表已经从mysql数据库中检索。我该怎么做? 这是我的代码。
ProjectSelect
public class ProjectSelect extends AppCompatActivity {
Toolbar mToolbar;
int sempid;
private ListView ProjectList;
String a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_project_select);
mToolbar = (Toolbar)findViewById(R.id.project_select_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("SELECT A PROJECT");
GlobalVariable g = (GlobalVariable)getApplicationContext();
sempid=g.getEmpid();
Log.d("project emp",sempid+"");
ProjectList = (ListView)findViewById(R.id.projectSelect);
getProject("http://www.mybusket.com/pmsapp/webs/get_all_project.php?vempid="+sempid+"");
}
private void getProject(final String webUrlService){
class GetJSON extends AsyncTask<Void, Void, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
LoadIntoListView(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(webUrlService);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
}
GetJSON getJSON = new GetJSON();
getJSON.execute();
}
private void LoadIntoListView(String json) throws JSONException{
JSONObject jsonObject = new JSONObject(json);
if (jsonObject != null){
JSONArray jsonArray = jsonObject.getJSONArray("project");
final String[] project = new String[jsonArray.length()];
for (int i = 0; i <jsonArray.length(); i ++){
JSONObject obj = jsonArray.getJSONObject(i);
project[i] = obj.getString("projectname");
}
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, project);
ProjectList.setAdapter(arrayAdapter);
ProjectList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
a = ProjectList.getItemAtPosition(position).toString();
Log.e("pname",a+"");
}
});
}
}
我想将所选的项目ID存储在全局变量类中。我已经从mysql数据库中检索了所有项目。请帮助我。
答案 0 :(得分:0)
使用hasMap创建一个单例类,并将值临时存储在hasmap中。当您选择一个值时,将该值添加到类中并在需要时检索它。
public class IdClass {
private Map<Integer, String> clickedIDs = new HashMap<>();
private static IdClass idClass;
public static IdClass getIdClass() {
if (idClass == null) {
idClass = new IdClass();
}
return idClass;
}
public void addId(int id,String idValue) {
clickedIDs.put(id,idValue);
}
public String getId(int id) {
return clickedIDs.get(id);
}
}
当您点击某个项目时
ProjectList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Storing inside the IdClass
IdClass.getIdClass().addId(position,ProjectList.getItemAtPosition(position).toString());
Log.e("pname",a+"");
}
});
答案 1 :(得分:0)
尝试以下代码
final String[] project = new String[jsonArray.length()];
final String[] projectId = new String[jsonArray.length()]; // create array to store id
for (int i = 0; i <jsonArray.length(); i ++){
JSONObject obj = jsonArray.getJSONObject(i);
project[i] = obj.getString("projectname");
projectId[i] = obj.getString("projectid");// stroe id
}
最后点击下面的项目点击
a = projectId[postion];
答案 2 :(得分:-1)
你可以对你有兴趣获取id的每个视图做一件事setClickListener。当调用该视图的回调方法时,它将返回如下面代码中所述的视图。
ivMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//when this method is called use below code
v.getId();
}
})
如果你想获得Activity上的视图的id,那么你可以再做一件事。将此以下属性设置为您有兴趣获取ID的视图
android:onClick="someMethod"
单击具有上述属性的视图时,它将调用属性中提到的方法。您需要在活动中定义方法,如下所述
public void someMethod(View view) {
view.getId(); //and done
}