从Android中的项目列表获取唯一的项目id
我正在创建android项目,因为我正在使用itemlist视图和分页。当我点击某个特定的项目时,我想得到该项目的id,但我没有得到唯一的id 当我使用position时,每一页都会得到0-9表 我有“审计id”字段。我希望将此值指定为项目id,并希望获取。是否可能 我的代码是:从Android中的项目列表获取唯一的项目id,android,list,pagination,android-arrayadapter,Android,List,Pagination,Android Arrayadapter,我正在创建android项目,因为我正在使用itemlist视图和分页。当我点击某个特定的项目时,我想得到该项目的id,但我没有得到唯一的id 当我使用position时,每一页都会得到0-9表 我有“审计id”字段。我希望将此值指定为项目id,并希望获取。是否可能 我的代码是: private class AsyncLogin extends AsyncTask<String, String, StringBuilder> { ProgressDialog pdLo
private class AsyncLogin extends AsyncTask<String, String, StringBuilder> {
ProgressDialog pdLoading = new ProgressDialog(Tblview.this);
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected StringBuilder doInBackground(String... params) {
try {
// Enter URL address where your php file resides
url = new URL("http://192.168.1.99/ashwad/ims/webservices/alldata.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "URL Exception", Toast.LENGTH_LONG).show();
e.printStackTrace();
return null;
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
// setDoInput and setDoOutput method depict handling of both send and receive
conn.setDoInput(true);
conn.setDoOutput(true);
// Append parameters to URL
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("user_id", "sdfa")
.appendQueryParameter("password", "asffs");
String query = builder.build().getEncodedQuery();
// Open connection for sending data
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return null;
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
result = new StringBuilder();
String next1;
while ((next1 = bufferedReader.readLine()) != null) {
result.append(next1 + "\n");
}
Log.e("dfasf",result.toString());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
conn.disconnect();
}
return result;
}
@Override
protected void onPostExecute(StringBuilder s) {
super.onPostExecute(s);
try {
JSONArray login;
JSONObject obj = new JSONObject(s.toString());
if(s.toString().contains("Result")) {
data = new ArrayList<String>();
login = obj.getJSONArray("Result");
for(int i=0;i<login.length();i++) {
JSONObject c = login.getJSONObject(i);
productsArray = c.getJSONArray(Latest_Products);
TOTAL_LIST_ITEMS=productsArray.length();
int val = TOTAL_LIST_ITEMS%NUM_ITEMS_PAGE;
val = val==0?0:1;
pageCount = (TOTAL_LIST_ITEMS/NUM_ITEMS_PAGE)+val;
for (int j = 0; j < productsArray.length(); j++) {
JSONObject cc = productsArray.getJSONObject(j);
//------------------------------------------------------------------------
Log.e("audit",cc.getString("phone_name"));
String audit_id_str = cc.getString("audit_id");
int audit_id =Integer.parseInt(audit_id_str);
listview.setSelection(audit_id);
data.add(cc.getString("phone_name") +"\n\n"+cc.getString("audit_status") );
loadList(0);
btn_next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
increment++;
loadList(increment);
CheckEnable();
}
});
btn_prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
increment--;
loadList(increment);
CheckEnable();
}
});
//------------------------------------------------------------------------
}
}
pdLoading.dismiss();
//CheckEnable();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void loadList(int number)
{
ArrayList<String> sort = new ArrayList<String>();
title.setText("Page "+(number+1)+" of "+pageCount);
int start = number * NUM_ITEMS_PAGE;
for(int i=start;i<(start)+NUM_ITEMS_PAGE;i++)
{
if(i<data.size())
{
sort.add(data.get(i));
}
else
{
break;
}
}
sd = new ArrayAdapter<String>(Tblview.this,android.R.layout.simple_list_item_1,sort);
listview.setAdapter(sd);
}
private void CheckEnable()
{
if(increment+1 == pageCount)
{
btn_next.setEnabled(false);
btn_prev.setEnabled(true);
}
else if(increment == 0)
{
btn_prev.setEnabled(false);
btn_next.setEnabled(true);
}
else
{
btn_prev.setEnabled(true);
btn_next.setEnabled(true);
}
}
}
私有类AsyncLogin扩展了AsyncTask{
ProgressDialog pdLoading=新建ProgressDialog(Tblview.this);
httpurl连接连接;
URL=null;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//此方法将在UI线程上运行
pdLoading.setMessage(“\t加载…”);
pdLoading.setCancelable(假);
pdLoading.show();
}
@凌驾
受保护的StringBuilder doInBackground(字符串…参数){
试一试{
//输入php文件所在的URL地址
url=新url(“http://192.168.1.99/ashwad/ims/webservices/alldata.php");
}捕获(格式错误){
//TODO自动生成的捕捉块
Toast.makeText(getApplicationContext(),“URL异常”,Toast.LENGTH_LONG.show();
e、 printStackTrace();
返回null;
}
试一试{
//设置HttpURLConnection类以从php和mysql发送和接收数据
conn=(HttpURLConnection)url.openConnection();
conn.setReadTimeout(读取超时);
连接设置连接超时(连接超时);
conn.setRequestMethod(“POST”);
//setDoInput和setDoOutput方法描述了发送和接收的处理
conn.setDoInput(真);
连接设置输出(真);
//将参数附加到URL
Uri.Builder=新的Uri.Builder()
.appendQueryParameter(“用户id”、“sdfa”)
.appendQueryParameter(“密码”、“asffs”);
字符串查询=builder.build().getEncodedQuery();
//打开用于发送数据的连接
OutputStream os=conn.getOutputStream();
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(os,“UTF-8”));
writer.write(查询);
writer.flush();
writer.close();
os.close();
连接();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
返回null;
}
试一试{
int response_code=conn.getResponseCode();
//检查连接是否成功
if(response\u code==HttpURLConnection.HTTP\u OK){
//读取从服务器发送的数据
InputStream input=conn.getInputStream();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(输入));
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
结果=新的StringBuilder();
字符串next1;
而((next1=bufferedReader.readLine())!=null){
结果。追加(next1+“\n”);
}
Log.e(“dfasf”,result.toString());
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
连接断开();
}
返回结果;
}
@凌驾
PostExecute上受保护的void(StringBuilder s){
super.onPostExecute(s);
试一试{
JSONArray登录;
JSONObject obj=新的JSONObject(s.toString());
如果(s.toString()包含(“结果”)){
数据=新的ArrayList();
login=obj.getJSONArray(“结果”);
对于(int i=0;i如果您保持页面计数,则可以将页码添加到项目位置。
这将为每个项目提供唯一的编号。如何在listview.setOnItemClickListener方法中获取页码?您可以解释一下吗?创建接受数据列表和当前页码的自定义数组适配器。并将该适配器用于列表。
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int positon1 =position;
String a1 = Integer.toString(positon1);
Toast.makeText(getApplicationContext(),a1,Toast.LENGTH_SHORT).show();
}
});