gpt4 book ai didi

android - 如何使用android获取id onclick listview

转载 作者:行者123 更新时间:2023-11-30 21:59:39 26 4
gpt4 key购买 nike

我正在处理 listview 示例,我想从 mysql 数据库 onclick selected listview 获取 tid。怎么做请帮助我,我在这方面工作了很长时间,但找不到任何解决方案。

ListTask Activity

public class ListTask extends Activity {

Task task_;
ListView listCollege;
ProgressBar proCollageList;

TextView GetTaskName;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_task);

listCollege = (ListView)findViewById(R.id.tasklist);
proCollageList = (ProgressBar)findViewById(R.id.proCollageList);
GetTaskName = (TextView) findViewById(R.id.taskname);

new GetHttpResponse(this).execute();
}
private class GetHttpResponse extends AsyncTask<Void, Void, Void>
{
private Context context;
String result;
List<Task> Task_;
public GetHttpResponse(Context context)
{
this.context = context;
}

@Override
protected void onPreExecute()
{
super.onPreExecute();
}

@Override
protected Void doInBackground(Void... arg0)
{
HttpService httpService = new HttpService("http://192.168.0.103/GroupBuilder/GetListTask.php");
try
{
httpService.ExecutePostRequest();

if(httpService.getResponseCode() == 200)
{
result = httpService.getResponse();

System.out.println("Result . . . "+result);

Log.d("Result", result);
if(result != null)
{
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(result);

JSONObject object;
JSONArray array;
// cources college;
Task_ = new ArrayList<Task>();
for(int i=0; i<jsonArray.length(); i++)
{
task_ = new Task();
object = jsonArray.getJSONObject(i);

task_.taskname = object.getString("taskname");
task_.tid = object.getString("tid");

System.out.println("Taask Tid "+task_.tid);
Task_.add(task_);
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
{
Toast.makeText(context, httpService.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

列表适配器类

public class ListTaskAdapter extends BaseAdapter {

Context context;
List<Task> taskList;
Task task_;

public ListTaskAdapter(List<Task> listValue, Context context)
{
this.context = context;
this.taskList = listValue;
}

@Override
public int getCount() {
return this.taskList.size();
}

@Override
public Object getItem(int position) {
return this.taskList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, final ViewGroup viewGroup) {

ViewTaskName viewtaskname = null;
if(convertView == null)
{
viewtaskname = new ViewTaskName();
LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInfiater = LayoutInflater.from(context);
convertView = layoutInfiater.inflate(R.layout.listtaskadapter, null);

viewtaskname.task_name = (TextView)convertView.findViewById(R.id.taskname);

final String str = viewtaskname.task_name.getText().toString();
convertView.setTag(viewtaskname);
}
else
{
viewtaskname = (ViewTaskName) convertView.getTag();
}

viewtaskname.task_name.setText(taskList.get(position).taskname);
return convertView;
}
}
class ViewTaskName
{
TextView task_name;
}


class Task{
public String taskname;
public String tid;
}

activity_list_task.xml代码

<ListView
android:id="@+id/tasklist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:background="#38a2ed"
android:layout_marginTop="35dp" >
</ListView>

listadaper代码

 <TextView
android:id="@+id/taskname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TaskName"
android:textColor="#000000"
android:textSize="18sp"
/>

我没有得到任何解决方案,请帮助我并更新我的代码,谢谢

最佳答案

你应该使用 setOnItemClickListener

Register a callback to be invoked when an item in this AdapterView has been clicked.

创建一个 TextView,其中包含 Name 之类的 ID。

<TextView
android:id="@+id/taskID"
android:layout_width="0dp"
android:layout_height="0dp"

/>

然后

 listCollege.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() 
{
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
TextView taskIDOBJ=(TextView)view.findViewById(R.id.taskID);
String item = tasknameOBJ.getText().toString();
Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();
}
});

简单的方法

 String str_ID = Task_.get(position).tid;

代码

 listCollege.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() 
{
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
String str_ID = Task_.get(position).tid;
Toast.makeText(this,"You selected : " + str_ID ,Toast.LENGTH_SHORT).show();
}
});

关于android - 如何使用android获取id onclick listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43800988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com